Global Variables : Some work, some don't ? What am I missing ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By antourer

Hello,
Recently I have been working on a little game project.
Everything has been going well and it’s starting to look like an actual game.

It’s a top down game in the dark where you make your way through the different levels with just a small source of light that you carry.
Recently my brother suggested that I add doors which need a key to be open. I thought this would be very easy to implement and add interest to some levels.

Here is the problem :
I wanted to keep track of the number of keys carried by the player in my “GameManager.gd” (what I like to think of as my global variables)
I am already using this file to keep track of the current level and things like that :

# Level tracking
var current_level : int = 1
var lvl_to_load : String = ""
var last_level : bool = false
var nbr_levels : int = 3

All of these variables are accessed and get updated from other scripts and it works with no problems whatsoever.
So I thought : “I just need to add a variable to keep track of the player keys and update it from other script and I’m done” (spoiler : it didn’t work)
I added my variable right under the first four :

# Level tracking
var current_level : int = 1
var lvl_to_load : String = ""
var last_level : bool = false
var nbr_levels : int = 3

# Player related
var player_keys : int = 0

But this time for some reason when I try to update this variable Godot acts like the variable doesn’t exist.

I try to update it from the key script :

extends Node2D

func _on_PickUpArea_body_entered(body):
	if "Player" in body.name:
		print("Key collected")
		GameManager.player_keys += 1
		queue_free()

In this very same script if I change “player_keys” with “current_level” godot is happy to update the variables without complaining but when I put “player_keys” I get this error :
Invalid get index ‘player_keys’ (on base ‘Node (GameManager.gd)’).

So I have tried adding simple getter and setter method to GameManager.gd :

func get_player_key():
	return player_keys

func set_player_key(value : int):
	player_keys = value

func player_collect_key():
	player_keys += 1

But when I call them I get this error :
Invalid call. Nonexistent function ‘player_collect_key’ in base ‘Node (GameManager.gd)’.

I really can’t get my head around it especially since current_level is an int as well and the two variables are right one below the other in the same script
One works fine the other doesn’t.

It almost feels like godot saved a version of GameManager.gd and only recognize variables that where in the file at a specific date and refuses to update to the new content that is added to it.

Please help me understand the issue here. I’m lost.
(Ps : maybe it’s not the best way to implement the key system anyway but at this point I am really more interested in understanding why some GameManager variables can be call from outside the script and some can’t when they have been declared the same way)

:bust_in_silhouette: Reply From: Inces

It totally should work. It really sounds like editor doesn’t use updated script, or autoload script is not the one You are updating, or some typos.

What happens if game menager would print all own variables in ready() ? Are keys printed there ? Also try to change this keys var form ready in game menager. ( If You want setter to work, it has to be self.player_keys )

Hello sorry for taking so long to reply I was quite busy this week. Thanks for taking the time to respond.

Really looks like it isn’t updating for some reason, you are right.
It’s not a typo I have checked many many times.

I have tried to print the variables in the ready function, as you suggested and it doesn’t work so this seem to confirm that the file is not updating.

I’m pretty sure I can easily get rid of the problem by making an other script similar to GameManager.gd, call it something like PlayerVariables.gd and I’m confident it would work.

It’s probably the best option and I will give it a try soon but just out of curiosity I would still really want to understand why I’m encountering this issue.

antourer | 2021-03-17 17:14