Introducing Dragonfly Cloud! Learn More

Question: When should you use StringName in Godot?


StringName in Godot is a datatype that is optimized for comparison operations. It's particularly useful when you need to compare strings frequently or when the string is used as a key in a Dictionary. In Godot, some common cases where StringName is preferred over the regular String type include:

  1. Signals: When you connect or emit signals, using StringName can be more efficient since signals are identified by name and these names are often compared internally.
  2. AnimationNodeStateMachine: If you are dealing with state machine nodes in animations, state names can benefit from being StringName instances for faster comparisons.
  3. Scripting API calls: When calling methods by name (e.g., through call() or emit_signal()), converting method names to StringName can speed up the lookup process.
  4. Property Set and Get: If you're frequently accessing properties by name at runtime, using StringName is more efficient for property lookups.

Here is an example of how StringName can be utilized in signal connection:

# Assuming you have a signal 'health_changed' that you want to connect dynamically var signal_name = StringName("health_changed") # Connect the signal from the emitter object to the provided callback function emitter.connect(signal_name, self, "_on_health_changed") func _on_health_changed(new_health): print("Health changed to: ", new_health)

In this code snippet, we use StringName instead of a regular String to potentially improve performance during the signal connection process, especially if connecting many signals or making frequent connections/disconnections at runtime.

To sum up, consider using StringName in instances where you expect heavy usage of string comparison or dictionary key access. Doing so will help you leverage the internal optimizations provided by Godot for these specific cases.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.