Hello. I know people have asked this in the past, but please don’t mark it as a duplicate because I need to include my project’s code as it is very specific to what I need to save.
So, I’m making a top-down survival game. Currently, the world generation consists only of trees, 1000 of them randomly placed around the map. I know how to save the player’s location and inventory, but the world saving (tree locations) has not been as easy. Does anyone have an example project or solution? I’m relatively new to develop.
I’ve tried saving the X+Y positions of each of the trees individually, but 2000 entries into the data was too much for my browser and it always triggers a SIGTRAP/SIGILL crash.
I can provide game files and code screenshots if needed.
can you post a screenshot of your load/save events?
I’m guessing you tried to save individual entries in the local data? 1000 is a lot of entries.
I did something similar before, this is how:
Add the positions in an array as soon as the objects are created, and save the array as a JSON string. This should save / load 1000 or more objects without any problem.
It’s actually not too hard to do saving, I usually use the UUID extension for making IDs.
I just attach the ID to objects with a variable. (I put the ID in a structure variable), then do the saving events. I use the “For each object” Function. I name the Files the name of the object. then I do “+ Object.Variable(Save.ID)” so all the files are separated. then when loading use the “For each Object” function again. then use the loading events, then for the filepath shouldent be too hard. Its hard to explain though, if you want a picture showing events, feel free to ask me.
I’ve tried doing this, but it just won’t save even to the variable.
Can you post a screenshot of your events?
In the load section, I think you need to convert it back from Json. This uses a scene string variable called tempJSON
Here’s a project I made.
Click the green [code] and select [download zip]
Try me. The trees are draggable with the left mouse button and you can toggle the tree from green to red with the right mouse button. It uses 4 buttons and a tree object with a green and red animation.
The events and structure can be changed to save any property or variable.
Edit: I tried this with 10,000 trees. There was a lag while it loaded and added the trees but, on my PC anyways it worked. If there is a limit then you could break up the trees and save them in groups or blocks of trees using different group names.
Thanks for pointing this out I forgot to add this part.
Alright honestly, theres easier ways to do this than using arrays
How would you do it? I’m always eager to learn. This is nice and organized into 1 variable.
Ill show a screenshot when i can. I’m unsure how to use arrays (never tried) but what are they for here though?
BTW, this uses the file system type save, if you’re looking for the Ingame storage type, you can pretty much copy this but adjust it to work, it shouldn’t be too hard. This also uses an Extension I made for ID’s you can download it here if you want, here
It will save multiple files for each object, but as long as you use a folder just for it, it should be fine. (but this wouldn’t matter at all if you’re using the Storage save system)
EDIT: Also, there’s another extension called UUID with integer ID’s but its just a number that goes up endlessly constantly. I made this extension to have an accurate ID
Arrays are very similar to structures. The difference is an array is organized with an index number instead of a string child names. They’re just a way to store values in an organized way.
Player[# or variable ]
Player (an array)
Player.Name (an array of structures)
You can mix and match structures and arrays. You can have a structure of arrays or arrays of structures or any combination.
They’re very similar. There are a few unique conditions, expressions and actions. The differences can seem subtle.
For my example, I used an array so I can append the structures to the array instead of having to create structure names.
Yes, you can use the objects ID or a counter variable for the structure name or the array index. In my example, IMO it’s slightly simpler. Not by much but anytime you can reduce the number of events, you make it that much easier to read and debug.
In my example, I created a temporary structure and then added it to an array. You could just as easily use an child name or number converted to a string as a child structure name.
Honestly, within GDevelop they’re very similar.
Thanks for sharing. I haven’t used the file system much. I like the storage. There are infinite ways of writing code. Everyone has their own preferences. It’s all good.
Yeah, i prefer file system since the files are more accessible to players.
I can see the benefit. There’s more permancy.
Yeah. Btw, is there away to combine all the instances data in 1 file? Its not very necessary but its cleaner.
I don’t have much experience with the file system. I guess you could do something like my example. Create an array or structure of all the objects positions and then convert the variable to the JSON format and save it as a string. You would then load the string and convert it back from JSON to an array or structure. Just use 1 main variable with a bunch of children.