- Home /
How to save a 2D-grid of instantiated GameObjects in C#?
With "save" mean a savefile for players to load and overwrite. I made a Map with perlinNoise. That part works fine, it creates a 200x200 gridfield of diffrent terrainfields.
The player should be able to change the map in diffrent ways, like buildings, explosions and stuff like that. So I can not just generate the Map again.
For other values to save I use a Binary Formatter. But I have no idea how to save every gridcell with its information and postion.
Here is my script for generating the map:
private Dictionary<int, GameObject> tileset;
private Dictionary<int, GameObject> tileGroups;
public GameObject[] mapTiles = new GameObject[0];
public int seed;
public int mapWidth;
public int mapHeight;
private Vector2 GSL; //GenerationStartingLocation
private List<List<int>> noiseGrid = new List<List<int>>();
private List<List<GameObject>> tileGrid = new List<List<GameObject>>();
//Detail (recommended between 4 and 20)
public float magnification;
public int xOffset;
public int yOffset;
private void Start()
{
GSL.x = -mapWidth / 2;
GSL.y = -mapWidth / 2;
CreateTileset();
CreateTileGroups();
GenerateMap();
}
private void CreateTileset()
{
tileset = new Dictionary<int, GameObject>();
tileset.Add(0, mapTiles[0]);
tileset.Add(1, mapTiles[1]);
tileset.Add(2, mapTiles[2]);
tileset.Add(3, mapTiles[3]);
tileset.Add(4, mapTiles[4]);
}
private void CreateTileGroups()
{
tileGroups = new Dictionary<int, GameObject>();
foreach(KeyValuePair<int, GameObject> prefab_pair in tileset)
{
GameObject tileGroup = new GameObject(prefab_pair.Value.name);
tileGroup.transform.parent = this.transform;
tileGroup.transform.localPosition = new Vector3(0, 0, 0);
tileGroups.Add(prefab_pair.Key, tileGroup);
}
}
private void GenerateMap()
{
for (int x = 0; x < mapWidth; x++)
{
noiseGrid.Add(new List<int>());
tileGrid.Add(new List<GameObject>());
for (int y = 0; y < mapHeight; y++)
{
int tileID = GetIdUsingPerlin(x, y);
noiseGrid[x].Add(tileID);
CreateTile(tileID, x, y);
}
}
}
private int GetIdUsingPerlin(int x, int y)
{
float rawPerlin = Mathf.PerlinNoise((x - xOffset + seed) / magnification, (y - yOffset + seed) /magnification);
float clampPerlin = Mathf.Clamp(rawPerlin, 0.0f, 1.0f);
float scaledPerlin = clampPerlin * tileset.Count;
if(scaledPerlin >= mapTiles.Length)
{
scaledPerlin = mapTiles.Length - 1;
}
return Mathf.FloorToInt(scaledPerlin);
}
private void CreateTile(int tileID, int x, int y)
{
GameObject tilePrefab = tileset[tileID];
GameObject tileGroup = tileGroups[tileID];
GameObject tile = Instantiate(tilePrefab, tileGroup.transform);
tile.name = string.Format("tileX{0}_Y{1}", x, y);
tile.transform.localPosition = new Vector3(x + GSL.x, y + GSL.y, 0);
tileGrid[x].Add(tile);
}
As you can see, the script also generates Folders containing the MapTiles. So maybe there is a way to save these folders?
Answer by sacredgeometry · Dec 05, 2021 at 12:47 AM
You could store them in a list of lists? Or if you are going the array route you have the option of jagged or multidimensional arrays:
Or you can just store them in a 1D list or array if you want.
I am not entirely sure what the problem you are having is. Is it serialising it?
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/jagged-arrays https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/multidimensional-arrays
Yeah, figured that out by now... Not gonna lie. I felt kinda dumb. Guess I was thinking to complicated.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Save multiple scriptableObjects in just one binary file 2 Answers
Is this code handling fall back on previous saves upon failure the right way? 2 Answers
Can I create a tree using L-systems without having to instantiate each branches ? 2 Answers