Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2006
    Location
    Lancashire, UK
    Posts
    473

    need php help before i die with frustration

    Here is some php from my ticker file, for my online game. The code that im struggling with is the code that calculates the battles

    I run a loop to go through an array, write the value in the current element of the array into a variable

    i then pass these variables through a function called attack

    this function is supposed to multiply one set of numbers (a battle statistic stored in array, by a value held in a variable)

    These values are passed into the attack function,

    and the sum of that mutiplication is removed from the first variable.

    here is the attack function

    PHP Code:
    Function Attack ($unitone,$unittwo,$amount)
    {


    echo 
    "$unittwo ships hit $unitone ships with an attack speed of $amount ";


        
    $tmp $unitone;
        
    $tmp2 0;
        
    $unitone round($unitone $amount);
        if (
    $unitone<0$unitone 0;

    echo 
    "so the first unit was reduced to $unitone<BR>";

    and here is what happens in the actual code:

    PHP Code:
    //a loop to calculate the battle by running through values in the array
        
    for ($x 1$x <= $total_defenders$x++) {
            
    // if there was a victim in the war column
            
    if ($kill_id_1["$x"] > 0) {
                
    $current_kill $kill_id_1["$x"];


    //i write the current value in the array into a variable - to save typing later



    $d_uone $total_d_one[$current_kill];
    $d_utwo $total_d_two[$current_kill];
    $d_uthree $total_d_three[$current_kill];
    $d_ufour $total_d_four[$current_kill];
    $d_ufive $total_d_five[$current_kill];
    $d_usix $total_d_six[$current_kill];
    $d_useven $total_d_seven[$current_kill];
    $d_ueight $total_d_eight[$current_kill];
    $d_unine $total_d_nine[$current_kill];
    $d_uten $total_d_ten[$current_kill];
    $d_ueleven $total_d_elelven[$current_kill];
    $d_utwelve $total_d_twelve[$current_kill];

    $uone $total_a_one[$current_kill];
    $utwo $total_a_two[$current_kill];
    $uthree $total_a_three[$current_kill];
    $ufive $total_a_five[$current_kill];
    $usix $total_a_six[$current_kill];
    $useven $total_a_seven[$current_kill];
    $ueight $total_a_eight[$current_kill];
    $unine $total_a_nine[$current_kill];

    //here is a sample of what goes on.



                
    attack($uone,$d_ueight$d_ueight $stats['eight']['one']);
                
    attack($ueight,$d_ueight$d_ueight $stats['eight']['eight']);
                
    attack($utwo,$d_ueight$d_ueight $stats['eight']['two']);
                
    attack($uthree,$d_ueight$d_ueight $stats['eight']['three']);
                
    attack($ufive,$d_ueight$d_ueight $stats['eight']['five']);
                
    attack($usix,$d_ueight$d_ueight $stats['eight']['six']);
                
    attack($useven,$d_ueight$d_ueight $stats['eight']['seven']);
                
    attack($unine,$d_ueight$d_ueight $stats['eight']['nine']);

    //ok, so the attacking units ($uone etc) get hit by the defending unit eights ($d_ueight). the amount they get hit
    //by is the $d_ueight * $stats['eight']['one']. The $stats array is stored in another file, but that seems to work ok

    }



    the attack function outputs this to me


    40000 ships hit 200 ships with an attack speed of 833.33333333333 so the first unit was reduced to 0
    40000 ships hit 100 ships with an attack speed of 3.3333333333333 so the first unit was reduced to 97
    40000 ships hit 100 ships with an attack speed of 500 so the first unit was reduced to 0
    40000 ships hit 0 ships with an attack speed of 500 so the first unit was reduced to 0
    40000 ships hit 0 ships with an attack speed of 83.333333333333 so the first unit was reduced to 0
    40000 ships hit 0 ships with an attack speed of 50 so the first unit was reduced to 0
    so 40,000 being the $d_ueight and it hits for example 100 ships, and its power knocks it down to 97

    here is the problem

    when it echoes to me that it has been reduced to 97, when i use that variable again a few lines down, it hasnt actually changed it to 97, so where i removes the $amount from itself, it never actually does


    i have been coding php for a long time, and never has something slowed me down so badly.

    Is it an easy error that i am overlooking?

    please help me before i pull my hair out!!!

    mike

  2. #2
    I must admit that I didn't read it all carefully but your attack() function is supposed to modify the first parameter passed (variable,) which it doesn't as you're not passing it by reference.

    You should do it like this:

    PHP Code:

    $uone 
    attack($uone,$d_ueight$d_ueight $stats['eight']['one']); 
    Also, attack() should return a value. That way you modified the variable.

    Another way would be:

    PHP Code:
    attack(&$uone$d_ueight$d_ueight $stats['eight']['one']);

    // and attack function:

    function attack (&$unitone$unittwo,$amount)
    {
        
    // processing here.

    Dyslexics Have More Fnu

  3. #3
    Join Date
    Feb 2006
    Location
    Lancashire, UK
    Posts
    473
    ok, i will give it a go....thanks

  4. #4
    Join Date
    Feb 2006
    Location
    Lancashire, UK
    Posts
    473
    thanks maxymizer, it worked

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •