Chapter 9 of 20
Chapter 7: Advanced Game Mechanics — Timers & Global Functions
As your game grows, you will want background systems that run automatically—such as real-time day/night cycles, turn counters, ambient weather updates, periodic monster spawns, or reusable global action routines.
In this chapter, you will learn how to use Real-Time Timers, Turn Ticks, Global Functions, and Game Lifecycle Hooks.
7.1 Real-Time Timers vs. Turn Ticks
RagNext provides two complementary timing systems:
flowchart TD
A["Timing Systems"] --> B["1. Turn Ticks (OnTurnTick)"]
A --> C["2. Real-Time Timers (Interval Seconds)"]
B --> B1["Fires whenever the player makes a move.<br/>(Hunger meters, poison ticks, spell durations)"]
C --> C1["Fires on a real-world clock interval.<br/>(Every 5.0 seconds: real-time combat, day/night cycles)"]
| Timing System | Trigger Condition | Example Game Use Cases |
|---|---|---|
Turn Ticks (OnTurnTick) |
Player executes an action or moves between rooms | Hunger meter decreases by 1, poison inflicts damage |
Real-Time Timers (Timer) |
Fixed real-time interval (e.g. every 5.0 seconds) |
Real-time torch burn out, ticking bomb countdowns |
7.2 Step-by-Step: Creating a Real-Time Timer
Let's create a real-time countdown timer in Studio:
Step 1: Open the Timers Workspace
- In the left navigation or top view rail, click ⚙️ Variables & Timers.
- Scroll down to the Timers section.
- Click ➕ Add Timer.
Step 2: Configure Timer Attributes
- Name: Give your timer a name (e.g.
TorchBurnTimer). - Interval Seconds: Set how often the timer fires in real seconds (e.g.
10.0seconds). - Initially Active: Check to start the timer immediately when the game loads.
Step 3: Author Timer Action Steps
- Select your timer and click 🎨 Visual Graph Editor.
- Add action steps:
Modify Integer: TorchLight -= 1Condition: TorchLight <= 0:- True:
Print Message ("Your torch flickers and dies! Darkness surrounds you.")
- True:
7.3 Global Functions (CurrentGame.Functions)
A Global Function is a central action sequence stored at the project root level.
Why Use Global Functions?
- Reuse Logic: Instead of rebuilding the exact same "Level Up" or "Calculate Combat Damage" graph on 20 different enemies, build it once under Global Functions and call it anywhere!
- Clean Organization: Keep your room and object actions lightweight by delegating heavy calculations to global functions.
- On Close Hooks: Assign a global function to an Interactive Screen's
On Close Linked Actionto handle cleanups.
Step-by-Step: Creating a Global Function
- In the left navigation rail, click 🧩 Global Functions.
- Click ➕ Add Function (e.g.
AwardQuestReward). - Click 🎨 Visual Graph Editor and add your reward steps:
Modify Integer: Gold += 100Modify Integer: PlayerXP += 500Play Sound: quest_fanfare.wavPrint Message ("Quest Complete! You received 100 Gold and 500 XP!")
7.4 Game Save & Load Hooks
RagNext includes two automatic lifecycle trigger events:
OnGameStart: Executes once when a player launches a brand-new game session. Use this to display introductory story text, initialize starting items, or set starting variables.OnGameLoad: Executes immediately after a player loads a saved game file. Use this to restore custom UI overlays, resume background music, or refresh status bars.