Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

How to manage and process data? E.G. Questions for a Quiz Game

Discussion in 'Project Tiny' started by RobertoPelonara, Mar 16, 2019.

  1. RobertoPelonara

    RobertoPelonara

    Joined:
    Mar 2, 2015
    Posts:
    9
    Hi devs,

    I'm porting my mobile app to project tiny in order to have a version in Facebook Instant.
    My app is a Music Trivia, so my app is very data-based.
    So far, I studied the project tiny core fondumentals, and I can correctly download/stream image and audio inside my build, so I'm ready to put my information in order to have the actual application flow.
    So here it is my question:
    How can I manage the data for my game? II have an API wich will give me all the needed information, the response is formatted like this:

    Code (JavaScript):
    1. "questions": [
    2.         {
    3.             "answer": {
    4.                 "correct": {
    5.                     "song_api_id": "x",
    6.                     "id": "xxx",
    7.                     "artist": "ARTIST",
    8.                     "title": "TITLE",
    9.                     "url": "***.mp3",
    10.                     "rank": "73056",
    11.                     "value": 82.682523381337
    12.                 },
    13.                 "alternatives": {
    14.                     "12": { "song_api_id": "x",
    15.                     "id": "xxx",
    16.                     "artist": "ARTIST",
    17.                     "title": "TITLE",
    18.                     "url": "***.mp3",
    19.                     "rank": "73056",
    20.                     "value": 82.682523381337
    21.                     },
    22.                     "13": {
    23.                          "song_api_id": "x",
    24.                     "id": "xxx",
    25.                     "artist": "ARTIST",
    26.                     "title": "TITLE",
    27.                     "url": "***.mp3",
    28.                     "rank": "73056",
    29.                     "value": 82.682523381337
    30.                     },
    31.                     "14": {
    32.                          "song_api_id": "x",
    33.                     "id": "xxx",
    34.                     "artist": "ARTIST",
    35.                     "title": "TITLE",
    36.                     "url": "***.mp3",
    37.                     "rank": "73056",
    38.                     "value": 82.682523381337
    39.                     }
    40.                 }
    41.             }
    42.         },
    43.        
    44. {
    45.             "answer": {
    46.                 "correct": {
    47.                     "song_api_id": "x",
    48.                     "id": "xxx",
    49.                     "artist": "ARTIST",
    50.                     "title": "TITLE",
    51.                     "url": "***.mp3",
    52.                     "rank": "73056",
    53.                     "value": 82.682523381337
    54.                 },
    55.                 "alternatives": {
    56.                     "12": { "song_api_id": "x",
    57.                     "id": "xxx",
    58.                     "artist": "ARTIST",
    59.                     "title": "TITLE",
    60.                     "url": "***.mp3",
    61.                     "rank": "73056",
    62.                     "value": 82.682523381337
    63.                     },
    64.                     "13": {
    65.                          "song_api_id": "x",
    66.                     "id": "xxx",
    67.                     "artist": "ARTIST",
    68.                     "title": "TITLE",
    69.                     "url": "***.mp3",
    70.                     "rank": "73056",
    71.                     "value": 82.682523381337
    72.                     },
    73.                     "14": {
    74.                          "song_api_id": "x",
    75.                     "id": "xxx",
    76.                     "artist": "ARTIST",
    77.                     "title": "TITLE",
    78.                     "url": "***.mp3",
    79.                     "rank": "73056",
    80.                     "value": 82.682523381337
    81.                     }
    82.                 }
    83.             }
    84.         }
    85. ,
    86. ..
    87. ]

    How can I store all this information and access them in a proper way? I thought to make an external JS library that will manage this, but probably it's not the best option.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
  3. Deleted User

    Deleted User

    Guest

  4. RobertoPelonara

    RobertoPelonara

    Joined:
    Mar 2, 2015
    Posts:
    9
    My question was not about how parsing the response from the API. Maybe I wasn't totally clear. My apologies.

    My question was about how to store the data. For example with the Object-Oriented pattern I used to create my Question class which contains all the information of the question, and a QuestionManager as singleton, in order to process all the flow from anywhere in the app.

    Something like:
    QuestionManager.instance.GetCurrentQuestion()


    How can this can be performed in ECS?
     
  5. Zoelovezle

    Zoelovezle

    Joined:
    Aug 7, 2015
    Posts:
    54
    Storing data on disk or just as a cache for that moment?
     
  6. Adrian-Anta

    Adrian-Anta

    Joined:
    Oct 26, 2012
    Posts:
    35
    The way I do it is to create a config component. This makes its info available in all the systems, similar to the singleton pattern you added as an example.

    I'd like someone to confirm this is the proper way to do it. I came to the forums looking for an answer on the exact same thing. Thank you.
     
    Zoelovezle likes this.
  7. RobertoPelonara

    RobertoPelonara

    Joined:
    Mar 2, 2015
    Posts:
    9
    @Zoelovezle caching for the moment.

    @Adrian-Anta I thought so, but I can't add custom type to the confing. For example I need a List/Array of my questions which is a custom tupe created parsing the json above.
     
  8. Adrian-Anta

    Adrian-Anta

    Joined:
    Oct 26, 2012
    Posts:
    35
    You can create an array of your custom type by creating an array of tiny structs. Then link it to the config component.
    Create/Tiny/Struct
     
  9. Zoelovezle

    Zoelovezle

    Joined:
    Aug 7, 2015
    Posts:
    54
    One way would be as @Adrian-Anta said, the other way would be to create an empty static variable in some service class and assign the parsed json there. With a little bit of playaround, you can even create a class constructor to populate the parsing of json file and automatically create a dictionary/list and just access it whenever you want until the page is refreshed.
     
  10. RobertoPelonara

    RobertoPelonara

    Joined:
    Mar 2, 2015
    Posts:
    9
    Thank you so much for pointing me the way.
    I created a custom class that contains my question info, and a struct which with an array of question info and have all referenced in a Service in order to access the info where I need them.
     
    raymondyunity likes this.
  11. Rob_Elias

    Rob_Elias

    Joined:
    Mar 21, 2014
    Posts:
    15
    Hi! How you parse the data from the json to show parameters and add it to an entity? Thank