| View previous topic :: View next topic |
| Author |
Message |
Meatballsub Divine Bovine

Joined: 16 Jun 2003 Posts: 437 Location: Northwest Georgia
|
Posted: Tue Apr 03, 2007 6:43 am Post subject: Using "Random" properly |
|
|
So I have this script. It is supposed to roll a random number between 0 and 3, then depending on which number it lands on an action takes place. My script does that, but it runs multiple times instead of running once. I have a hunch it has to do with me using the "while" command. That leads me to beleive I am doing something very wrong, so please glance at my code and let me know what you think. It's not the complete code, but I don't think the rest will be necessary:
| Code: | while (random (0,3)) do (
if (random (0,3) == 0) then (
show textbox (42)
wait for textbox
)
else (if (random (0,3) == 1) then (show textbox (43)
wait for textbox
))
else (if (random (0,3) == 2) then (
show textbox (44)
wait for textbox
get item (13,2)
))
else (if (random (0,3) == 3) then (
show textbox (45)
wait for textbox
get item (13,3)
)))
resume player
resume npcs
set caterpillar mode (on)
wait for all
end |
_________________ MOCBJ Software - My Games
The Hamster Wheel - OHRRPGCE Information Database |
|
| Back to top |
|
 |
Bob the Hamster OHRRPGCE Developer

Joined: 22 Feb 2003 Posts: 2526 Location: Hamster Republic (Southern California Enclave)
|
Posted: Tue Apr 03, 2007 7:11 am Post subject: Re: Using "Random" properly |
|
|
"while" causes the code to repeat over and over again until the expression is false. false == 0, so this block of code will repeat over and over until 0 is rolled randomly. You probably want something more like this:
| Code: |
variable (r)
r := random(0, 3)
if (r == 0) then (
show textbox (42)
wait for textbox
)
if (r == 1) then (
show textbox (43)
wait for textbox
)
if (r == 2) then (
show textbox (44)
wait for textbox
get item (13,2)
)
if (r == 3) then (
show textbox (45)
wait for textbox
get item (13,3)
)
|
note also that we are only calling "random" once, and storing the result in a local variable. |
|
| Back to top |
|
 |
Meatballsub Divine Bovine

Joined: 16 Jun 2003 Posts: 437 Location: Northwest Georgia
|
|
| Back to top |
|
 |
Uncommon His legend will never die

Joined: 10 Mar 2003 Posts: 2503
|
Posted: Tue Apr 03, 2007 5:43 pm Post subject: |
|
|
An easier way to write it would also be
| Code: | variable (r)
r := random(0, 3)
show textbox (42+r)
wait for textbox
if (r >> 1) then(
get item (13,r)) |
|
|
| Back to top |
|
 |
Raekuul Delicious!

Joined: 31 Mar 2004 Posts: 641 Location: Nowhere
|
Posted: Thu Apr 05, 2007 5:57 pm Post subject: |
|
|
Cleaner. I like that. Horray for *real* programmers! _________________ A broken clock is still right twice a day. |
|
| Back to top |
|
 |
Uncommon His legend will never die

Joined: 10 Mar 2003 Posts: 2503
|
Posted: Thu Apr 05, 2007 8:19 pm Post subject: |
|
|
| I am trying to understand what you mean by that because I'm sure I'm not reading it right. |
|
| Back to top |
|
 |
|