- Home /
Best way to store / activate levels in a game?
Currently building a game, with 4 levels, each with their own level trigger, but would like to have a system where it's easy to add more, preferable storing them in some sort of list. The process for activating them would be done by:
Creating a level trigger > giving it a name / tag(ID) > (when triggered) search through a list for the level with the matching name / tag > then activate that level.
I was thinking of creating an abstract class "Level", create a subclass of it "GetLevel", then create methods within the subclass "LevelOne", "LevelTwo" etc.
Is this the best way? And is anyone able to give a brief example of how it can be implemented?
Answer by xxmariofer · May 29, 2019 at 12:33 PM
i dont really understand why you need an abstract class and create a different class for each of the levels, what kind of methods those classes would implement?
i would go for an easier solution, a struct called levels like this (the id property is just in case you want to store it in a dictionary )
public struct Levels
{
public string id;
public int buildIndex;
public bool isActive;
public Level(string _id, int _index, bool _isActive = false)
{
isActive = _isActive;
buildIndex = _index;
id = _id;
}
}
all the levels saved in a dictionary
Dictionary<string, Level> levels = new Dictionary<string, Level>();
add the levels with your desired key
levels.Add("YourFirstLevel", new Level("YourFirstLevel", 1));
and when you want to access the index just access whenever you trigger by the key/tag
public int LevelByKey(string key)
{
return levels[key].index;
}
Never even thought of using a struct! This makes things much easier. Thanks very much.
Your answer
![](https://koobas.hobune.stream/wayback/20220612202306im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Storing complex GameObject structure 1 Answer
Level alignment script crashing after a few iterations 2 Answers
c# reference site? need help figuring out storing rigidbodies into List<> 3 Answers
How to create a level select menu and store a level list permanenece in android like the 1 Answer