-
Notifications
You must be signed in to change notification settings - Fork 0
Helpful functions
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...
The CollectionUtils class provides supplemental functions for Dictionary and Array.
Returns a copy of a dictionary with only the filtered entries remaining similar to Array.filter. Returning true from the filter callable will retain the key/value pair it was called with.
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(CollectionUtils.filterd(items, egg_filter))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 npcRemoves all entries from the first array that are also in the second array.
var a: Array[int] = [1, 2, 3, 4, 5]
var b: Array[int] = [2, 4]
# Prints [ 1, 3, 5 ]
print(CollectionUtils.remove_all(a, b))🚧 WIP
🚧 WIP
🚧 WIP
🚧 WIP
🚧 WIP
🚧 WIP
🚧 WIP
🚧 WIP
🚧 WIP
🚧 WIP
The TimeUtils class provides various functions for working with units of time.
Lerp functions are commonly misused by passing delta time (or some product of it) as the weight parameter. While not intuitive, this actually creates a framerate-dependent lerp which will not be correct as the framerate changes. The correct way to make a framerate independent lerp requires an exponential curve.
This function makes the process simpler by hiding those details and simply returning a weight suitable for use with lerp.
func _physics_process(delta: float) -> void:
# Wrong
# velocity = velocity.lerp(Vector3.ZERO, delta)
# Correct
var rate: float = 1.0 # Adjust this to change the damping amount
velocity = velocity.lerp(Vector3.ZERO, TimeUtils.framerate_aware_lerp_weight(rate, delta))Formats the given number of milliseconds into stopwatch format with either centisecond or millisecond precision.
var millis: int = 217195
# Prints 03:37:19
print(TimeUtils.format_stopwatch(millis))
# Prints 03:37:195
print(TimeUtils.format_stopwatch(millis, true))Converts the given number of minutes to milliseconds.
Converts the given number of seconds to milliseconds.
Converts the given number of minutes to centiseconds.
Converts the given number of seconds to centiseconds.
Converts the given number of milliseconds to centiseconds.
Converts the given number of milliseconds to minutes.
Converts the given number of milliseconds to seconds.
Converts the given number of centiseconds to minutes.
Converts the given number of centiseconds to seconds.
Contributions are always welcome! Check out the contributing guide to get started.
Made with ❤️ for humans by humans.