Search Unity

Save building that the player created in runtime.

Discussion in 'Scripting' started by AIP21, Apr 2, 2019.

  1. AIP21

    AIP21

    Joined:
    Nov 27, 2018
    Posts:
    6
    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. ;)
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,771
    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.
     
  3. AIP21

    AIP21

    Joined:
    Nov 27, 2018
    Posts:
    6
    Yeah, it is the first one, the player has the option to place a wall or a roof or a door.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,905
    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.
     
    Lethn likes this.
  5. AIP21

    AIP21

    Joined:
    Nov 27, 2018
    Posts:
    6
    I solved it using serialization.
    Thank you so much
     
  6. AIP21

    AIP21

    Joined:
    Nov 27, 2018
    Posts:
    6
    uh oh
    it doesnt work anymore, can you recommend a sccript without errors
    thanks!
     
  7. AIP21

    AIP21

    Joined:
    Nov 27, 2018
    Posts:
    6
    i need a full script to save the player created objects
     
  8. ensiferum888

    ensiferum888

    Joined:
    May 11, 2013
    Posts:
    265
    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/
     
    ProtagonistKun likes this.
  9. AIP21

    AIP21

    Joined:
    Nov 27, 2018
    Posts:
    6
    thanks a lot, sorry for being kinda annoying about this:)