Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Making levels automatically using json or scriptable objects or manually for each level?

Discussion in 'Scripting' started by mahdiii, Oct 20, 2018.

  1. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    853
    I have many levels. Every level has several shapes. Every shape contains an equation(like circle, sin ,cos ,ellipse, etc) or a collection of points to draw. Objects can move on the shapes.
    Do you prefer to design each level in one scene and create them manually or use big file like json file that has information about drawing shapes in each level?
    If I use the second method, I will only need one scene and shapes are created automatically.
     
  2. Kobaltic1

    Kobaltic1

    Joined:
    Jan 22, 2015
    Posts:
    183
    I am assuming each level is really small. Since you have many levels, I would create each level at run time from the json file. I prefer procedural levels when they are not intense levels.
     
    SparrowGS likes this.
  3. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    853
    Thank you. So because every shape is different and has different formula, you use switch case to draw them?
    I can not use jsonUtility. They are not structured.
    Code (CSharp):
    1. //Level1
    2. {
    3. {type:"Circle", radius: 5, centerX:0, centerY:0},
    4. {type:"Object",vertexPointx:[],vertexPointy:[]},
    5. {type:"Ellipse",radiusX: 5, radiusY: 3, centerX:10, centerY:-3},
    6. }
    7.  
    8. //Level2
    9. {
    10. {type:"Circle", radius: 5, centerX:0, centerY:0},
    11. {type:"Object",vertexPointx:[],vertexPointy:[]},
    12. {type:"Ellipse",radiusX: 3, radiusY: 5, centerX:0, centerY:0},
    13. {type:"Object",vertexPointx:[],vertexPointy:[]},
    14. {type:"Circle", radius: 2, centerX:1, centerY:1}
    15. }
     
    Last edited: Oct 21, 2018
  4. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    I would use inheritance to handle the formula. Switch statements are a little CS101, where polymorphism is the proper way to do it.
     
  5. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    853
    Can you explain more specific according to my json?
     
  6. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    How you serialize your data is not relevant to its runtime structure. Just have your JSON have a field for the class type.
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    Couldn't it be argued that this json file is just a direct replacement of a scene?

    Unity scenes are really just 'yaml' files. Yaml is just another serialization language like Json:
    https://en.wikipedia.org/wiki/YAML

    Creating special json files is almost the same as creating unity scene files. The only difference is you can add your own custom features (like maybe support multiple scenes in a single file, or a less verbose layout, ability to download scenes from raw text files). While losing other core features (like a visual editor instead having to hand write them, a more robust layout that supports more complex object types, and that it already exists).

    What benefits are you gaining from using json? What benefits are you losing? How does this impact your end goal of creating games? (if creating games is your end goal)
     
    mahdiii and Ryiah like this.
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    It depends. For simple projects tend to create my own JSONs. For more complex projects I tend to use scenes

    Advantages of custom
    • Marginally faster iteration
    • Can be created at runtime
    • Can be edited without Unity
    • Can reuse a single scene for the static parts of the game level
    • Can be stored in a single file
    Disadvantages
    • You have to write your own loading/editing/saving system
     
    Ryiah likes this.
  9. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    853
    I thought json file can take less memory than making custom scenes but in my example json does not have enough ability to take it. I have problems about deserialization of different structures in json.
     
  10. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    853
    I demonstrate other example. Suppose that I want to design an RTS card game. The ability of cards are different.
    I have a server that keeps the card information. I want to display the cards and spawn them. So I send a request to the server and receive data like below:
    Code (CSharp):
    1. { Cards:[
    2. {type:"skeletons", attackPoint:5, speed:10, count:10},
    3. {type:"archer", attackPoint:15, speed:3, range:4},
    4. {type:"minions", attackPoint:5, speed:10, count:5},
    5. {type:"ghost", attackPoint:25, speed:8, ghostTime:2}
    6. ]
    7. }
    They can be so different and have various fields. How do you serialize that? use switch case?
     
  11. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    Technically, it's already serialized. That's what json is. It's a language you serialize data into.

    You want to 'deserialize' it.

    In which case you need some sort of json serializer/deserializer. Many of which exist out there.

    Due to what looks like might be a dynamic json layout, I'd bet you'd want a json parser to be exact so you can hand parse it and convert to data. Which means the built in JsonUtility won't really fit your needs (since it requires a pre-defined data structures in the form of class/structs).

    Json.Net has the 'JObject' class for json parsing. Which allows you to treat a json structure as a nesting table of key-value pairs.

    Here you can see an article about using JObject:
    https://weblog.west-wind.com/posts/2012/Aug/30/Using-JSONNET-for-dynamic-JSON-parsing
     
    mahdiii and Ryiah like this.
  12. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    If the layout is static... like it it always comes in the form of a object containing an array of 'Cards' where each card has those 4 properties.

    Then you CAN use JsonUtility.

    Your classes would look like this:
    Code (csharp):
    1.  
    2. [System.Serializable]
    3. public class CardPack
    4. {
    5.     public Card[] Cards;
    6. }
    7.  
    8. [System.Serializable]
    9. public class Card
    10. {
    11.     public string type;
    12.     public int attackPoint;
    13.     public int speed;
    14.     public int count;
    15. }
    16.  
    And you'd just deserialize like so:
    Code (csharp):
    1.  
    2. CardPack pack = JsonUtility.FromJson<CardPack>(theJsonYouDownloaded);
    3.  
    4. foreach(var card in pack.Cards)
    5. {
    6.     //do stuff with each card
    7. }
    8.  
     
    jacko_the_gog and Ryiah like this.
  13. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    853
    Thank you. I will see Json.Net. I know jsonUtility but in both my examples, data do not have special structure that can easily use array
     
  14. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The huge advantage of JsonUtility is that its natively supported by Unity. Which means it works the same way as inspector serialisation. That's a huge advantage. It means you only need to know the ins and outs of one system.