Skip to content
GodotComponents
← Back to components
The title “Loot Drop (Cascade Roll)” over a pattern of Godot's Node icon.

Loot Drop (Cascade Roll)

Type: General

Inspired by a youtube video by candlesan titled "Making an RPG with Loot? Let me save you some time.", we made this component and wanted to share it. We made it so that you can save the loot box data as resources.

Setup

  1. Add a LootDrop node to your scene.
  2. Set up Rarity Table and rarities you want. Then assign items in each of RarityClass.
  3. There are 2 method that provide same result, pick the one that you prefer in the Methods setting.
  4. Assign MarbleBag into one of your node as reference, or connect it with signals.
  5. Call begin_pull() or emit signals in your code to pull an item.

Code

Released under CC0 1.0 licence (opens in a new tab): use, change and share it in any project, without asking and without giving credit.

GDScript
class_name LootDrop
extends Node

signal start_pull
signal item_choosen(item: Resource)

## A resource for table list of rarities and its items.
@export var rarity_table: RarityTableClass
## There are 2 methods to roll for rarity
## [br][br][br]
## Method 1: It roll for each rarity from rarest to most common.
## It stop at rarity with a success roll.
## [br][br]
## Pro: Get to roll for each rarity.
## [br]
## Con: Longer code, maybe heavy performance.
## [br][br][br]
## Method 2: It only rolls a number, then subtract the number from
## [member rarity] until its equal or less than 0.
## It then check which rarity the number stop at, and pick that rarity.
## [br][br]
## Pro: Easy to understand, few code, lightweight.
## [br]
## Con: One roll decide the rarity.
## [br][br][br]
## You can comment or delete the other method if you want to
## keep the script short.
@export_enum("Method 1", "Method 2") var methods: String = "Method 1"
## You can check the percentage of each rarity,
## and check which rarity and item has been picked.
## [br][br]
## It is normal that the rarity is slighty off from actual rarity,
## so long as it dosen't off by 0.5
@export var debugging: bool = false



func _ready() -> void:
	start_pull.connect(begin_pull)
	if debugging:
		_test_rarity_rolls()


func _unhandled_input(event: InputEvent) -> void:
	if debugging and event.is_action_pressed("ui_accept"):
		#for r in range(10):
			#begin_pull()
		begin_pull()

## Call this function to start pulling
func begin_pull() -> Resource:
	var pick_rarity: RarityClass = _rarity_roll()
	var pick_item: Resource = _item_roll(pick_rarity)
	item_choosen.emit(pick_item)
	
	if debugging:
		print("Rarity: ", pick_rarity.rarity_name)
		print("Item: ", pick_item)
	
	return pick_item


func _rarity_roll() -> RarityClass:
	var return_rarity: RarityClass = null
	
	# If you do decided to delete a method, make sure
	# you replace the match below with return_rarity = 
	# method you choose.
	match methods:
		"Method 1":
			return_rarity = _method_one()
		"Method 2":
			return_rarity = _method_two()
	
	return return_rarity


func _method_one() -> RarityClass:
	if rarity_table.rarities.is_empty():
		return null
	
	var max_value: float = 100.0
	
	for r in rarity_table.rarities:
		if max_value <= 0.0:
			break
		
		var randn: float = randf_range(0.0, max_value)
		if r.rarity > 0.0 and randn < r.rarity:
			return r
		
		max_value -= r.rarity
	
	return rarity_table.rarities[rarity_table.rarities.size() - 1]


func _method_two() -> RarityClass:
	var randn: float = randf_range(0.0, 100.0)
	
	for r in rarity_table.rarities:
		randn -= r.rarity
		if randn <= 0.0:
			return r
	
	return rarity_table.rarities[rarity_table.rarities.size() - 1]


func _item_roll(rarity_class: RarityClass) -> Resource:
	if rarity_class.items.size() != 0:
		var picked_item: Resource = rarity_class.items.pick_random()
		
		return picked_item
	
	return null

# This is for debugging rarity persentage.
func _test_rarity_rolls(iterations: int = 100000) -> void:
	if rarity_table.rarities.is_empty() or iterations <= 0:
		push_warning("Cannot run rarity test: rarities array is empty or iterations <= 0.")
		return

	var counts: Dictionary = {}
	for r in rarity_table.rarities:
		counts[r] = 0

	for i in range(iterations):
		var rolled: RarityClass = _rarity_roll()
		if rolled != null:
			counts[rolled] = counts.get(rolled, 0) + 1

	print("--- Rarity Roll Test (%d rolls) ---" % iterations)
	for r in rarity_table.rarities:
		var hit_count: int = counts[r]
		var actual_pct: float = (float(hit_count) / float(iterations)) * 100.0
		var tier_name: String = r.name if "name" in r else str(r)
		print("%s | Target: %.2f%% | Actual: %.2f%% (%d)" % [
			tier_name,
			r.rarity,
			actual_pct,
			hit_count
		])


# ----- Create a new resource script and paste it -----
class_name RarityTableClass
extends Resource
## !!! GIGA NOTE !!!
##[br]
## This component use percentage, so make sure each rarity
## from [RarityClass] inside the [member rarities]
## add up to 100.
## [br][br]
## Rarity start from Lowest to Highest.
## For example:
## [br][br]
## Size 0: Legendary
## [br]
## Size 1: Epic
## [br]
## Size 2: Rare
## [br]
## Size 3: Common
@export var rarities: Array[RarityClass]
@export var table_name: String = ""


# ----- Create a new resource script and paste it -----
class_name RarityClass
extends Resource

@export var rarity_name: String = ""
@export_range(0.0, 100.0, 0.1) var rarity: float = 10.0
## Stored items here will be pick randomly.
## You can use custome resources for your needs.
@export var items: Array[Resource]

Usage example

GDScript
func open_chest() -> void:
	animation_player.play("open")
	
	var item_data: Resource = loot_drop.begin_pull()
	var loot_item: Area2D = item_node.instantiate()
	add_child(loot_item)
	
	loot_item.item_data = item_data