Attention | Topic was automatically imported from the old Question2Answer platform. | |
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)