Skip to content

Main loop scripts can use but not statically reference autoloads #111488

@mihe

Description

@mihe

Tested versions

  • Reproducible in: 4.6.dev [16a11ac]

System information

Godot v4.6.dev (16a11ac) - Fedora Linux 42 (Adams) on Wayland - X11 display driver, Multi-window, 2 monitors - Vulkan (Forward+) - dedicated AMD Radeon RX 9070 XT (RADV GFX1201) - AMD Ryzen 9 9950X3D 16-Core Processor (32 threads) - 60.39 GiB memory

Issue description

When using a custom main loop script through the application/run/main_loop_type project setting, where the script is written in GDScript, and you try to reference an autoload global statically through GDScript from something like its _initialize method, like so:

class_name MyMainLoop
extends SceneTree

func _initialize() -> void:
    MyAutoload.greet()

... you end up with parser errors saying:

Parser Error: Identifier not found: MyAutoload

However, the autoloads are in fact set up by that point, meaning you can do something like this from a main loop script that derives from SceneTree:

class_name MyMainLoop
extends SceneTree

func _initialize() -> void:
    root.get_node("MyAutoload").call("greet")

The reason for this seems to be that the main loop script is instantiated before the autoloads are set up and registered, but its _initialize method is called after the autoloads are set up.

Is this just how it has to be perhaps? Is there a good reason for why the main loop is instantiated long before its _initialize method is called?

Steps to reproduce

  1. Open the MRP
  2. Run the main scene
  3. Note the parser error
  4. Go into my_main_loop.gd
  5. Comment out MyAutoload.greet()
  6. Uncomment root.get_node("MyAutoload").call("greet")
  7. Run the main scene again
  8. Note how the code runs fine

Minimal reproduction project (MRP)

main-loop-using-autoload.zip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions