Search code examples
global-variablesgodotgdscript

(GDscript) Autoload doesn't seem to be working


I'm trying to set up a basic global script with some variables in godot 4.1. I've added my script to autoload and enabled global variables. For some reason, when I run my game I inevitably get the following error:

"Invalid get index 'Health' (on base: 'Nil')" (With Health being the variable I'm trying to change)

I also have the following error in the debugger: "start: Script does not inherit from Node res://Globals.gd"

Note: I can reference Globals in the script editor and it highlights it as though it's autoloaded.

I have the exact same set up in two other games using godot 4.1, they both work fine. I'm at a complete loss here, and any help would be appreciated.

I've tried removing it and readding it into autoload (caveman level technical skills, but it was worth a shot), I've also tried looking at my older games to see if there's any differences in how the script is set up and couldn't find anything. I verified that the script is in the same file as all my other scripts, and I tried looking online to see if anyone else had this issue and already posted a solution.

Please note, I'm rather new to programming in general, so any moderately technical answers will probably just go over my head.

Thanks.


Solution

  • It sounds like you have correctly set a script as an autoload. The error "Script does not inherit from Node res://Globals.gd" indicates that your autoload script does not descend from Node.

    Autoloads are just nodes that are added to the scene tree root before, say, your main scene node. Historically, autoloads were referenced like so: get_node("/root/MyAutoload").

    To fix your script make sure it inherits from some kind of Node:

    extends Node
    
    # ... rest of your autoload script.