PDA

View Full Version : Global script questions...?



Nimono
01-30-2008, 04:21 PM
Okay, this has been bugging me, and I want this off my chest NOW.

1: I forgot, what do the two available Global Script slots do?
2: Is it at ALL possible to use Waitframe() in a Global script without it quitting? If not, how would I avoid it freezing the game when it enters a while loop?

Thanks in advance.

Russ
01-30-2008, 04:28 PM
Global script slot two activates every frame of the game. Slot three activates when you load a previously saved file.

And no, you can't have waitframe() in global scripts. They'll just quit there. But there are workarounds you can do by increasing and decreasing global variables, and doing something when the variable hits a certain point.

Joe123
01-30-2008, 04:56 PM
:eyebrow:

Yeah, just stick Waitframe(); at the end of your while loop in the 2nd global script like you would in an ffc script.

Works the same way, I don't get what all the fuss is about.

Dunno what you're talking about Russ, but I've certainly never had to do anything like that.

ScaryBinary
01-30-2008, 07:59 PM
Let's try to put this stuff to rest with a little emperical evidence...:cool:

Attached is a script file containing some global scripts. Import it into a simple quest and compile it.

For the first example, assign the script "Slot_2_No_Loop" to global slot 2. Then play your quest. After a few seconds of playing, pause the game (hit the ESC key, for instance). Now go browse to your Zelda Classic folder, and find the "allegro.log" file. Open it (any text editor will do). Scroll to the bottom. You should see one number written there...if it's the first time you've tried this experiment, it should be 0; whatever it is, there should only be one number there. What does this demonstrate? It demonstrates that the Slot 2 script, without any fancy while loops or anything, only gets called once. If it were automatically called every frame, the log file would be filled to the brim with numbers.

As a second example, edit your quest so that global slot 2 is assigned the "Slot_2_While_Loop" script. Play your quest again. This time, hit the "A" button a few times (keep count of how many times you hit it), then pause the game and look at the allegro.log file again. You should see a block of numbers for each time you hit the "A" button. This demonstrates that you can get the slot 2 script to run continuously with a while loop, and that you can call the Waitframe() function in a global script. You have to call the waitframe function in this case, or your game will freeze because it will get stuck in one of the while loops.

As the third example, set up the global slot 3 script to call the Slot_3 script. Play the quest again. Walk around for awhile, whatever, but then pause the game and look at the allegro.log file. You won't see the "1234" that the script is supposed to write to the log file. Go back to your game, play around a bit, then using the menus QUIT (not exit) the game. You'll get to the Continue/Retry/Save screen. Click retry. Now look at allegro.log. You'll see the 1234. Start the experiment over, and this time just get yourself killed. Again, you'll see the 1234 in the log file. I'm not sure entirely what this implies, other than the Slot 3 script evidently gets called when Link dies or when you quit the game.

Perhaps someone can extend this little experiment and we can learn more....?


// Standard imports.
import "std.zh"

// Global variables.
int Slot_2_Count = 0;

global script Slot_2_No_Loop{

void run(){

// Just increment a counter and dump it
// to the log file.
Trace(Slot_2_Count++);
} // end run

}

global script Slot_2_While_Loop{

void run(){

// Dump something to the log file everytime
// you press the "A" button. Monitor forever.
// Note how the game doesn't freeze. Note that
// since the frames pass faster than you can
// press the button, you'll get multiple log
// entries for a single button press.
while(true){

if(Link->InputA){
while(Link->InputA){
Trace(Slot_2_Count);

// Call Waitframe() or we'll freeze the game.
Waitframe();
}

// Update the counter so we get a new value
// next time we press the A button.
Slot_2_Count++;
}

// Call Waitframe() otherwise the game will freeze.
Waitframe();
}

} // end run

}

global script Slot_3{

void run(){
// Dump something to the log file so
// we know when this script actually
// gets called.
Trace(1234);
}
}

Russ
01-30-2008, 08:43 PM
Wow, good experiment. So you can use waitframe() in global scripts? I could have sword you couldn't.

ScaryBinary
01-30-2008, 08:58 PM
Wow, good experiment. So you can use waitframe() in global scripts? I could have sword you couldn't.

It's all very weird. For instance, I just tried this script:

global script Slot_2_Wait{
void run(){
Trace(-1);
Waitframe();
Trace(-2);
}
}

What I was expecting is that the "-1" would get written to the log file, but the Waitframe() call would screw things up and cause the script to quit, so the "-2" wouldn't get written. But both showed up in the log file. :eyebrow:

The Waitframe() description in zscript.txt says:

Temporarily halts execution of the current script. This function returns at the beginning of the next frame of gameplay. Global scripts and item pickup scripts only execute for one frame, so in those scripts Waitframe() is essentially Quit().

That's why I figured the "Trace(-2)" in my script wouldn't get called, but it did. Which sort of does indicate that global slot 2 gets called every frame....?

Or perhaps, it would be more accurate to say that the script gets called only once, but it can span multiple frames?

Russ
01-30-2008, 10:04 PM
Hm. DarkDragon, what do you have to say about this?

pkmnfrk
02-02-2008, 04:11 PM
No, it makes more sense if we were to rename the function:


global script Slot_2_Wait{
void run(){
Trace(-1);
LetTheRestOfTheGameRunThenContinue();
Trace(-2);
}
}

DarkDragon
02-02-2008, 04:31 PM
Slots 1 and 3 are one-time-only, like item use scripts. Slot 1 is invoked at the very start of a new quest; this is where ZScript sticks its ~init. Slot 3 is invoked any other time gameplay resumes in a quest, whether by loading a save, dying, or quit->continue. For both of these slots, a Waitframe() is a Quit().

Slot 2 runs every frame of the game, but here "runs" means "keeps executing," not "starts over." So you can use Waitframe()s in slot 2 scripts.

beefster09
02-02-2008, 05:46 PM
That's good to know. That means you could make the "title screen" appear every time you continue and still make it go back to the place that you were just at.