RapidGameFramework
Reusable Godot managers for data-driven small games
Loading...
Searching...
No Matches
reward_manager.gd
Go to the documentation of this file.
1extends RefCounted
2
3
6
7
8
10func roll_id(table, fallback_ids:Array = []) -> String:
11 var rows := []
12 var total_weight := 0
13 if table is Array:
14 for row in table:
15 if not row is Dictionary:
16 continue
17 var id = str(row.get("id", ""))
18 var weight = max(0, int(row.get("weight", 0)))
19 if id == "" or weight <= 0:
20 continue
21 rows.append({"id": id, "weight": weight})
22 total_weight += weight
23 if rows.is_empty() or total_weight <= 0:
24 return random_fallback_id(fallback_ids)
25 var roll = randi() % total_weight
26 var cursor := 0
27 for row in rows:
28 cursor += int(row.get("weight", 0))
29 if roll < cursor:
30 return str(row.get("id", ""))
31 return str(rows.back().get("id", ""))
32
33
34
35func random_fallback_id(fallback_ids:Array) -> String:
36 if fallback_ids.is_empty():
37 return ""
38 return str(fallback_ids[randi() % fallback_ids.size()])