• 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!

How to call a certain part of a script on certain frames?

S

shadow7692

Guest
Hello, this is my first post after weeks of using the forums as an excellent resource. I come from a Python background and there are built-in functions such as wait(), which allow me to 'pause' the execution of the script.

I have written a script (shown below), which currently causes my NPC to frantically jitter around, and from my understanding, this is because the whole section is being run every frame. I want to change it so that HorizontalMovement = choose(-1, 0, 1); and VerticalMovement = choose(-1, 0, 1); is only run every, say, 60 frames. All while the rest of the script runs normally.

I think I need to use alarms (which I don't fully understand yet), but I'm not too sure. I appreciate any guidance.

EDIT: I ended up using @kburkhart84 's suggestion of variables instead of alarms. [SOLVED]

Code:
HorizontalMovement = choose(-1, 0, 1);
VerticalMovement = choose(-1, 0, 1);

var TargetX = x + HorizontalMovement * Walkspeed;
var TargetY = y + VerticalMovement * Walkspeed;

x = TargetX
y = TargetY

if (HorizontalMovement == 0 and VerticalMovement == 0) {
    State = CharacterState.Idle;
    Y = 0;
    AnimationLength = 4;
};
else {
    State = CharacterState.Walk;
    Y = 1;
    AnimationLength = 6;
};
 
Last edited by a moderator:

Nidoking

Member
Alarms sound like the way to go. There's a guide to how they work in the Manual. You just put that part in an Alarm event and set the timer for the corresponding alarm to the number of steps you want.
 
S

shadow7692

Guest
Alarms sound like the way to go. There's a guide to how they work in the Manual. You just put that part in an Alarm event and set the timer for the corresponding alarm to the number of steps you want.
Okay so, I've added the line alarm[0] = 60; on the third line. I now don't know where to write the code in the alarm, since the script I have is in a 'script,' not an object (so I can't create an alarm event). It is, however, called from an object, so would I create the event and write it in there?
 

kburkhart84

Firehammer Games
You could add it in the alarm code on the object itself.

An alternative to using alarms is to use your own code. Its easy enough to add a variable, count it down each step, and then if that variable is 0, execute your direction change and reset that variable. It also adds an easy place to make the countdown time random in case you don't want to have it be the exact same 60 steps each time. This stuff can also be done with alarms too, but with my mention at the least the code is all in the same place and easy to manage.
 
Top