Castle Paradox Forum Index Castle Paradox

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 Gamelist   Review List   Song List   All Journals   Site Stats   Search Gamelist   IRC Chat Room

Using "Random" properly

 
Post new topic   Reply to topic    Castle Paradox Forum Index -> HELP!
View previous topic :: View next topic  
Author Message
Meatballsub
Divine Bovine




Joined: 16 Jun 2003
Posts: 437
Location: Northwest Georgia

PostPosted: Tue Apr 03, 2007 6:43 am    Post subject: Using "Random" properly Reply with quote

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
View user's profile Send private message Visit poster's website
Bob the Hamster
OHRRPGCE Developer




Joined: 22 Feb 2003
Posts: 2526
Location: Hamster Republic (Southern California Enclave)

PostPosted: Tue Apr 03, 2007 7:11 am    Post subject: Re: Using "Random" properly Reply with quote

"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
View user's profile Send private message Send e-mail Visit poster's website
Meatballsub
Divine Bovine




Joined: 16 Jun 2003
Posts: 437
Location: Northwest Georgia

PostPosted: Tue Apr 03, 2007 7:19 am    Post subject: Reply with quote

Dude, I NEVER understood how to store stuff in variables until now. Thanks so much for clearing that up Happy Can't wait to try it out.
_________________
MOCBJ Software - My Games
The Hamster Wheel - OHRRPGCE Information Database
Back to top
View user's profile Send private message Visit poster's website
Uncommon
His legend will never die




Joined: 10 Mar 2003
Posts: 2503

PostPosted: Tue Apr 03, 2007 5:43 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website AIM Address
Raekuul
Delicious!




Joined: 31 Mar 2004
Posts: 641
Location: Nowhere

PostPosted: Thu Apr 05, 2007 5:57 pm    Post subject: Reply with quote

Cleaner. I like that. Horray for *real* programmers!
_________________
A broken clock is still right twice a day.
Back to top
View user's profile Send private message Visit poster's website
Uncommon
His legend will never die




Joined: 10 Mar 2003
Posts: 2503

PostPosted: Thu Apr 05, 2007 8:19 pm    Post subject: Reply with quote

I am trying to understand what you mean by that because I'm sure I'm not reading it right.
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
Display posts from previous:   
Post new topic   Reply to topic    Castle Paradox Forum Index -> HELP! All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group