The Godot Community Forums are back up and running! Please read the the announcement post for important information on what happened. Thanks!

Script Property is not changing when using Set()

I'm currently trying to set up a saving/loading system that uses a Dictionary to hold values.

Basically, it saves information like the following:

savefile = { "gamescore" : gamescore, "highscore": highscore }

gamescore and highscore are both variables in my GlobalData script that has been autoloaded..

So when the time comes to load the information, I wanted to use:

for i in savefile.get_keys(): GlobalData.set(i, savefile[i])

When I print both i and savefile[i], the values that they return are correct.

Perhaps Set() is the wrong thing to use?

I've been trying for a while now, and I'm at a complete loss.


  • AeternuxolusAeternuxolus Posts: 11Member

    I just read up on the documentation - would I need to create a function for every variable?

    I'm probably going to end up with at least 15 variables, and that might get a bit crazy.

    My original goal in using Set() was to take advantage of the "i, savefile[i]" portion, so that it could go through each variable automatically.

    Prior, I had it set up something like this:
    GlobalData.highscore = savefile[highscore]

    This worked, but I ran into a lot of problems when things needed to be changed, etc.

    Could it be due to the fact that it is an Autoload?
    I tried using get_node("/root/GlobalData").set(i, savedata[i]) too, but nothing.
    There's no errors or anything.

  • cyberealitycybereality Posts: 1,053Moderator

    I think it will work if you use Dictionaries for everything (not loose variables).

    Here is an example I made. This is the global script that is autoloaded:

    extends Node2D
    var save_data = { "gamescore" : 0, "highscore": 0 }

    And this is the script for the player that loads and saves the data:

    extends Node2D
    var player_data = {}
    func _ready():
        print("load from global script")
        print("randomize local data")
        print("save random data to global")
        print("clear local data")
        print("load from global again")
    func load_data():
        player_data = Globals.save_data
    func save_data():
        Globals.save_data = player_data;
    func display_data():
        for i in player_data:
            print(i, ": ", player_data[i])
    func randomize_data():
        for i in player_data:
            player_data[i] = int(rand_range(0, 100))
    func clear_data():
        player_data = {}

    See if that helps at all.

  • AeternuxolusAeternuxolus Posts: 11Member

    @Megalomaniak - Thank you for the help and also thank you for telling me about setters and getters!

    @cybereality - Thanks you for writing this up. I tried converting a few of my variables into Dictionary entries and they were processed through my loading function with no issues. But while I was doing this, I noticed 1 of my variables was actually processing correctly with the Dictionary entries..

    Then I saw the culprit.. It seems like all the data is saved as a float.. and I declared my variables to being int..

    The 1 variable that started working had the int declaration removed by accident when I was copy/pasting them into the Dictionary, and that's why it started working.

    GlobalData.set(i, int(savedata[i]))

    I sabotaged myself.. I always end up making every number variable an int...

    Thanks a lot for the help!

  • cyberealitycybereality Posts: 1,053Moderator

    Okay, as long as you got it working that's great.

  • MegalomaniakMegalomaniak Posts: 2,908Admin

    Sounds like you might want to make more rigorous use of static typing perhaps?

  • AeternuxolusAeternuxolus Posts: 11Member

    @Megalomaniak - Thanks again for the information - I learned that static typing was important, and I made it a point to declare all my variables.. but only when they were outside of functions - that really worked against me here.. I didn't even consider that the green line numbers actually meant something. Definitely going to have to revisit everything and start applying things before I run into another silly self-induced headache..

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file