Skip to content

Instantly share code, notes, and snippets.

@WolfgangSenff
Last active October 11, 2024 15:47
Show Gist options
  • Save WolfgangSenff/f0d3f807966b244ee6e3d79db5faf1cc to your computer and use it in GitHub Desktop.
Save WolfgangSenff/f0d3f807966b244ee6e3d79db5faf1cc to your computer and use it in GitHub Desktop.
Godot safe_navigate implementation
# This can be static if you have a nice place to put it. I just have it in one spot, not static.
func safe_navigate(root: Variant, path: String) -> Variant:
var properties = path.split(".")
var current_value = root
for prop in properties:
if current_value == null:
print_debug("Navigation stopped: '", prop, "' is null.")
return null
elif not is_instance_valid(current_value):
print_debug("Navigation stopped: '", prop, "' is queued for deletion.")
return null
elif not prop in current_value:
print_debug("Navigation stopped: Property '", prop, "' does not exist on object ", current_value)
return null
current_value = current_value.get(prop)
return current_value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment