Skip to content
GodotComponents
← Back to components
The title “Marble Bag (Controlled Randomness)” over a pattern of Godot's Node icon.

Marble Bag (Controlled Randomness)

Type: General

Inspired by a youtube video from candlesan titled "How to control randomness in your game", we decided to try this out on crit system in our shooting game, and it works wonder! Of course it can be used on other randomness.

Setup

  1. Add a MarbleBag node to your scene.
  2. Assign MarbleBag into one of your node as reference, or connect it with signals.
  3. Adjust the settings to your needs
  4. Call pick_random_marble() or emit signal in your code to pick a random marble type (true or false).

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
@icon("res://addons/at-icons/node/pouch.svg")
class_name MarbleBag
extends Node
# There're only 2 types of marbles:
# true marble
# false marble
# Can comment signals if not used.
signal blind_pick
signal picked_marble(marble_type: bool)

@export_range(0.0, 100.0, 0.1, "or_greater", "hide_slider") var max_value: float = 100.0
## Adjust the marble persentage.
## The higher the number, the more true marble type in collection.
## [br][br]
## The decimal of [member percentage] set here are based on [member step] setting.
## So even if the [member percentage] is set to 10.25%, if [member step] setting is 1.0,
## the .25% will be ignored.
@export_range(0.0, 100.0, 0.01, "suffix: %")
var percentage: float = 10.0:
	set(new_value):
		percentage = new_value
		
		current_value = (percentage / 100) * max_value
## Adjust the amount of boolean generated based on decimal from [member percentage],
## if the decimal from [member percentage] is needed.
##[br][br]
## If set to 1.0, the amout of boolean in collection are same as max_value.
## [br]
## If set to 0.1, the amout of boolean in collection are multiplied by 10.
## [br]
## If set to 0.01, the amout of boolean in collection are multiplied by 100.
@export_enum("0.01", "0.1", "1.0") var step: String = "1.0"
## If true, refresh [member current_marble_collection] when
## [member percentage] value has been updated.
@export var refresh_on_update: bool = true
@export var debugging: bool = false

var current_value: float:
	set(new_value):
		current_value = new_value
		
		_generate_marbles(current_value)
var default_marble_collection: Array[bool] = []
var current_marble_collection: Array[bool] = []



func _ready() -> void:
	# Can comment signals if not used.
	blind_pick.connect(pick_random_marble)
	
	percentage = percentage
	_generate_marbles(current_value)
	_restock_marble_bag()
	
	if debugging:
		_marble_bag_debugging()


func _unhandled_input(event: InputEvent) -> void:
	if debugging and event.is_action_pressed("ui_accept"):
		pick_random_marble()


func _generate_marbles(value: float) -> void:
	var i: float = 0.0
	var ary: Array[bool] = []
	
	while i < max_value :
		if (i < value):
			ary.append(true)
		else:
			ary.append(false)
		
		i += step.to_float()
	
	default_marble_collection = ary
	if refresh_on_update:
		_restock_marble_bag()


func _restock_marble_bag() -> void:
	current_marble_collection = default_marble_collection.duplicate()

## Call this function to start rolling for marble type.
func pick_random_marble() -> bool:
	if current_marble_collection.is_empty():
		_restock_marble_bag()
	
	if !current_marble_collection.is_empty():
		var random_index: int = randi() % current_marble_collection.size()
		var picked_value = current_marble_collection.pop_at(random_index)
		# Can comment signals if not used.
		picked_marble.emit(picked_value)
		
		if debugging:
			_marble_bag_debugging()
			print("Marble Type: ", picked_value)
		return picked_value
		
	return false


func _marble_bag_debugging() -> void:
	var true_count: int = current_marble_collection.count(true)
	var false_count: int = current_marble_collection.count(false)
	var remaining_count: int = current_marble_collection.size()
	
	print("---------------------------")
	print("True Count: ", true_count)
	print("False Count: ", false_count)
	print("Remaining: ", remaining_count)

Usage example

GDScript
@export var crit_bag: MarbleBag

func shoot() -> void:
	var new_bullet: Node2D = bullet_scene.instantiate()
	muzzle.add_child(new_bullet)
	
	var is_crit: bool = crit_bag.pick_random_marble()
	
	if is_crit:
		new_bullet.damage = damage * 2.0
	else:
		new_bullet.damage = damage
	
	new_bullet.speed = speed