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. Dismiss Notice

When to load 700 questions for a trivia game quiz

Discussion in 'Scripting' started by yohanscritch, Mar 8, 2021.

  1. yohanscritch

    yohanscritch

    Joined:
    Apr 30, 2020
    Posts:
    28
    Hi there,

    I'm making a trivia game and all my questions are in XML files: Level1.xml, level2.xml, etc. I have 2 scenes in my game:

    First one is the level selection, that tells for each level how much question is left to answer

    Second one is the actual game, that loads all questions from 1 level in particular and play them for the player.

    My problem is that my game takes a lot of time for loading everything. Currently, I've put all my xmls in prefab objects like this:

    Capture d’écran 2021-03-08 à 19.51.43.png

    This "Level" object is a container that is in the 2 scenes. As you can imagine, my level selection takes 30 seconds to load.

    So I'm trying to find a better way to load only what I need : for the game itself I could resource load only the level I need, but for the level selection, I need all the progress of the player so I guess I need all questions size of all levels to do so.

    Any best practice/ideas?

    Thanks,
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    A few thousand lines in an XML file should load very quickly, probably dealing with fractions of a second. In my game it loads and processes hundreds of thousands of lines from csv files in under 2 seconds. Have you used the Profiler during this process to see what Unity sees as taking the bulk of that 30 seconds? Does each entry in the XML kick off some additional processing of the data?

    I notice a GameObject called "MusicSource". Does it happen to reference several large music audio clips? I've personally seen large audio clips as a source of scenes taking a long time to load. If this is the actual source of the lag, set all of the audio clips to load in background, except the first music file you want playing when the scene loads (so it will start playing when expected to).

    https://docs.unity3d.com/ScriptReference/AudioClip-loadInBackground.html
     
    yohanscritch likes this.
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Why not store the player's progress separately, say in its own "Player.xml" file, which would contain the last level and question passed?

    Then you'd only need to read these two values to determine which levels/questions the player has access to and enable their corresponding UI menu options in the game.
    You could then load each level/question XML file separately for each level/question the user is currently on in the application.
     
    Joe-Censored likes this.
  4. yohanscritch

    yohanscritch

    Joined:
    Apr 30, 2020
    Posts:
    28
    Well this is what I do for the player progress. My problem is that on the level side I'm writing : You have passed X questions of Y total.

    X is easy to get with your method, I actually need the Y, which is contained in my XML file, so I need to read it in the end to give this Y value, which is the problem.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Any reason you couldn't store Y right next to X just for the sake of speed and quick access?
     
  6. yohanscritch

    yohanscritch

    Joined:
    Apr 30, 2020
    Posts:
    28
    When the game first ever loads, player hasn't start playing so X = 0. But Y = 120 (120 questions for the first level). So I should write : 0 questions out of 120.

    And this for all levels on my level selection scene...

    I have the feeling I need to hardcode all values for each level (level1 = 120 questions, level2 = 110 questions, etc.) , but tat sounds really silly if I ever want to add or remove questions, my code should calculate it.
     
  7. yohanscritch

    yohanscritch

    Joined:
    Apr 30, 2020
    Posts:
    28
    Actually it's not XML files, sorry for missing that part: it is objects that are built from the XML. One question looks like this:

    upload_2021-3-8_23-20-35.png

    I think the loading time is because of the images for each question that are texture resources.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    That would be an excellent hypothesis. You could prove it with the Window -> Analysis -> Profiler

    You might get a lot of benefit by making an editor script that scans all the XML questions and builds a quick index so you can go get precisely the next one you want.

    That way you could:

    1. only load one at a time

    2. always know how many there are