From 18924c0df3b4b5da16766259b055e2f917abb9ae Mon Sep 17 00:00:00 2001 From: Mees van der Wijk Date: Thu, 28 Aug 2025 23:13:36 +0200 Subject: [PATCH] Added paragraph about .getTime to GameLib introduction --- ModuleServerAPIGameLibIntroduction.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/ModuleServerAPIGameLibIntroduction.md b/ModuleServerAPIGameLibIntroduction.md index f2e6140..bff9894 100644 --- a/ModuleServerAPIGameLibIntroduction.md +++ b/ModuleServerAPIGameLibIntroduction.md @@ -35,6 +35,26 @@ GameLib enforces a clean division between server and client responsibilities: Think of clients as "smart displays" that beautifully present the server's authoritative game state. +## Time Synchronization + +A critical aspect of server-authoritative multiplayer is ensuring that all timing calculations are perfectly synchronized across devices. GameLib provides the [`.getTime()`](./ModuleClientAPI#gettime) function in the ModuleClientApi for this exact purpose: + +```typescript +// Get server-synchronized timestamp +const syncedTime = api.getTime(); +``` + +The `.getTime()` function returns an epoch timestamp (in milliseconds) that is synchronized with the server. This must be used for all time-dependent calculations and game timing in your client code instead of local time functions like `Date.now()`. + +Using `.getTime()` ensures that: + +- All clients operate on the exact same time reference +- Animations and transitions remain synchronized across devices +- Time-based game mechanics (countdowns, intervals, etc.) are consistent for all players +- Race conditions caused by client-side timing discrepancies are eliminated + +**Important:** Always use `.getTime()` rather than native JavaScript timing functions when building time-sensitive game mechanics to maintain perfect synchronization in your multiplayer experience. + ## Built-in Game Infrastructure GameLib provides three essential infrastructure components that work seamlessly together through standardized APIs, giving you complete flexibility in how you implement your multiplayer experience.