[SOLVED] But how to put the structured object variable back into the object?

How do I…

After reading a structured object variable from storage, how to put this information back into the object structured variable?

What is the expected result

The values from the structured object variable that were saved in storage should be placed back into the structured object variable.

Explain what should happen when you run the game.

The structured object variable should be repopulated with the values it had at save time.

Explain what is happening instead, what is going wrong.

Instead, even though my string comes back fine and with the correct variables, and the parsed information comes back fine as a structured scene variable, when I use the same “convert” action on the string for an object variable that I had used on the scene variable, the object structure maintains its default values. I feel like I am overlooking something so obvious. But I’m too tired to think and I missed you guys anyway.

Please include a screenshot of the full related events including both conditions and actions.
And screenshots to help showing us the issue.

Summary

Scene variables:

Object variable, all default value.

Just editing to add yeah I know I can just add them one at a time by setting each individual one to the corresponding scene var.

Just editing to add if I Clear children from variable Stats of AnimalHabitats right before the Parse JSON string VariableString(TVar) and store into variable Stats of AnimalHabitats, then Stats just shows up as an empty structure with no children.

Another edit, when I tell it to change the text of AnimalHabitats var Stats set to TVar, it correctly changes to the string.
But when I tell it to do that and then add Parse etc right behind it, the value of Stats is still the string.

Ok so since I discovered that it would change the text of the object variable correctly, even though it would not parse a text into the object variable correctly, I simply called it to store the text into the object variable, then in later beg of scene events where I have a group of events to be done For Each AnimalHabitat, I call it to parse the text I stored into it and put it back in the same variable. Everything is correct now.

Ok new update. Tonight I made a copy of the game and changed my save system to storing the structured object variables inside a structured scene variable that I have set to save the other game values, instead of their own separate save system. I chose to append them to an array in the scene structure as JSON at save time, since I will not be needing read or update their values as scene variables during runtime. I only need to get the info out in the beg of scene to create the objects and repopulate the objects structured variable with the correct information, or to clear all the children and append the new JSON at save time.

So with this new setup I did not run into the minor issue of the last set up. I was able to immediately repopulate the objects structured variable after creation. Not sure what the difference is but adding it here for comparison.

It makes me think the first issue was some error in the way I set up the other save system.

I was playing along at home. :grinning: This is what I came up with. I went a little overboard.

Edit: the objects are dragable.

doug13579/Gdevelop-Save-object-variables-to-storage (github.com)

1 Like

Lol “overboard”, I’m imagining craziness. I’m running behind so I can’t peek at it now but I can’t wait to get home and see what you did!

No, nothing too crazy. :smile:

No I don’t think it looks too crazy, it looks pretty much like mine except my save event I just write the whole object structure of each group object as an ObjectVarToJson into storage. But that’s because through the game they are assigned different values naturally and in yours you were having to give it values of something to save on the spot.

But I notice in the debugger your structure is not repopulated with values it had at save either. The objects end up in the right spot and the right object from the group, but the saved info parsed to the scene variable did that. So if you only go by item type and location you could think it’s parsing the string back into the object structure when it’s not.

But I could tell right away mine wasn’t working because I had written a piece of logic that “should” have also created the same quantity of products the AnimalHabitats had when the game was saved if the player had failed to collect them yet. Remember those products that you helped me with the tweening to shop?

So I knew right away something was wrong because by all means it should have worked, I could see no reason it wouldn’t unless the variable quantity was not showing up (which it wasn’t). And now it does work, just by storing the string after creating and parsing it back at a later event. I almost think it’s a bug though.

1 Like

Hmmm, I’ll have to look at my project. I never had a need to save object variables. As you said, I used the values from the scene variable not the object.

Gdevelop can be touchy at times. It doesn’t take much to inadvertently break/overwrite a structure or change a variable from one type to another.

1 Like

Well let me know what you think. I hesitate to say it’s a bug because I think I read something somewhere that some events might not work in the same block as the object being created, but in my vague recollection that was about creating them through external events or something.

This was the first time I’ve saved a structured object variable for the purpose of putting it back in to the object variable. I was extremely surprised it didn’t work like putting it into a scene or global would. But as long as it does work, even if it means an extra line, I’m happy.

1 Like

I did some testing. I’m still confused. I believe my issue is the way objects are deleted. I’ve seen code where people read an object property or variable right after it’s deleted and for some reason it works. I’m not sure why that affects newly created objects.

Objects don’t seem to be completely deleted until the next new event not just a subevent. When I create a new event for the delete condition then it works. If I make the rest of the code a subevent it doesn’t work.

Adding a pick all instances after the delete condition also fixes it. I wonder if it’s the picking method? Or the way instances work?

I’m going update all of my files to use a different event. So, if anyone tries my example, it should work.

That makes sense because if the group exists in storage I delete the default scene objects before creating the saved objects in their saved positions, since I don’t want duplicates and maybe the player even sold the starting game stuff by now. I guess I could give the starting objects an instance variable. Then I could repopulate the items from storage if the group exists and then I could pick all, check for the variable and delete the ones that have it, so the delete action is after rather than before. But for this project I’m keeping it the way it is because lazy.

1 Like