- Home /
Store map as int array and load/save
I have a game map stored in an int array. I want to be able to store this int array and load it to determine tiles in my 3d game level. I am not sure that storing this int array in a file is safe, as someone can just copy say Level 1 and rename it to Level 99 to complete the level.
What is a safe way to store an int array created by a Random.Range set of values for future use? I need to be able to load and save these maps.
Store another variable along with the map data that contains the map number. Check this when loading and if it's not the same as the map that should be loaded, stop the map loading.
That's a very smart idea. Store an extra value makes sense. I guess I need to encrypt the file somehow as well. Thanks for your comment.
I personally like lightly encrypting $$anonymous$$e with sine and cosine functions because they'll scramble the values a little bit
How do you currently strore(serialize) them? There is no actual 100% foolproof way to prevent "unwanted" manipulation. With a range from localy saved files with a text based format been the unsafest to an encrypted database on server side been the safest it all boils down to the answer of a simple question, does it really matter if the player cheats?
If it is a single player game with no online score board you really shouldn't care, while on the other hand if it is an online game where people compete against each other you should definitely care.
Anyway if the data are generated on edit time you ll be safe enough with the use of scriptable objects and Unity's serializer.
I don't "currently strore(serialize) them" - I have a game map stored in an int array.
int[,] map;
public int mapWidth;
public int mapHeight;
void Start () {
map = new int[mapWidth,mapHeight];
}
Like it's just inside the int array created at runtime.
Answer by Meguia · Feb 11, 2018 at 05:20 PM
Use ScriptableObjects!
By default it's serialized as a binary asset, but you can save/load it as json (or any other format) during development.
This talk is realy interesting: https://www.youtube.com/watch?v=6vmRwLYWNRo
Also you can read more information here:
https://docs.unity3d.com/Manual/class-ScriptableObject.html
https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/scriptable-objects
https://unity3d.com/learn/tutorials/topics/scripting/ability-system-scriptable-objects
Unless I'm forgetting something important, you generally don't want to use scriptable objects to load and save data during runtime -- they're intended to be used as readonly data and you usually don't edit them outside of the inspector or editor tools (though if you really wanted to use them for that, I don't think there's anything stopping you)
This unity tutorial recommends using a binary formatter for saving/loading data; the file this creates won't be 100% uneditable by someone who really wants to edit it, but A) it will prevent people from randomly stumbling into a plaintext file containing their save data, which is generally good enough, and B) if someone really wants to hack their own save you can't really stop them short of hosting the data on a remote server ins$$anonymous$$d of on the player's computer
Thanks, I ended up finding the following tutorial a bit easier to understand. https://www.youtube.com/watch?v=sWWZZByVvlU