 |
Castle Paradox
|
View previous topic :: View next topic |
Author |
Message |
Uncommon His legend will never die

Joined: 10 Mar 2003 Posts: 2503
|
Posted: Sun Apr 26, 2009 7:04 pm Post subject: Help with the Random command |
|
|
Oh man it has been so long since I've done this, I've gotten rusty as hell.
I want to make a script that calls a few different items randomly, is there an easy way to do this, or will I just have to make if statements for each item to make sure it isn't accessed twice? |
|
Back to top |
|
 |
Pepsi Ranger Reality TV Host

Joined: 05 Feb 2003 Posts: 493 Location: South Florida
|
Posted: Sun Apr 26, 2009 8:06 pm Post subject: |
|
|
Are the item numbers in succession to each other?
Try:
Code: | set variable (compare1,inventory (item first))
set variable (compare2,inventory (item second))
set variable (compare3,inventory (item third))
set variable (compare4,inventory (item fourth))
set variable (compare5,inventory (item last))
#this checks how many of each item you already have.
for (amount,1,5) do(
get item (random (first item,last item))
#this pulls an item from your range of options 5 times.
if ((inventory (item first)--compare1)>=2) then(
delete item (item first)
)
if ((inventory (item second)--compare2)>=2) then(
delete item (item second)
)
if ((inventory (item third)--compare3)>=2) then(
delete item (item third)
)
if ((inventory (item fourth)--compare4)>=2) then(
delete item (item fourth)
)
if ((inventory (item last)--compare5)>=2) then(
delete item (item last)
)
#these make sure you don't gain more than one item of each.
)
#this closes the for / do block. |
It's probably sketchy, but should serve as a launching point for jogging your memory. _________________ Progress Report:
The Adventures of Powerstick Man: Extended Edition
Currently Updating: General sweep of the game world and dialogue boxes. Adding extended maps.
Tightfloss Maiden
Currently Updating: Chapter 2 |
|
Back to top |
|
 |
Uncommon His legend will never die

Joined: 10 Mar 2003 Posts: 2503
|
Posted: Sun Apr 26, 2009 8:42 pm Post subject: |
|
|
I should've been clearer. I didn't mean items as in inventory items so much as like "items on an agenda". In this case they're actually scripts I want to run in a random order.
Isn't there being a way to refer to a global variable by its ID number? If so I guess I could do something like:
Code: | variable (this)
this := random (first global ID, last global ID)
if (this==true)
then,begin
while (this==true)
do (this := random (first global ID, last global ID))
end
else (do whatever) |
Though the problem there is I'd still have to check the value of "this" to know which script to run since you can't run a script from its id number... |
|
Back to top |
|
 |
TMC On the Verge of Insanity
Joined: 05 Apr 2003 Posts: 3240 Location: Matakana
|
Posted: Mon Apr 27, 2009 3:16 am Post subject: |
|
|
'plot:Read global' and 'plot:Write global'
Quote: | Though the problem there is I'd still have to check the value of "this" to know which script to run since you can't run a script from its id number... |
I see you have never heard of 'plot:Run script by ID'. But that doesn't really make your script any simpler, unless your scripts all have consecutive ID numbers.
Here's one possibility. It does nothing if all scripts have already been called.
Code: | variable (this)
for (this, first global ID, last global ID) do, begin
if (read global (this) == false) then, begin
# there's at least one script that hasn't been called yet
this := random (first global ID, last global ID)
while (read global(this))
do (this := random (first global ID, last global ID))
write global (this, true)
switch (this -- first global ID) do, begin
case (0) do (script 1(...))
case (1) do (script 2(...))
...
end
exit script
end
end
#no scripts haven't been called yet. Do something special |
_________________ "It is so great it is insanely great." |
|
Back to top |
|
 |
Uncommon His legend will never die

Joined: 10 Mar 2003 Posts: 2503
|
Posted: Mon Apr 27, 2009 6:29 am Post subject: |
|
|
The Mad Cacti wrote: | Quote: | Though the problem there is I'd still have to check the value of "this" to know which script to run since you can't run a script from its id number... |
I see you have never heard of 'plot:Run script by ID'. But that doesn't really make your script any simpler, unless your scripts all have consecutive ID numbers. |
Run script by ID? We have those now? Why, back in my day mutter mutter mutter.
But giving the scripts consecutive ID numbers really isn't much of a problem, I guess I'd just have to use "define script" now. In the current syntax it doesn't look like there's any other way. Might be even easier if I make sure the globals and the accompanying scripts have the same ID.
And we have switch/cases now? Cool!
Thanks for your help, it's been about two years since I've touched the engine, and I dunno how long it was before that. |
|
Back to top |
|
 |
Moogle1 Scourge of the Seas Halloween 2006 Creativity Winner


Joined: 15 Jul 2004 Posts: 3377 Location: Seattle, WA
|
Posted: Mon Apr 27, 2009 7:31 am Post subject: |
|
|
You can also use the syntax @scriptname to refer to the script's id number, but define script will probably be easier for your purposes. _________________
|
|
Back to top |
|
 |
Uncommon His legend will never die

Joined: 10 Mar 2003 Posts: 2503
|
Posted: Tue Apr 28, 2009 11:01 am Post subject: |
|
|
Looking at this script TMC wrote, a few things stand out. One, the whole switch/case isn't necessary if I'm doing the define script/run script by id, since the whole idea there was to make the scripts have the same id as the global associated with them, so something as simple as this should work, right?
Code: | this := random (first global ID, last global ID)
while (read global(this))
do (this := random (first global ID, last global ID))
write global (this, true)
run script by id (this) |
A few other things I've gotta wonder, will using the variable "this" inside the script and as the for loop's counter as you did mess with the loop at all? It really seems like it should, but I guess you'd know better than me.
Also, why the exit script? Isn't that going to break the loop, too?
Also, would setting the variable to true disrupt the flow control? I was gonna put it toward the end of the accompanying script.
EDIT: Alright, both scripts work fine without the questionable bits, though both also hang if all scripts have been called, I guess something needs to be in the while loop to check if all the globals are true.
Do I have a misconception about this here, or shouldn't the for loop make it so it runs through all ten scripts before quitting? Because it's only running through one and ending. This is easy to fix, I'm just making sure that's what it should be doing. |
|
Back to top |
|
 |
Moogle1 Scourge of the Seas Halloween 2006 Creativity Winner


Joined: 15 Jul 2004 Posts: 3377 Location: Seattle, WA
|
Posted: Tue Apr 28, 2009 12:02 pm Post subject: |
|
|
The problem with the "for" loop is that the loop variable is being changed inside of the loop. If you use one variable for the for loop and another variable for "this," you should be okay.
PS. Using "this" as a variable name is a surefire way to confuse anyone with a background in C++, Java, Javascript, or any other OO language. Shame on you, TMC. _________________
|
|
Back to top |
|
 |
Uncommon His legend will never die

Joined: 10 Mar 2003 Posts: 2503
|
Posted: Tue Apr 28, 2009 7:22 pm Post subject: |
|
|
Yeah, I'd tried it first with a rewrite of my own that used a different variable for the for loop before trying his and got the same behavior out of both. I figure even if the variable was changed, it should've run at least twice if the first script called wasn't the highest numbered one, but each time it ran only once.
I'm thinking about faking the "for" loop with a "while", so I know it'll end on time.
Also "this" was mine in the little mockup I did towards the top, so it's not TMC's fault. I don't have a background in C++, Java, Javascript, or any other OO language, so I wouldn't know any better (and i tend to name variables like that sometimes).
EDIT: Dammit, now it's just freezing at the end of the each called script. >:(
I don't even know what's the deal anymore. |
|
Back to top |
|
 |
TMC On the Verge of Insanity
Joined: 05 Apr 2003 Posts: 3240 Location: Matakana
|
Posted: Wed Apr 29, 2009 12:51 am Post subject: |
|
|
Oh, you weren't very specific, so I assumed that you wanted a single script to be called every time (such as an NPC doing a different thing every time you talk to it) but you seem to be implying that you want all the scripts to be run consecutively, in a random order?
That's what the exit script was for. As for reusing the same variable - my bad, but it wouldn't have made a difference anyway because of the exit script.
Code: | variable (i, this)
for (i, first global ID, last global ID) do ( # loop correct number of times
this := random (first global ID, last global ID)
while (read global(this))
do (this := random (first global ID, last global ID))
write global (this, true)
run script by id (this)
) |
_________________ "It is so great it is insanely great." |
|
Back to top |
|
 |
Uncommon His legend will never die

Joined: 10 Mar 2003 Posts: 2503
|
Posted: Wed Apr 29, 2009 3:36 am Post subject: |
|
|
I've got it taken care of. Thanks for the refresher on read/write global and letting me know about run script by id, though. |
|
Back to top |
|
 |
|
|
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
|