- Home /
Storing level data
Level 1
Type: Attack
Map: DeadEnd
Time: 1:00
Enemies: GFB
EnemiesHealth: 238
EnemiesStrength: 339
WaveOrder: GFGFGFFGGFGB
Weapons: PistolShotgun
Reward: 1000
Level 2
Type: Survive
Map: Junkyard
Time: 1:30
Enemies: F
EnemiesHealth: 2
EnemiesStrength: 3
WaveOrder: FFFFFFFFFFF
Weapons: PistolShotgunRifle
Reward: 500
I need to store properties for each of my levels like the example above but I'm clueless on the best way to store this data. I'm fine reading this and generating a level from it but I don't know the best way to store and manage the data, especially considering it could grow to be 50+ levels. I've read a lot on using external text files, XML files, structs, dictionaries, serializing... but I don't know what is most common, or if any of those is even the right approach for me.
Should I just put all of this information into one long JavaScript script? What's the advantage to using an external text file(s)? Would an external file open up the game to easy cheating?
I'm unfamiliar with this area of game development so any advice would be valuable to me. I'm hoping for a JavaScript option if possible, and it's for an iOS game.
I'm interested in the answer to this as well. I thought it would be simple to do. The way I would have approached it is to create an empty game object for each level and attach a script to it with these variables.
Jason's answer sounds a good one, or just set the variables at the beginning of each level, another way is to set them all on a global variable script attached to a empty game object and DontDestroyOnLoad then take it through all your levels.
DontDestroyOnLoad not the only way to store you data between levels. You can althow use static class in c# or playerPrefs. in my current game i make singleton class http://en.wikipedia.org/wiki/Singleton_pattern which discribe profiles logic. i use my class Profiles which include prifile name, score and profiles list. at start i check in "myApplicationPath"/Documents (only in this path i can create files in ios) is ihe my xml exist. if not i go to Resourses and load some xml data from this to my singleton class. if my data change (maybe you add some profile) my singleton check it and save new data in "myApplicationPath"/Documents . profit of singleton is: you may use constructor, you may use inheritance, game logic ensures thet only one instance exist and you dont see annoing game oblect in your editor =)
Answer by IT_Criminal · Nov 20, 2012 at 02:31 PM
it's a rough answer but in C# you can do...
//at the top of the script
using System.Collections.Generic;
//put this in the monobehaviour
public Level CurrentLevel;
public List<Level> LevelList = new List<Level>();
void Start()
{
DontDestroyOnLoad(this);
}
//put this outside the monobehaviour
//level data structure
[System.Serializable]
public class Level
{
public MapType type
public string name;
public float time;
public string enemies;
public int enemies_health;
public int enemies_strenght;
public string WaveOrder;
public List<Weapon> WeaponList;
public int reward;
}
//map types
[System.Serializable]
public enum MapType
{
Attack,
Defence,
Other
}
now assign levels directly inside the unity editor then you can set the current level with
CurrentLevel = LevelList[0]; // zero is the number of the level
load the level with Application.LoadLevel(CurrentLevel.name);
and acces data about the current level with the Currentlevel variable
I like this approach. Using classes would also allow me to organize and edit the level parameters in the inspector with nesting which is neat and handy. However, if I create this as a static class and assign it to a gameObject, none of the variables show up in the inspector as they're now all static. D'oh. However I need the class to be static as I'll need to reference it in other scenes. Any ideas?
Answer by nameless323 · Nov 20, 2012 at 11:22 AM
i think you sould use xml to store this information. in C# XMLDocument will be perfect for this. in java i dont know =( if you will use this informantion will not change until the game you should create your xml in Assets/Resouses folder, and load it use
XmlDocument doc = new XmlDocument();
TextAsset ta = (TextAsset) Resources.Load("myXML");
doc.LoadXml(ta.text);
and you can use both types
<level>
<type> Attack </type>
<map> DeadEnd </map>
</level>
<level>
....
</level>
and
<level type="Attack" map="DeadEnd"/>
<level ... />
and variations try to use all and find what type more convenient for you
I think X$$anonymous$$L is the way to go if you can use it. It makes it easy to edit the variables later.
i'm very interested in this answer too, from this example it sounds simple but i'm not sure why is this easier or even better than creating a structure in a c# script and edit it inside of it
also, why is it easier to edit later? are there tools to edit xml files easily?
main tool to edit xml easly is the any text editor =) you need xml to store you data between the games. for exampe: you have some intitial settings. you store them in xml. after that you can easly change you settings even not launch unity. in unity you can create you class Level { string Name;} and class Levels { List levels;} when game launch you parse xml into your Levels class. them maybe some changes occurs during the game which reflect on some on your lelvel. Levels store in during the game. but you decided to quit the game and continue later. you parse your Levels class into X$$anonymous$$L and quit the game. when you launch new game, you parse X$$anonymous$$L and you last game changes transferred into you current game. now i used it for profiles/score/achievement systems
Your answer
Follow this Question
Related Questions
How to detect swipe on an Android device using javascript? 0 Answers
Calling an Android plugin from Javascript? 0 Answers
GUI.DrawTexture wrong on mobile build 0 Answers
Displaying Current Level 1 Answer
Multitouch with two Different Scripts 0 Answers