Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Essential · Nov 20, 2012 at 10:53 AM · javascriptmobilelevels

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jason210 · Nov 20, 2012 at 11:09 AM 0
Share

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.

avatar image Griffo · Nov 20, 2012 at 11:14 AM 0
Share

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.

avatar image nameless323 · Nov 20, 2012 at 10:59 PM 0
Share

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 =)

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Essential · Nov 21, 2012 at 09:03 AM 0
Share

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?

avatar image Essential · Nov 21, 2012 at 09:54 AM 0
Share

Okay, I used a singleton. Seems to be working alright.

avatar image
2

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);
Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nameless323 · Nov 20, 2012 at 11:35 AM 0
Share

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

avatar image Jason210 · Nov 20, 2012 at 01:34 PM 0
Share

I think X$$anonymous$$L is the way to go if you can use it. It makes it easy to edit the variables later.

avatar image kebrus · Nov 20, 2012 at 11:16 PM 0
Share

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?

avatar image nameless323 · Nov 20, 2012 at 11:50 PM 0
Share

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

avatar image Jason210 · Nov 21, 2012 at 12:00 AM 0
Share

I wonder if it's possible to do it UnityScript?

Show more comments
avatar image
0

Answer by Syameshk · Aug 26, 2016 at 10:06 AM

I think you should use scriptable object.

Unity Reference: ScriptableObject

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges