Can GDScript change the value of its variable permanently?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Suleymanov

Hope you are doing well. I wonder if a script can change its variable permanently. I’ll clarify my question with an example. I have a variable, let’s say:

var time = 0

Can a function change var value permanently so that the next time it’s something else but “0” even after I restart Godot?

For example, the next time I open the script I see:

var time = 378

Is there any way the script can rewrite the var value automatically and update itself?

Why do I need it you ask? I want to dump JSON.

You mean after you restart the programm? or do you mean next time when you run your function again without restarting the programm?

klaas | 2020-07-17 18:04

I mean after I restart the program. Let me put it this way, I can manually change the value of a variable, right? But can I write a function to do it automatically? So that the initial var value is always the updated value?

var = 0

if blah-blah:
var = 57

And the next time I start Godot (or a level) the variable is 57

var = 57

if blah-blah:
var = 178

And so on.

Suleymanov | 2020-07-17 18:42

:bust_in_silhouette: Reply From: klaas

im sorry, no, you have to implement a save and load mechanic to do so.

Thanks mate for the reply! I was hoping maybe there was a simpler way, but it seems like I have no choice but make friends with JSON :slight_smile:

Suleymanov | 2020-07-17 19:09

There is a simpler way, no JSON required:

hilfazer | 2020-07-18 09:34

Huge huge thanks mate!!! I’ll check it as soon as I can. Please add it as an ANSWER so that I can select it as the BEST ANSWER for those in the same boat.

Suleymanov | 2020-07-18 13:54