Best approach for timer in authorative match


I have a turn based game where each player gets a fixed amount of time to play his turn. I have this turn management in server side lua script.
Currently what I have planned is to use the match loop to schedule the events, like for example if current tick is 15 and player gets 5 seconds to play then check at each match loop if the tick is equal to 20 then execute the next action.
Is this a correct approach or is there something better that I am missing?
Also, is it possible that due to heavy execution a tick can be missed (like a frame on the client-side) and the tick goes from 19 directly to 21 and 20 was skipped because things were processing?

Dear @Faraz

Your aprouch is corrct
You can make variable on you match state to hold the next tick value

– give the player 5 seconds to play, note tick_rate = 15 per you example and you should set it Init_match

state.next_tick_turn_end = tick + state.tick_rate * 5

– check if player’s time to play is elapsed
if tick >=state.next_tick_turn_end then
–time is finished

I don’t think loop skips ticks so you can build your logic upon that for now

But is it possible to skip tick? or what is going to happen if your logic is extremely heavy? then i dont know the answer for that, but i think it will terminate the match

@Mohammed Thanks for your detailed answer. I will continue with this approach and for the heavy processing part, my game logics are fairly simple so I guess I shouldn’t worry about missing the tick for now, and also with a >= comparison I can be sure that the code will get executed.

@Faraz The logical clock in a match handler (ticks) are blended if the execution of the game logic within the tick time exceeds the time allowed for the match loop invocation so it’s not possible for ticks to be “skipped”.

For example if your tick rate is 10 then you have 100 milliseconds to perform the work in the current tick before the next tick runs. But if you exceed that time the next tick will still happen immediately after the current tick but it will not “overlap” or skip a tick. It will just be the next tick executed “late”.

You can also have a look at the TicTacToe match handler example which implements exactly the approach you’ve described above to calculate how much time a player has using the logical clock (ticks) for their turn:

nakama-project-template/match_handler.ts at master · heroiclabs/nakama-project-template · GitHub

Hope it helps.

1 Like