Skip to content

Helpful functions

Charles edited this page Jun 11, 2026 · 6 revisions

Introduction

Have you ever searched the Godot docs looking for a function and thinking "surely there has to be a function to..." only to not find what you're looking for? ProtoJam comes packed with a variety of commonly needed static functions to help fill that gap, including:

  • Graceful shutdown
  • Framerate independent lerps
  • Time formatting
  • and more...

Using CollectionUtils

The CollectionUtils class provides supplemental functions for Dictionary and Array.

get_or_create(dictionary, key, callable)

Performs the same function as Dictionary.get_or_add(key, default) but defers to a callable to produce the default value. This is ideal for situations where the default value requires initialization beyond its construction or should not be constructed when not required.

var _npcs: Dictionary[String, NonPlayerCharacter] = {}

func _ready() -> void:
	CollectionUtils.get_or_add(_npcs, "blacksmith", _spawn_blacksmith) # Invokes _spawn_blacksmith and stores the result
	CollectionUtils.get_or_add(_npcs, "blacksmith", _spawn_blacksmith) # Returns the previously created blacksmith


func _spawn_blacksmith() -> NonPlayerCharacter:
	var npc: BlacksmithCharacter= BlacksmithCharacter.new()
	add_child(npc)
	return npc

filterd

Returns a copy of a dictionary with only the filtered entries.

Similar to [method Array.filter]. The [param filter] receives the key and value for one entry as arguments and should return [code]true[/code] to retain the entry in the filtered dictionary or [code]false[/code] to exclude it.

var items: Dictionary[StringName, int] = {
	&"egg": 2,
	&"bullet": 20,
	&"herb": 0,
	&"key": -1,
}

# Remove egg items
var egg_filter: Callable = func(key: Variant, _value: Variant) -> bool:
	return &"egg" != key


# Prints { "bullet": 20, "herb": 0, "key": -1 }
print(filterd(items, egg_filter))
  • remove_all

Using NodeUtils

🚧 WIP

  • quit_gracefully
  • get_tree
  • connect_descendant_signal
  • focus_first_available

Using PhysicsUtils

🚧 WIP

  • is_on_floor

Using RandomUtils

🚧 WIP

  • pick_random

Using TimeUtils

🚧 WIP

  • framerate_aware_lerp_weight
  • minutes_to_millis
  • seconds_to_millis
  • minutes_to_centis
  • seconds_to_centis
  • millis_to_centis
  • millis_to_minutes
  • millis_to_seconds
  • centis_to_minutes
  • centis_to_seconds
  • format_stopwatch

Clone this wiki locally