In-Memory Aggregators
In fast-paced games, certain events occur dozens of times every minute: firing weapons, taking damage, collecting small items, or dodging obstacles. Emitting individual HTTP events for each action creates unnecessary network overhead and pollutes database tables.
The LogLudo SDK provides In-Memory Aggregators (aggregate_add and aggregate_pop) to accumulate client-side metrics in memory and dispatch the calculated statistical summary when a level, match, or wave ends.
How Aggregators Work
Section titled “How Aggregators Work”Aggregators live inside the LOGLUDO._aggregates struct. When you call LOGLUDO.aggregate_add(), the SDK dynamically creates or updates an accumulator. When you call LOGLUDO.aggregate_pop(), it computes the final metric, deletes the accumulator from memory, and returns the formatted result.
The SDK supports three distinct aggregation types:
| Type | How It Accumulates | What aggregate_pop() Returns |
Best Used For |
|---|---|---|---|
"sum" |
Increments running total (value += _value) |
Real number (total sum) | Total gold collected, bullets fired, jumps made |
"average" |
Tracks running sum and total count | Real number (sum / count) |
Average damage per hit, frame time, reaction delay |
"frequency" |
Tallies frequency map of stringified keys | Sorted array of top 10 items [{ item, count }] |
Favorite weapons, spells used, death hazards |
1. Sum Aggregation ("sum")
Section titled “1. Sum Aggregation ("sum")”Use "sum" to track cumulative quantities throughout gameplay without dispatching an event on each collection:
// Increment the running sum of apples collectedLOGLUDO.aggregate_add("apples_eaten", 1, "sum");instance_destroy();// You can add any positive or negative real numberLOGLUDO.aggregate_add("gold_earned", 150, "sum");2. Average Aggregation ("average")
Section titled “2. Average Aggregation ("average")”Use "average" to compute the mean value across multiple samples. The SDK tracks both the sum and the sample count, automatically dividing them upon pop:
function player_take_damage(_damage_amount) { hp -= _damage_amount;
// Add damage value to the running average LOGLUDO.aggregate_add("damage_per_hit", _damage_amount, "average");}When popped, if 5 hits were taken with values [10, 20, 15, 30, 25], LOGLUDO.aggregate_pop("damage_per_hit") returns 20. If zero hits occurred, it safely returns 0.
3. Frequency Aggregation ("frequency")
Section titled “3. Frequency Aggregation ("frequency")”Use "frequency" to identify player preferences, common choices, or frequent hazards. Every time a value is added, its string representation is tallied in a frequency dictionary.
function player_fire_weapon(_weapon_name) { // Tally weapon usage LOGLUDO.aggregate_add("weapons_used", _weapon_name, "frequency");}
// Example calls:player_fire_weapon("shotgun");player_fire_weapon("shotgun");player_fire_weapon("plasma_rifle");player_fire_weapon("rocket_launcher");Automatic Sorting & Top-10 Truncation
Section titled “Automatic Sorting & Top-10 Truncation”When you pop a "frequency" aggregator, the SDK:
- Iterates over all keys in the frequency map.
- Sorts items in descending order of popularity.
- Automatically truncates the array to the top 10 most frequent items.
- Returns an array of structs:
[{ "item": "shotgun", "count": 2 },{ "item": "plasma_rifle", "count": 1 },{ "item": "rocket_launcher", "count": 1 }]
Complete End-of-Level Walkthrough
Section titled “Complete End-of-Level Walkthrough”Here is how all three aggregators combine into a single, clean progression event when the player completes a level:
/// @desc Player reached the exit door
// Pop all accumulated metrics into local variables (clearing them from memory)var _total_apples = LOGLUDO.aggregate_pop("apples_eaten");var _avg_damage = LOGLUDO.aggregate_pop("damage_per_hit");var _weapon_stats = LOGLUDO.aggregate_pop("weapons_used");
// Dispatch a single consolidated progression eventLOGLUDO.send_action("progression", "level_complete", { level_id: room_get_name(room), clear_time_seconds: round(level_timer), metrics: { apples_collected: _total_apples, average_damage_taken: _avg_damage, top_weapons_used: _weapon_stats }, scenelytics: { type: "point", pointType: "dot", x: x, y: y, color: "#10b981" }});
// Transition to next levelroom_goto_next();