Godot Engine

Godot Engine

31 értékelés
How to save , load data using Godot
Készítő: CroMliFy
In this guide you will learn how to save and load data using Godot Engine
2
2
   
Díjazás
Kedvenc
Kedvenc
Törlés
Variables
Firstly lest get the variables needed for saving and loading data.
var Player_Directory = "user://Player/" var Money_File = Player_Directory + "Data.dat" var Data = 0

1 Line : The first variable is for the path that saves our data.

By " user:// " it means that user path of your computer is windows it's
" C:\Users\(Your Name)\AppData\Roaming\(The name of the Godot Project) "

The " Player/ " is a custom directory is side of the file mention before that we are going make using Godot later.(It can be anything you want)

2 Line : It's the file that we are going to save date and load data

By " Player_Directory + " it tells that the file is in the Custom folder that we make to the Program.
You can have it just " Data.dat " if you want the file in main folder.

" Data.dat " is the file name that we are going make later and save data to (You can have it to any name you want)

3 Line : It's the Float , String or the data that we are going to save.

" Data " is just a name you can have anything instead of just Data

" 0 " is the data that we are going to save and load you can have it whatever you need instead of 0



Creating the Directory
Lets see how to make that custom folder in the main folder mentioned earlier.
func Player_Dir(): var Player_Dir = Directory.new() if !Player_Dir.dir_exists(Player_Directory): Player_Dir.make_dir_recursive(Player_Directory)
1 Line : It's the function that we are going to call later in order to make the custom folder.(You can have any name to it)

2 Line : It's just for ease of access. (You can have name to the variable)

3 Line : It check if the directory is already there and if its not it will continue. In my code it checks if its not there and continue you can have it to check if its there and not to continue.

Make sure to put whatever you put as the name of the variable that contains the path instead of " Player_ Directory " in the brackets.

4 Line : just keep it as it is only change " Player_Dir " if changed the variable name in 2 line,
and like the 3 line only change " Player_Directory " in brackets if didn't use it earlier.
Saving Data
Let's see how can we save data.

func Data_File_Create(): var file__ = File.new() var error__ = file__.open_encrypted_with_pass(Money_File, File.WRITE, "321TestCode") if error__ == OK: file__.store_var(Data) file__.close()
1 Line : It Creates the function for saving data that we are going to call later.(You can have anything you want)

2 Line : Like before its just for ease of access.(You can have it whatever you want)

3 Line : It creates the file for saving.

" file__ " is the variable we created on the 2 line.(make sure to keep it the name you put for that variable)

" open_encrypted_with_pass " is for saving the data encrpted in order to make it not editable by the user.

" Money_File " is the file's directory variable we created before in the variable section.(make sure to keep it the name you put for that variable)

" File.WRITE " is to tell the program that we want to write the file.

" 321TestCode " is the encrypt pass that we have to use in order to load the data later.(You can have it whatever you want it to)

4 Line : It checks is the file is successfully created and continues.

5 Line : It stores the value you want to save in to the file.

" Data " is the variable we created in the variable section.(amake sure to keep it the name you put for that variable)

6 Line : it closes file from the program.(This is very important so dont forget to enter this line)
Loading Data
Let's see how to load the data saved in to the file.
func Load_Data_File(): var file_ = File.new() if file_.file_exists(Money_File): var error = file_.open_encrypted_with_pass(Money_File, File.READ, "321TestCode") if error == OK: var loaded_data_ = file_.get_var() Data = loaded_bal_ file_.close()

1 Line : It Creates the function for loading data that we are going to call later.(You can have anything you want)

2 Line : Like before its just for ease of access.(You can have it whatever you want)

3 Line : It checks if there is file in the " Money_File " variable directory wich we created in the variable section.(make sure to keep it the name you put for that variable)

4 Line : It open the file for loading data from it.

" open_encrypted_with_pass " is very important since we saved data encrypted so we'll have to enter this line.

" Money_File " is the file's directory variable we created before in the variable section.(make sure to keep it the name you put for that variable)

" File.WRITE " is to tell the program that we want to read the file instead of writing it.

" 321TestCode " is the pass we entered in the saving data section.(Make sure to keep it whatever you put for that before)

5 Line : It checks of the file successfully loaded and continue.

6 Line : It saves the loaded file's data into a variable.(You can only change the name of the variable other than that keep everything like it is)

7 Line : It saves the loaded data in to the main data variable that we created in the variable section so that next time it saves in will save the loaded data instead of default count we gave to it.(" loaded_data_ " van be changed to whatever you put to it in line 6, " Data " should be the nhme you put to the main data variable in the variable section)

8 Line : it closes file from the program.(This is very important so dont forget to enter this line)
4 megjegyzés
NintenHero 2024. okt. 8., 13:02 
That's the error i got from using this exact code.
CroMliFy  [készítő] 2024. okt. 8., 10:06 
@NinetenHero
I'm sorry for the late reply but what exactly do you mean by that?
NintenHero 2024. aug. 20., 9:49 
"Identifier 'File' not declared in the current scope"
DogeDev 2024. márc. 11., 7:14 
Good guide :GDNormal: