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

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:

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.
 
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