Variable in Autoloaded Script is Not Being Detected

Godot Version:

4.2.2 Stable Mono
OS: Arch Linux

Question:

I’ve been attempting to play an audio stream when the player enters a scene if a variable is true. I’ve declared the variable in my Global script. Here’s A mockup of the code:

extends Node2D

func _ready():
    if Global.variable:
        audio.play()

At first, I though it was an error with the audio, but I added print(“Audio Playing”) to the if statement to see if the variable detection in the auto-loaded script was the issue, and lo and behold, the audio was not the issue. I’ve triple checked to see if there are any typos or anything along the lines of that, but nothing. There aren’t any errors alluding to what might be causing the problem, so I’m stumped.

Can you show your auto load script? Did you print Global.variable’s value?

Global:

extends Node

var allow_input = true
var variable = true

I printed the value of the variable , still didn’t print anything.

it should print “true”

What about your audio playing node, the initial example. How is that set up? What does the actual _ready function look like?

That’s pretty much it for the ready function, I’ve just started working on the scene and that’s the first thing I decided to do. Nothing is changed in the _ready function except the names of the variables.

Just to clarify, you tried:

func _ready():
    if Global.variable:
        print("Audio Playing") 
        audio.play()

and it didn’t print “Audio Playing”, correct?

If so and there are no errors, then somewhere you are setting Global.variable to false.
But since you are not specifically typing your variables it might not be set to ‘false’ but something that evaluates to false.
IMO it is leagues above a good idea to always type your variables until you run into a situation wherein you need to do it otherwise.
Try adding the type to the variable:
var variable:bool = true
It might help you track down where it is or might be being changed.
(Assuming that Global.variable in actuality is a bool. Also a good idea to post actual code rather than “kinda like this” code.)

If it did print “Audio Playing” and audio.play() is the next line then the Global.variable isn’t the issue.

Is your global script loaded here?

I declared the variable as a boolean, It didn’t print. This is the first script where I’m using this variable, so it can’t be switched off by other scripts, because it isn’t in other scripts.

Yes, Auto-loaded and Enabled.

Seems like an issue with the audio player then. Can you show a picture of how its configured in the inspector

I Don’t believe that it’s an issue with the audio player because it isn’t printing “Audio Playing”. I can give you the configuration, I just need the things that you need me to check.


Here’s a screenshot of the Configuration of the Audio Player

Everything you are showing here will work.
One of the drawbacks of posting “mock up” or pseudo code is that it usually does work.
The problem invariably lies in the parts you aren’t showing.

I recommend doing a minimal reproduction project. Just enough code that we can run for ourselves and see the problem playing out.
Sometimes just doing that leads to the answer.

2 Likes