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.
Reset Global_variable
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
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)
I will watch and try to understand. Any questions I will ask you later. Thank you, Cyber!
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.
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.
@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?
- Edited
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.
- Edited
@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.
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
That's a good modification. Thanks.
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!