How to make code repeat

I want to make code repeat forever but idk how

use the update script

1 Like

sorry i was in a hurry writing this cuz i was in a game jam but i meant i wanted to make code repeat every second

To make a code repeat once every second, you’d need some type of time variable first.
In a default script, create a seconds variable and call it “project.sec” for now. Set it to zero for a start.

Default Script

project.sec = 0; // start with zero seconds

Next, in the update script, increase the seconds variable by 1.

Update Script

project.sec++; // increase by 1

After 1 second, the variable should be 12, then 24, then 48, and etc. if the fps is around 12.
In other words, multiplying the project.sec variable by the reciprocal of the FPS (project.framerate) will give you the seconds.

So every time the FPS is a factor of the project.sec variable, you know that a second passed.
In other words, anything you write in this if statement will be run once every second:

Update Script:

if(project.sec%project.framerate===0){
   // code here runs once every second
}
Note: The % symbol stands for module, it returns the remainder when project.sec is divided by the frame rate. If the remainder is zero, you know that they’re factors. It could be confusing at first, but it’s easy to understand. This is a good method to use when you’re working with time variables.

If you want something to be run once every 2 seconds, multiple the “project.framerate” variable by 2 like this:

Update Script:

if(project.sec%(project.framerate*2)===0){
   // code here runs once every 2 seconds */
}

Same for 3, 4, 5, etc. seconds.

Here’s a working example of a square moving to the right once every second:
example.wick (1.8 KB)
You can find the code in the frame.

1 Like

be careful with this code though. if you use a decimal number for the amount of seconds, there’s a chance that the counter will not hit 0 (depending on the number you pick). and since you need it to be exactly 0 to run the code, it may not run as many times as you want it to (it might not run at all).

1 Like