0 votes

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 "playerkeys" with "currentlevel" godot is happy to update the variables without complaining but when I put "playerkeys" 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 'playercollectkey' 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)

Godot version Godot_3.2.3
in Engine by (12 points)

1 Answer

+1 vote

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 )

by (1,592 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.