Help with autoloads and global variables!

Godot Version

Godot 4

Question

I’m currently working on creating a turn based game. I have a player with a state machine and an enemy with a state machine. On the player script I have a variable called player turn, and obviously after every action or state the player has performed the player.playerturn variable is set to false so that he can no longer perform actions until the variable is changed back, I thought that I could just change the variable back to true when the enemy has completed an action but for some reason it just doesn’t change? I’ve set up the player script as a Global, but I’m wondering if I should rather set up the entire player scene as an autoload? I tried to make the enemy scene an autoload but it just gave me errors saying that the path doesn’t hold a scene even though it does, and that the node count was equal to zero even though it clearly wasn’t.

I feel like I’m just not understanding the basic concepts of autoloads and singletons. If someone could please please give any advice, even if it’s just a summary of how autoloads and singletons work or how to autoload an entire scene, I will be so grateful!

Thank you!

I’m betting you have a player Scene with script and a Global with the same script. The Global is a Node on the root with your script, different from your player inside the scene with your script.

You probably do not want a Player Global/Autoload scene, instead you can give your player scene a group and use get_tree().get_first_node_in_group("Player") to get the active player.

You can check the “remote” tab of the scene tree while your game is running to see the in-game tree structure.

Ah that makes more sense, I was getting some funny stuff happening when using the globals.

Thanks for the reply mate! I’m going to try implement this now.

Sorry, I got one more question, it’s giving me problems when I try declare a variable and set it to the get_tree.get_first_node etc. I have the player group but when I try type get_tree the auto complete suggests get_stack and not get_tree, am I missing smth here?

try get_tree() on it’s own, autocomplete is not always right. How are you using this function? Could you paste a snippet of code?

1 Like

Sure thing,
this is the code that is on the enemy in the enemy scene.
image

This is the error I get when I try to run:
image

At this point in the code the Enemy is not in the tree yet, so it cannot access get_tree(). Add the @onready attribute.

@onready var player = get_tree().get_first_node_in_group("player")

hmm that seems to have worked but only partly, I’m still not able to change the value of player turn to be true. Just also as more context, I’m trying to change the value of the player turn variable, which is inside the script on the player scene and player group, by setting it equal to true when a state has been exited on the enemy state machine INSIDE the enemy scene, not as apart of the enemy script and node itself.

This is my test scene:
image

the warnings are just because I dont have collision shapes set up because theyre not necessary for the game

This is the code for one of the states as an example of what I am trying to do:

This is totally fine, and you implemented it correctly by enemy.player.player_turn = true

Maybe your player isn’t using this value correctly or resets it?

Make sure to paste scripts instead of screenshots

okay got it thanks mate, let me show you some player scripts, I feel like I might accidentally be resetting the player turn variable

func _on_enter() -> void:
	player.position += Vector2(-32, 0)


func _on_exit() -> void:
	player.player_turn = false

thats for the player walking left state, and as you can imagine, for the player walking right state its pretty much the same

I got it, the main reason is that the states were conflicting eachother and competing. THANK YOU