• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Self dependent "Var" timer in scripts

M

mtski

Guest
Hello again everybody,

Quick question,

Is it possible to make a self dependent script with a timer built in using local vars? I am attempting to draw sprites WITHOUT creating objects, and to animate them just once and disappear. For some reason I am having a lot of problems doing this.

Any help would be great,
Thanks
 

TsukaYuriko

☄️
Forum Staff
Moderator
Choose two:
1) in a script
2) keeps memory for more than one step
3) var

Local variables are discarded at the end of a script, so unless you use some other form of data storage, such as an instance variable, global variable or data structure (and then find something to store the reference of the data structure in that is not a local variable), you won't be able to keep anything in memory for more than a step.
 
M

mtski

Guest
That makes sense,

At least now I know its not just me :D. Thank you for responding :). If I knew how to mark "solved" I would.
 

Tsa05

Member
As per above, nope--

What I've usually done, in the case where there needs to be a timing script that might be called by many different objects that each need to track their own time is something simple like this:

myTimer = timing_script(myTimer);

I can set up something where myTimer starts as -1 (Create event), and then passes the time into the script; the script then passes the time back out again, so the calling instance keeps track of its own time. In this way, the script has no internal concept of time and just uses the input value instead, making it flexible and capable of working with multiple objects that have different times.

Sometimes, you'll need to return other values, and then an array is just as easy:
Create event: myTimedData = -1;

Somewhere else, like Step event:
myTimedData = do_things_involving_timers(myTimedData);
infoFromScript = myTimedData[0];

In that setup, the script does something like this:
Code:
/// @description     script do_something_involving_timers
/// @arg     parameterArray

var params = argument[0];
if(is_array(params)){
  var time = params[1]; // If parameters exist, use the second element in the array for "time"
}else{
  var time = 0; // If no params exist, start timer at zero
}
/*
*  Do stuff, compute things, modify time,
*  compute return value.
*/
return [computedValue, time];
A script like that will send in -1 at the start, which will begin the timer at zero, then the script does whatever you want and basically returns two values instead of one. The first value is whatever the script was supposed to compute. The second value is the timer that you want to keep track of. In this way, it's easy to obtain the computed value for the script, while also maintaining an array of data that will get sent back into the script on the next step, enabling it to track time.

I use this same technique for things like menus that need to draw every step while remembering which option is highlighted and also remembering how far down the menu has been scrolled. Everything is computed while drawing, then returned as an array that is used in the next running of the script to preserve the data from the previous run-through.
 

NightFrost

Member
Technically, you could write a layer script that depends on a globally declared data structure to track the sprites you need to draw. Of course, since you're not creating objects you cannot depend on their sprite animation automation but need to track that data yourself.
 

Yal

🐧 *penguin noises*
GMC Elder
Vars are gone the moment a script finishes, you need an instance variable for this. The most robust solution would be to have a special "init" script which is run in the Create event to set up all the counters and other stuff you need, then the "main" scripts (run in the Step event) can assume they're all there.
 
M

mtski

Guest
wow, thank you all for the feedback on this!

I like the "init_script" idea for establishing proper variables for such a timer and tsa I will look into your solution as well. This is something to be talked after some reset though :D. Thanks again!
 
Top