10func roll_id(table, fallback_ids:Array = []) -> String:
15 if not row
is Dictionary:
17 var id = str(row.get(
"id",
""))
18 var weight = max(0, int(row.get(
"weight", 0)))
19 if id ==
"" or weight <= 0:
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
28 cursor += int(row.get(
"weight", 0))
30 return str(row.get(
"id",
""))
31 return str(rows.back().get(
"id",
""))
35func random_fallback_id(fallback_ids:Array) -> String:
36 if fallback_ids.is_empty():
38 return str(fallback_ids[randi() % fallback_ids.size()])