Minase Posted September 19, 2008 Share Posted September 19, 2008 hy there im having an issue with a syntax,dont know where the error is (its more like a math problem,but its not that complex and it should be here) $attacker->Dexterity = 100; $defender->Dexterity = 150; $dodge = $attacker->Dexterity - $defender->Dexterity; if ($dodge < 1) { $dodge = 0; } it seem that the dodge variable has no content after IF . weird enough... at first i though it was a math problem,but when i did write this topic i did check where the error was,and i did find that dodge variable disapear after IF. thanks Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 19, 2008 Share Posted September 19, 2008 This script <pre><?php $dexA = 100; $dexB = 150; $dodge = $dexA - $dexB; var_dump($dodge); if ($dodge < 1) { $dodge = 0; } var_dump($dodge); ?></pre> Returns int(-50) int(0) As expected. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 19, 2008 Share Posted September 19, 2008 When I run that code, $dodge equals zero after the if statement, as it should (100 - 150 = -50, thus the if sets it to 0). Are you saying it has a null value for you? Quote Link to comment Share on other sites More sharing options...
Minase Posted September 19, 2008 Author Share Posted September 19, 2008 found the problem but how to fix it?? $dc = ($r < $dodge) ? 1 : 0; $r is a random number $dodge is a calculated one so $dc = (10 < 0) ? 1 : 0; its false and it should return 0 ... but it doesnt return =/ Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 19, 2008 Share Posted September 19, 2008 Then your PHP is broken? <pre><?php $r = 20; $dodge = 10; $dc = ($r < $dodge) ? 1 : 0; var_dump( $dc ); ?></pre> Outputs int(0); Quote Link to comment Share on other sites More sharing options...
Minase Posted September 19, 2008 Author Share Posted September 19, 2008 i dont think its broken this one works $test1 = 250 - 402; if ($test1 < 1) { $test1 = 0; } $r = rand(0, 99); $t1 = ($r < $test1) ? 1 : 0; echo $t1; its indeed very weird... Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 19, 2008 Share Posted September 19, 2008 I think it's a small typing error on your part, or a minor logic flaw. Echo all expected values before attempting to compare them to make sure they're what you expect. Also check spelling and case on variables... they shouldn't just unset themselves. error_reporting( E_ALL ); at the top of your script might help. Quote Link to comment Share on other sites More sharing options...
Minase Posted September 19, 2008 Author Share Posted September 19, 2008 thanks for reply but i already did that,no success here is something interesting that i found $attacker->Dexterity is set everything is fine but after this part it is unset... $rounds1 = rand(5,10); for($r = 1; $r <= $rounds1 ; $r++){ // DEFENDER ATTACK if ($r % 2 == 0) { $dodge = $attacker->Dexterity - $defender->Dexterity; after the if ($r % 2 == 0) { the variables stop to work.. but when it come } else { $dodge = $defender->Dexterity - $attacker->Dexterity; .... } it work normally ??? Quote Link to comment Share on other sites More sharing options...
.josh Posted September 19, 2008 Share Posted September 19, 2008 as disco said, turn your error reporting on. I think the more likely thing is that $attacker and/or $defender is not being instantiated somehow or maybe they are not within the scope of that script there (not passed to a function, etc...) so it's ending up basically saying $dodge = 0 - 0; or even $dodge = 0 - 100; causing the if to evaluate as true. Just a thought. Try echoing out $attacker->dexterity and $defender->dexterity after you set them. Quote Link to comment Share on other sites More sharing options...
Minase Posted September 19, 2008 Author Share Posted September 19, 2008 Crayon Violent read my above post please.. thanks Quote Link to comment Share on other sites More sharing options...
Minase Posted September 19, 2008 Author Share Posted September 19, 2008 sorry for 2 post,but i want someone please to test this code $rounds1 = rand(5,10); for($r = 1; $r <= $rounds1; $r++){ // DEFENDER ATTACK if ($r % 2 == 0) { $dodge = $attacker->Dexterity - $defender->Dexterity; if ($dodge < 1) { $dodge = 0; } $r = rand(0, 99); $dc1 = ($r < $dodge) ? 1 : 0; echo "DC1 :",$dc1; // ATTACKER ATTACK } else { $dodge = $defender->Dexterity - $attacker->Dexterity; if ($dodge < 1) { $dodge = 0; } $r = rand(0, 99); $dc2 = ($r < $dodge) ? 1 : 0; echo "DC2 :",$dc2; } } from my knowledge it should return DC1: VALUE DC2: VALUE on my server it wont return those 2 values... weird enough... thank you very much Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 19, 2008 Share Posted September 19, 2008 *sigh* You redefine $r in your loop, and it screws things up. You should be able to catch this kind of stuff on your own. <pre><?php class someClass { public $Dexterity; } $attacker = $defender = new someClass; $attacker->Dexterity = 250; $defender->Dexterity = 402; $rounds1 = rand(2,5)*2; for($r = 1; $r <= $rounds1; $r++){ // DEFENDER ATTACK if ($r % 2 == 0) { $dodge = $attacker->Dexterity - $defender->Dexterity; if ($dodge < 1) { $dodge = 0; } $r2 = rand(0, 99); $dc1 = ($r2 < $dodge) ? 1 : 0; echo "DC1 :$dc1\n"; // ATTACKER ATTACK } else { $dodge = $defender->Dexterity - $attacker->Dexterity; if ($dodge < 1) { $dodge = 0; } $r2 = rand(0, 99); $dc2 = ($r2 < $dodge) ? 1 : 0; echo "DC2 :$dc2\n"; } } ?></pre> I really don't understand your algorithm. Quote Link to comment Share on other sites More sharing options...
Minase Posted September 19, 2008 Author Share Posted September 19, 2008 the real thing is way more too complicated. my hands are frozen right now (just came inside ) and i was wondering why it doesnt continue to loop.. lolz i didnt see that i defined again $r .thanks for pointing Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.