Skip to content

Relax GDScript name lookup for tool scripts

Bernhard Liebl requested to merge github/fork/poke1024/fix-tool-globals into master

In scripts that are tool as well as runtime scripts, accessing singletons is broken, i.e.:

tool

func my_func():
	return my_singleton

will fail to even compile in the editor, as my_singleton is only defined at runtime (thus there's no way to fix this in the script).

The alternative, to use get_node("/root/my_singleton") doesn't work either if your code needs to run outside of the main scene tree (get_node will fail with "Attempt to get SceneTree while node is not in the active tree.", while the global my_singleton would be accessible there).

Allowing singletons in the editor would be one fix, but that is very complex. Extending get_node for a special case is complex too. I suggest we simply relax name lookups in tool scripts (only there): if a symbol is not defined there, it just evaluates to null (instead of a compile error). This allows users to add code to properly handle the editor case (e.g. if my_singleton: - note that if Engine.is_editor_hint() won't work due to the compile error) while keeping the non-get_node code for the runtime case that works everywhere. 

See https://github.com/godotengine/godot/pull/17511

Merge request reports