|
|
|
|
![]() ![]() |
Mar 20 2008, 02:42 PM
Post
#1
|
|
|
Kinda N00B Group: Members Posts: 230 Joined: 13-January 08 From: Sweden Member No.: 27,579 |
Hey!
Is it possible to use a random script on functions. Lets say I have created three functions (Items that a user will win if he defeates a monster). One function is a function named shield() and another function is named sword() and a third function is named helmet(). Now when a player defeates a monster, he must be awarded. So.. How can I randomize what item he should get? something like CODE rand(sheild(), sword(), helmet()); or? Thanks //Feelay |
|
|
|
Mar 21 2008, 08:21 AM
Post
#2
|
|
|
Premium Member Group: [HOSTED] Posts: 392 Joined: 16-February 06 From: Kolkata, India Member No.: 11,322 |
You can only use the rand function to generate a random number. However, this number can be used to call any of those functions randomly. This can be easily accomplished using a switch - case statement.
CODE switch(rand(1, 3))
{ case 1: sheild(); break; case 2: sword(); break; case 3: helmet(); break; } |
|
|
|
Mar 21 2008, 08:51 AM
Post
#3
|
|
|
Kinda N00B Group: Members Posts: 230 Joined: 13-January 08 From: Sweden Member No.: 27,579 |
oh my
//Feelay |
|
|
|
Mar 21 2008, 09:23 PM
Post
#4
|
|
|
Super Member Group: [HOSTED] Posts: 763 Joined: 8-April 06 From: Lima - Peru Member No.: 12,579 |
I agree with turbopowerdmaxsteel, that's the way to do that in my opinion, but, another way to do the same thing is by using a series of IF and ELSEIF statements on the same expression, for example the following code is another way to write the same thing:
CODE <?php $i=rand(1, 3); if ($i == 1) { sheild(); } elseif ($i == 2) { sword(); } elseif ($i == 3) { helmet(); } ?> Don't forget to always include a break statement at the end of every case, because the switch statement executes line by line and if you don't include it PHP will continue executing the next code until it finds a break statement or the end of the switch block. Also, it is recommended to include a default case that will be executed in case anything match. QUOTE A special case is the default case. This case matches anything that wasn't matched by the other cases, and should be the last case statement. For more information go to PHP: switch - Manual. Best regards, |
|
|
|
May 8 2008, 10:20 AM
Post
#5
|
|
|
Member [ Level 1 ] Group: [HOSTED] Posts: 40 Joined: 17-April 08 Member No.: 29,853 |
Yes you may find at some point through editing that the amount of rewards may change. So it is always wise to have as much error checking in your script as possible:
CODE $rewards_num = 3; $reward = rand (1, $rewards_num); // for now, there are 3 options. switch ($reward) { case 1: helmet (); break; case 2: sword (); break; case 3: helmet (); break; default: wrong_reward (); break; // this would be a function containing a returned value from the rand () that is invalid } And as the previous poster mentioned, check out that link for more info on the switch. |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 12th October 2008 - 03:40 PM |