- Home /
Game and Level State Logic
I'm trying to find the best way to implement a general script that will temporarily store variables from the player, inventory, level specific scripts (possibly), etc. Currently, I have a script that stores all the information of my player (and is connected to my player's prefab) as well as the contents of the inventory.
I haven't thought of a way to program/collect information from each level (mainly because each level will have there own set of variables.
I created a graph of the logic that I intend to use, everything with an asterisk I'm not entirely sure how I should implement it.
So, is this a good implementation? Should I have a "Game State" class? And any specific way I can do a "Temp Save" Container? I would also like to "prepare" this information when I need to save.
Any advice would be appreciated.
Impressive. I like this kind of presentations. You can collect/keep information between levels using an object having DonDestroyOnLoad().
Thanks. I have the DontDestroyOnLoad for the other scripts, just need info on setting up one central script efficiently to contain all this.
I don't know the optimal way of implementation. I am looking also for a tutorial about this. In my simple way, I have defined few variables - keeping major information (like items found, health, score, current level id) - into as component of 'GameState' simple as possible object, non-destructible and instantiated in level 0. These vars values are changed over the levels, showing the result at the end of the game. Saving/Loading game @ level n it's different issue (I don't know if your workflow above it's about save/load game on/from disk or store/retrieve game states during of game play with no saving/reload levels). I am looking for solutions too.
Answer by swisscoder · Jan 10, 2012 at 05:59 PM
If you work with interfaces (c#), you could define an interface for your storing/loading.
Then you could replace the implementation in each level based on the specific needs.
For example:
public interface ISavegameManager {
yourSaveGameDataStructure load();
void save(yourSaveGameDataStructure savegamedata);
}
then you could go and implement a different SavegameManager for every scene:
public Level1SaveGameManager : ISavegameManager
{
public yourSaveGameDataStructure load()
{
//read data from whatever datasource you stored it..
}
public void save(yourSaveGameDataStructure savegamedata)
{
//save your data to whatever source you like
}
}
In the end you can simply exchange the SavegameManager on your "global" GameManager Script on level loading, or whenever you need to change it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613062824im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to script a Save/Load option on IPhone? 1 Answer
Saving Game Problem 1 Answer
PlayerPrefsX - Not Saving? 1 Answer
Best way to save procedural world/mesh? 0 Answers
Save and load data with Javascript... 0 Answers