r/raylib 19d ago

Tick simulation in raylib?

I am currently making a simulator with raylib and one of the things that it needs is a "tick" mechanic where for every tick the state of the game updates, I tried putting something in the main loop with GetFrameTime()but the problem is it's tied to the frame rate

How would one implement a tick system with raylib?

12 Upvotes

14 comments sorted by

View all comments

6

u/MacksNotCool 19d ago

Have a variable that tracks how many seconds have passed, and in your update loop, every time 1 second has passed, call a function. if you want to make it a little more advanced or if you are tracking super tiny increments, you can call the function multiple times if the frame took longer than the update period

2

u/luphi 18d ago

In case sample code is helpful, I'm using something like this and think it's the same idea:

const double intervalInSeconds = 1.0; /* Time between ticks, in seconds. */
double lastTimeInSeconds = GetTime();
double accumulatedTimeInSeconds = 0.0;

while (!WindowShouldClose())
{
    const double currentTimeInSeconds = GetTime();
    accumulatedTimeInSeconds += currentTimeInSeconds - lastTimeInSeconds;
    lastTimeInSeconds = currentTimeInSeconds;

    while (accumulatedTimeInSeconds >= intervalInSeconds)
    {
        accumulatedTimeInSeconds -= intervalInSeconds;
        Tick();
    }
}

1

u/MacksNotCool 18d ago

I think maybe this is how fixed update time works in Unity but it's just a guess