How to get second from tpf in update() method?

I need to run something three times in one second.
how to run this in update method?

tpf is the time in seconds that took the last frame to render.
So basically

float time = 0;
public void update(float tpf){
    time += tpf;
    if (time > 0.33) {
         //do stuff
         time = 0;
    }
}
1 Like

Also remember, it will run a maximum of x times per second. There is no guaranteed minimum, depending on the loop speed of the game.

1 Like

Yeah good remark if you are below 30 fps this code might not work as expected

1 Like

If you need it exact three times a second you could do something like this instead:

float time = 0;
public void update(float tpf){
    time += tpf;
    while (time > 0.33) {
         //do stuff
         time -= 0.33;
    }
}
1 Like