Reset Global_variable

OdinOdin Posts: 124Member

My project is using quite a few variables as Global. So is there a way when I RESET the game, all those Global_scripts are back to the original as quickly as possible, everyone? Or do I have to manually reset it? Please support me.

Comments

  • cyberealitycybereality Posts: 5,318Moderator

    So the best way would be to store the original values in a Dictionary. If you are using export variables to set the numbers, then in the _ready() function your would have to save them into the Dictionary. If it is just a couple values, then it would be easier to just use single variables, but if is more than 2 or 3, then a Dictionary is better.

    extends Node2D
    
    export var player_health = 100
    var original_player_health
    
    func _ready():
        original_player_health = player_health
        print("original_player_health = ", original_player_health)
        player_health = 33
        print("player_health = ", player_health)
        reset_variables()
        print("player_health = ", player_health)
    
    func reset_variables():
        player_health = original_player_health
    
  • cyberealitycybereality Posts: 5,318Moderator

    This is the more advanced way to do it with Dictionaries:

    extends Node2D
    
    export var player_health = 100
    export var player_name = "Sam"
    var original_values : Dictionary
    
    func _ready():
        save_original_values()
        print_current_values()
        player_health = 12
        player_name = "Alice"
        print_current_values()
        restore_original_values()
        print_current_values()
    
    func save_original_values():
        var props = get_script().get_script_property_list()
        for prop in props:
            if prop.name != "original_values":
                original_values[prop.name] = get(prop.name)
    
    func restore_original_values():
        var props = get_script().get_script_property_list()
        for prop in props:
            if prop.name != "original_values":
                set(prop.name, original_values[prop.name])
    
    func print_current_values():
        print("player_health = ", player_health)
        print("player_name = ", player_name)
    
  • OdinOdin Posts: 124Member

    I will watch and try to understand. Any questions I will ask you later. Thank you, Cyber!

  • OdinOdin Posts: 124Member

    Excuse me! Until now i still don't know how can i put the variable in the dictionary. I already know how to add key and value to dictionary. But putting variables into dictionary doesn't. For example I have 3 types of variables in Global_script:
    var hello = true
    var hello = 10
    var yes_sir
    so how do i put these 3 variables into the dictionary so that when i need it i can retrieve it as a default database? Pardon my slow understanding. Please help me. Thanks guys so much.

  • xyzxyz Posts: 940Member

    What do you mean by "putting a variable into a dictionary"? Dictionary is a collection of key-value pairs. They store values. If you need to group variables you can use custom classes.

  • OdinOdin Posts: 124Member

    @xyz said:
    What do you mean by "putting a variable into a dictionary"? Dictionary is a collection of key-value pairs. They store values. If you need to group variables you can use custom classes.

    Yeah, I mean I want to encode any variable into a dictionary key and value. And vice versa, when I need to use that variable, I will get it from that dictionary. What should I do?

  • cyberealitycybereality Posts: 5,318Moderator
    edited January 23

    My code posted above takes all the user-defined variables in the script and puts them in a Dictionary automatically. So it should work with all variables without changes.

  • xyzxyz Posts: 940Member
    edited January 23

    @Odin said:
    Yeah, I mean I want to encode any variable into a dictionary key and value. And vice versa, when I need to use that variable, I will get it from that dictionary.

    Why? Simply use variables directly.

  • TheDiavoloTheDiavolo Posts: 83Member
    export(PoolStringArray) var var_reset_list: PoolStringArray = []
    var _var_reset: Dictionary = {}
    
    func __ready():
        for i in var_reset_list:
            var property_list: Array = self.get_property_list()
            for p in property_list:
                if p["name"] == i:
                    _var_reset[i] = get(i)
    
    
    func reset(property: String = ""):
        if property == "":
            for k in _var_reset.keys():
                self.set(k, _var_reset[k])
        else:
            if _var_reset.has(property):
                self.set(property, _var_reset[property])
    

    This is an extension of @cybereality answer, this is a more advanced solution where a variable defines the variables that can be reset and their default value

  • cyberealitycybereality Posts: 5,318Moderator

    That's a good modification. Thanks.

  • OdinOdin Posts: 124Member

    Ok thank you so much everyone! I'll try to read much more to understand this. Because it is too difficult for me. But I will try to complete that. Once, thank you ,my Godot family!

Leave a Comment

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