r/godot • • 1d ago

help Need help saving data

Post image

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!

1 Upvotes

11 comments sorted by

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.

3

u/xXxRazmirxXx 1d ago

Whoops, you're correct! Thanks, so much!

2

u/ATrollNamedRod 1d ago

No problem :) good luck it sounds cool

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/BinaryBolias Godot Regular 1d ago

And if you want code to execute upon initializing the Object, you can override the _init() virtual method.

Or for Nodes, you can override _ready() to execute code when the node is added to the scene tree.

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/xXxRazmirxXx 1d ago

Thanks! It's currently a bit of a struggle xD I might have to try watching more tutorials 'cause I can't even manage to get a button to print "test". If you don't mind me asking, when I input data into a control field, such as typing the creature's name into a LineEdit node, or selecting an option from an OptionButton, how would I go about grabbing the selected/entered value? It seams like LineEditName.text isn't it? and I can't figure out what it'd be for an OptionButton

2

u/x13warzone 1d ago

Don't worry about the use case for godot lol, I've made countless games but I've also made a few non-game apps in godot

As for thr button text, I'm not 100% since I can't check right now, but on the right side of the editor if you hover your cursor over a field's name, you can see how to access it or even copy its variable name

1

u/subbubman 17h ago

OptionButton inherits from Button, which has the property text. The docs state that the text property updates automatically based on which option is currently selected.

I can't test it currently but this leads me to believe that you can access OptionButton's current selection with OptionButtonName.text. 

To add on to u/x13warzone's good advice: You can also CTRL+click on properties to open up the official documentation on them (the popup pulls from documentation so it isn't necessary)

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/