Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Logic of Saving Game Object Locations

Discussion in 'Scripting' started by xerotechnologiesnetllc, Nov 26, 2014.

  1. xerotechnologiesnetllc

    xerotechnologiesnetllc

    Joined:
    Jul 1, 2014
    Posts:
    49
    Calling all Logic Masters!

    I'm working on a game and about to start building the save game mechanics. For now I'm only going to work about saving locally to the playerprefs file. The game is a web based game and nothing major, but it has a few mechanics that are making me struggle when trying to decide the logic to use to save the game information properly.

    To give a layout of the game real fast...
    The player starts with 5 cubes, clicks a cube, selects a building to build and it adds it to that cube.
    The player can upgrade at the HQ to add more rows of cubes always increasing by 2 as the rows go back. Currently I have no maximum number of rows set.
    The cubes are instantiated from a prefab.

    How could I save how many cubes are created and worse yet, how can I save what building they chose to build?

    Currently, all buildings load when the cube is instantiated, just not enabled. I did this so that the player can always sell the building on that cube and then build another and the building is already loaded into memory.

    Thoughts?

    The rest of the values I'll be saving, money, population, etc. I have down without any issue. The only thing hanging me up is the cubes and what the player is building on said cubes.

    I appreciate any insight available.

    Below is a screenshot of the game. Yes I know LOL@theart. I have no artist at the moment and can't pay one so...for now, it's box building art. LOL!

    Thank you for any time spent in assisting me with this.
     

    Attached Files:

  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    You could serialize the entire scene when saving, and then deserialize it when loading. But this seems a bit heavy-handed. How about this: When you save, record only the information needed to reconstruct the map. When you load, reconstruct the map from this information. If you can record the information into a single serializable class, you can serialize it to an XML-format string and store that single string anywhere (such as PlayerPrefs). Serializing to XML is just a couple lines of code (literally like 1-2 lines). There's a tutorial here.

    Here's a rough sketch of some C# code for the map:
    Code (csharp):
    1. [Serializable]
    2. public class Map {
    3.     public List<Row> rows;
    4. }
    5.  
    6. [Serializable]
    7. public class Row {
    8.     public List<Cube> cubes;
    9. }
    10.  
    11. [Serializable]
    12. public class Cube {
    13.     public string building; // Name of child GameObject for a building.
    14. }
    So a Map consists of a set of rows. Each Row has cubes. Each Cube has a building on it.

    You could either maintain an instance of Map in parallel to the actual map of GameObjects in the scene, or you could traverse the scene and construct it when you're saving.

    When you load, traverse the map to instantiate your prefabs:
    Code (csharp):
    1. public void InstantiateMap(Map map) {
    2.     for (int r = 0; r < map.rows.Count; r++) {
    3.         for (int c = 0; c < map.rows[r].cubes.Count; c++) {
    4.             InstantiateCube(r, c, map.rows[r].cubes[c]);
    5.     }
    6. }
    7.  
    8. public void InstantiateCube(int r, int c, Cube cube) {
    9.     var go = Instantiate(myCubePrefab);
    10.     // Position cube at (r,c).
    11.     // Might also parent to a master GameObject to track them all.
    12.     go.transform.position = new Vector3(r, 0, c);
    13.     // Activate the right building child GameObject:
    14.     go.Find(building).SetActive(true);
    15. }
     
  3. xerotechnologiesnetllc

    xerotechnologiesnetllc

    Joined:
    Jul 1, 2014
    Posts:
    49
    Holy crap....*watches it fly over my head so high*...

    I ... think I understand, but I will need to read over this a few times and put it into test practice before I get it I think.
    But...This is amazingly awesome. And I couldn't be more thankful for you taking the time to post this.

    Thank you for posting this and if I can get it working in the coming days I'll try to remember to post a video of it.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can use ArrayPrefs2 to load/save Vector3 arrays using PlayerPrefs.

    --Eric
     
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    Array2Prefs works, too, and might be simpler. The advantage of putting it in one XML string is that you can eventually save it somewhere else, such as a local disk file, to support multiple saved games.