Hello.
I am making a building game and I need a script to save the buildings that the player created in runtime. Is that possible? I am a noob with scripting so sorry if this is very easy. ![]()
This is not exactly āeasyā or trivial, and actually depends on your design.
In what way are they ābuildingā things? Do they put a bunch of prefab objects you make available to them together? In that case, you should store a list of the the names of the prefabs they used, and the position/rotation/scale information for them. Maybe even the parenting information if thatās also part of it.
Are they building meshes at runtime via some editor you created that allows them to create meshes by modifying/adding verts? In that case you should save the mesh information into some container. You could use one of the many existing formats like *.obj or something, OR your own custom one.
Or is what youāre building something completely different from what I just said? In that case⦠I donāt know⦠we need to know the parameters of your situation.
ā¦
Iām willing to bet there are assets in the asset store that give you āsavingā options like you want. But theyāll come with certain limitations/parameters based on that specific assets idea of what you can edit/save. So youād need to find the specific one that meets your needs.
Yeah, it is the first one, the player has the option to place a wall or a roof or a door.
You will need to serialize all the data needed to recreate the object and save it somewhere.
The data you need serializing will depend on the object and your game, but it likely consists of things like position, rotation, scale, type of object (so you know what prefab to use to recreate it), maybe color, whatever the player can customize.
Then how you store this data depends again on your game. Do you want to save it to disk? Do you want to save it to some central server you are running? Do you want to save it to some server the player is running?
If saving to disk locally, do you want to allow easy editing by the player?
So then you can do something like saving to disk using JSON or some other text format that allows easy editing. You could save to your own custom binary format which you might even add some obfuscation of the data to make it a little trouble for the player to edit. You could send it to a web server, you could save it to a database, etc, etc, etc.
Then when you load the data you do the reverse process, where you load it from wherever youāve stored it, decode the data to forms useful in your game, instantiate the appropriate prefab, and then place the instantiated object and set any other stored parameters.
This isnāt really a scripting novice friendly process. If youāre just learning Iād probably come back to this feature later.
I solved it using serialization.
Thank you so much
uh oh
it doesnt work anymore, can you recommend a sccript without errors
thanks!
i need a full script to save the player created objects
That will not happen, serialization is itās own beast and different from project to project. Your buildings most likely inherit from MonoBehaviour so you canāt serialize that directly. I suggest you look into Data Transfer Objects and general serialization (be it binary, xml, json or your own format).
You canāt expect people to code your game for you especially not on a difficult and non uniform process such as serialization or data persistence. There are hundreds of ways to go about it and the right one will depend on your specific implementation.
Hereās a blog post I wrote a few years ago about saving your game:
http://manorlordgame.blogspot.com/
thanks a lot, sorry for being kinda annoying about this:)