How to save my random gen map

Discussion and help for Easy Save 3
Post Reply
Gangly
Posts: 1
Joined: Thu Apr 11, 2019 7:20 am

How to save my random gen map

Post by Gangly »

Hello my name is xander and i just started using easy save. I have been tryng to save my random generated 2Dmap this map exists out of alot of prefabs (floor tiles, resources etc..) these objects get instantiated like this.

Code: Select all

 public void SpawnResources()
    {
        for (int x = 0; x < roomWidth; x++)
        {
            for (int y = 0; y < roomHeight; y++)
            {
                if (grid[x, y] == gridSpace.floor &&
                   Random.value < chanceToSpawnResource)
                {
                    GameObject resource = Resources[Random.Range(0, Resources.Length)];
                    Spawn(x, y, resource);
                }
            }
        }
        GenIsDone = true;
    }

Code: Select all

 void Spawn(float x, float y, GameObject toSpawn)
    {
        //find the position to spawn
        Vector2 offset = roomSizeWorldUnits / 2.0f;
        Vector2 spawnPos = new Vector2(x, y) * worldUnitsInOneGridCell - offset;
        //spawn object

        var obj = Instantiate(toSpawn, spawnPos, Quaternion.identity);
        obj.transform.parent = GenParent.parent;
    }

So what would be the best way to save my map generation ?
Last edited by Gangly on Fri Apr 12, 2019 5:44 pm, edited 1 time in total.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How to save my random gen map

Post by Joel »

Hi there,

In your case the most efficient way would be to create three Lists: one containing all of the X positions, one containing one of the Y positions, and one containing the index of the GameObject used in your Resources array. Then you could save these three lists.

Then when loading, simply load the lists, and then iterate through them and instantiate each GameObject based on the values stored.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply