Hi, I'm hoping someone can clear up how autoloads work with exported variables.

Example: I have a simple autoloading script with an export variable for a PackedScene, which is set in the editor.

When I setup the autoload on MyAutoLoad.cs (ie the script), the PackedScene is null.

But when I attach the script to a node and then use that in the autoload, it works fine.

It feels like both options should work, so I'm guessing there's something I don't understand here.

  • xyz replied to this.
  • "In Godot, class members can be exported. This means their value gets saved along with the resource (such as the scene) they're attached to."
    https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_exports.html

    I guess what you wrote is the way it is supposed to be.

    This does make sense to me.
    If you want a class that will be used multiple times with different default values, you create different scenes for each of them.
    To make changing those default values easier @export has been introduced.
    Else you would overwrite the default values for each of them in _ready I guess.

    If you want a class that you don't need multiple instances with different default values of, there is no need to overwrite the default values.

    That's how I understand it.

    tnz I'd always opt for not using autoloads. They are hacky and completely redundant in 4.x

      "In Godot, class members can be exported. This means their value gets saved along with the resource (such as the scene) they're attached to."
      https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_exports.html

      I guess what you wrote is the way it is supposed to be.

      This does make sense to me.
      If you want a class that will be used multiple times with different default values, you create different scenes for each of them.
      To make changing those default values easier @export has been introduced.
      Else you would overwrite the default values for each of them in _ready I guess.

      If you want a class that you don't need multiple instances with different default values of, there is no need to overwrite the default values.

      That's how I understand it.

      xyz I'd always opt for not using autoloads. They are hacky and completely redundant in 4.x

      Without an autoload, how do you ensure that initialization code is done at the beginning?

      • xyz replied to this.

        DaveTheCoder Without an autoload, how do you ensure that initialization code is done at the beginning?

        Autoload is just a node. It doesn't have any special status regarding initialization. If you need some code to run at startup you can put it into the top node of the main scene. It's exactly the same as keeping it attached to an autoloaded node.

        Autoloads and accompanying change_scene_to_*() functionality are gimmicks meant to spare beginner projects from deep diving into node/scene management. I'd never use them in a serious project. Imho they should be removed from the engine.