As you can see, in the output panel, blod (this is just a spelling mistake) failed to be read successfully, while damage was read successfully (both are int). I didn’t find any problem in the code.
The code
Also, I attempted to modify the Text property of the button, and the output indicated that the modification was successful. However, on the window, only “a” (the initial value) was displayed rather than something like “aaa” that I desire.
Thank you for helping me.
You problem seems to be the typo. You are trying to access and set blod of the newly instantiated button new_object (in line 36, 37 and 39).
If you do it like that, the properties in the json file have to have the same name as the ones in the class.
Thank you. After I changed it to “blood”, it indeed worked fine. But I still don’t understand. Shouldn’t the “blood” in the “save()” function and the “blood” in the JSON file be the same? They should be able to be called normally, shouldn’t they?
It doesn’t matter what you name them in the save function/the JSON file, as long as you match them correctly to the properties in the object when loading.
Your problem occurs when you try to assign it to the button instance based on the name specified in the json when loading.
Let me try to make it clearer: new_object is an instance of your button. This button has the properties blood, speed and damage.
Next you iterate over all keys in the JSON object, these are (among others) blod, damage and speed.
Finally, you try to assign based on the json key name (blod) which doesn’t exist in the new_object (if you call set with a property that does not exist, nothing happens).
for i in node_data.keys():
# i is 'blod' (from the json file)
# so here you are trying to set 'blod' on 'new_object' which only defines 'blood'
new_object.set(i, node_data[i])
I have understood.!The function set() can only be used to modify the @export variables, right? So when I tried to set blod, it didn’t work out.
Now it worked!Thank you!