r/godot • u/xXxRazmirxXx • 1d ago
help Need help saving data
Hi all, I'm trying to follow along with the godot tutorial for saving a game here: https://docs.godotengine.org/en/stable/tutorials/io/saving_games.html#identify-persistent-objects
Problem is, I am getting an error on the very first part and I'm not exactly sure what I'm doing wrong. All it says is:
Error at (4, 1): Unexpected "for" in class body.All I really want to do is build a Creature builder calculator tool for Pathfinder 2e so that I don't have to break out the GM Book for all the rules on it(I know, not the norm for a game dev engine, I just wanted to try and make it) that I can put on my phone and mess around with but obviously being able to save the creatures built is kind of important to that xD. Any help or guidance is appreciated!
18
u/ATrollNamedRod 1d ago
The class body is anything not inside a function. It's just for defining global variables, you can't execute code in it. A for loop has to go inside a function (in this case the save function). If you scroll a little further down on the docs you'll see those 2 lines inside a function.
6
u/Suspicious-Goop Godot Student 1d ago
It needs to be in a function to work. In the tutorial it is placed in the save_game() function.
4
u/Own-Struggle-98 1d ago
for loops need to be written inside function
1
u/subbubman 1d ago
Neat use case for Godot imo! I hope you find success in doing this.
Godot expects code to be in functions, so you'll never have a for loop hanging out in your class body (aka the script file you're working with here) without it being inside a function. Javascript would let you do this but GDScript does not because Godot runs all its code only by calling the functions you write.
You'll want to put your for loop and any code you want to execute while saving into your save function.
Your variable is okay because it's data, not code. Declaring variables in the class body is fine.
1
u/Infamous-Wealth3474 1d ago
I made this save manager addon.
Make saving data very simple
https://store.godotengine.org/asset/whats-code/save-manager/