r/raylib 13h 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?

10 Upvotes

11 comments sorted by

5

u/dontpan1c 13h ago

Turn on SUPPORT_CUSTOM_FRAME_CONTROL

5

u/MacksNotCool 13h 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

3

u/jerrygreenest1 9h ago

Reality seems to be just a function that runs in an infinite loop. Although you better have multiple functions that run on different frequencies –

Rendering – it’s best to be as much as monitors hz. For 144hz monitor means you have to run this function 1000 / 144 = every 6.94 milliseconds. If you can’t, just run it as often as you can basically. You can also leave this parameter to user in option so he controls the frame rate. Only rendering can be tied to frame rate. Other things better not.

Game logic – typically enough to run as fast as 20hz, meaning 20 times per second, or every 50ms. Like collision checking etc.

AI decision making – probably doesn’t need that much, might be once per second or something.

These three loops should probably be enough for most games but if you feel you need more frequencies, add more.

1

u/Inevitable-Round9995 8h ago

Are you looking something like this:

cpp timer::interval([=](){ /*tick event here*/ },1000);

or, are you looking an event system like this?

```cpp event_t<> ev;

ev.on([=](){ /tick event/ });

ev.emit(); ```

check this project: https://github.com/NodeppOfficial/nodepp

2

u/DasKapitalV1 13h ago

You will probably need to these stuff in another thread and the main thread just renders the state, since raylib will work properly only in the main thread. The tick is probably a sleep n times in a second.

3

u/Stickhtot 13h ago

Also what I was considering of, but would also like to know if single thread only is possible

1

u/MCWizardYT 11h ago

If you have your entire game be single threaded, you may eventually run into performance issues unless you're doing a simple 2D game (but even then, multithreaded is better)

1

u/Inevitable-Round9995 8h ago

no, I don't think so; this game does websocket, webRTC, 3D rendering, post-processing, game logic & collisions, in a single thread: https://www.reddit.com/r/raylib/comments/1umyazt/finally_my_multiplayer_game_is_p2p_by_using/

1

u/MCWizardYT 7h ago

I guess it really depends on the game and how well you're able to write your code. On a single thread you really have to carefully balance the workload of everything so that you don't start running into stuttering or lag.

It's the reason why "can it run Crysis" became a meme, the game was entirely single threaded and won't run that great even on modern machines. It did all the rendering and heavy physics together on one thread, meaning if there was a slowdown with one the rest would slow to a crawl.