godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.52k stars 21.07k forks source link

Unable to access scenes from external script when there is an autoload class #80319

Open Soremwar opened 1 year ago

Soremwar commented 1 year ago

Godot version

4.1.1

System information

Godot v4.1.1.stable.mono - EndeavourOS #1 SMP PREEMPT_DYNAMIC Tue, 30 May 2023 13:44:01 +0000 - Vulkan (Mobile) - dedicated NVIDIA GeForce GT 1030 (nvidia; 530.41.03) - AMD Ryzen 5 3600 6-Core Processor (12 Threads)

Issue description

Kind of a niche thing, but I have a script I run with godot --headless -s my_script.gd for development purposes following the guidelines in https://docs.godotengine.org/en/stable/tutorials/editor/command_line_tutorial.html#running-a-script. This script accesses some of the classes in my project and generates some data with them (without actually running any code within them mind you)

So far, this strategy has worked great, until I added a singleton to my project, which broke the whole setup since the classes are suddenly uncapable of accessing the singleton, which leads me to believe when Godot is launched in script mode, this autoload process doesn't happen at all

Is this too much of an outlandish request? Or should there be a way to initialize autoload the scripts just the same?

Steps to reproduce

  1. Run the project
  2. Run the script with godot --headless -s script.gd
  3. You'll receive
    SCRIPT ERROR: Compile Error: Identifier not found: Singleton
          at: GDScript::reload (res://main.gd:8)

Minimal reproduction project

test.zip

monitz87 commented 6 months ago

A workaround we discovered is to load your script dynamically in the "parent script" (the one that extends SceneTree). You won't have access to static typing, but it will run at least.

Using your reproduction project as an example, in script.gd, you would have to do

#!/usr/bin/env -S godot --headless -s
extends SceneTree

func _init():
        var main = load('res://main.gd')
    print(main.SOME_DATA)
    quit()

That should work, since now the engine doesn't have to compile Main before autoloads have been registered as global names. As I mentioned, you won't have any static typing information regarding Main in your script, but it's better than nothing.