Skip to main content

What is the Game Loop?

The game loop is a background process that calls your on_tick() handler at a fixed rate (default 20 times per second). This is essential for:
  • 🎮 Game Physics - Move objects, detect collisions
  • ⏱️ Timers - Countdown timers, phase transitions
  • 📊 State Updates - Update scores, health, positions
  • 🎯 Auto-events - Spawn items, trigger events

How It Works

The game loop runs independently of player actions, providing smooth, continuous updates.

Basic Usage

Step 1: Start the Game Loop

Start the loop when the first player connects:

Step 2: Define on_tick Handler

Implement game logic in on_tick():

Example: Moving Objects

Move objects smoothly using delta time:

Example: Countdown Timer

Implement phase-based gameplay with timers:

Example: Aviator Game

Full implementation of a crash game with automatic rounds:

Performance Tips

Always calculate delta_time to ensure consistent behavior regardless of tick rate:
Don’t broadcast on every tick if not needed. Use a counter:
Set tick rate based on your game needs:
  • Fast-paced games: 30-60 ticks/second
  • Normal games: 20 ticks/second (default)
  • Turn-based games: 5-10 ticks/second
Stop the loop when no players are active:

Next Steps

API Reference

Complete API documentation

Examples

See more complete game examples