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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Questions and Answers database for a Unity game... what's the best approach/method?

Discussion in 'Scripting' started by Larpushka, Aug 10, 2015.

  1. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    So I have a game with a lot of trivia questions, whereas every question has 4 possible answers. I was wondering how to approach it in terms of database....I've used mySQL in the past so I'm familar with that. I also figure XML can be an option. But I wonder... what method would you use for something like that?

    Any references with tutorials would be greatly appreciated.
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Depends on a few things. Strictly speaking, XML is probably the quickest to get up-and-running, and the easiest to share and edit externally, because just about everyone is familiar with XML files and there are free programs to edit the data everywhere. That said, it's an abnormally large addition to your game for the use of just a handful of functions from System.XML. (Like 1MB I think?) It's also pretty slow to access I think because it's all strings/text data.

    JSON has most of the benefits of XML but it has a longer set up time, is more complicated, and generally you have to use a third-party tool in order to share and/or edit the data externally if you want to go that route I think. It's far smaller and faster to access, I hear, once you do actually get it running. I admit I'd never actually used it, so I'm not that familiar with it.

    (Can't find a quick link to this one, and have to go)
    The old internal-Unity method was to make a data-container class, like "QuestionData", and a script that's just "List of those data objects". You'd then slap that list script on an empty GameObject, make a prefab out of it, delete the scene object, and then add all of your data to the prefab (in the inspector) to form a little database. When/where the database is needed, you'd just make a public GameObject reference on some manager in your scene and drag that prefab in there in the inspector and use the data directly- you'd never instantiate it. The problem with this is that the database data is all held in the magic limbo of inspector-set values. This always bugged me. It's also hard (or impossible, maybe?) to edit externally, it feel hack-ish, it isn't very versatile, etc...

    The new internal-Unity method (ScriptableObjects) takes a bit more setup than the old one, but it fixes the great majority of the problems. It's still next-to-impossible to edit from outside of Unity, but assuming that you don't need to, it's definitely a strong option.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Or you could do it old school and just throw them in a csv. Quick to set up. Easy to edit. Hardly flexible.
     
    blizzy likes this.
  4. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    First off thanks a lot for the detailed feedback, Lysander, I really appreciate it.
    I'll try the ScriptableObject method, since I'm not really looking to edit outside of Unity. I'll watch the tutorial you linked.
     
  5. willrmiller

    willrmiller

    Joined:
    Dec 15, 2009
    Posts:
    9
    I wrote a blog post on this subject that might help:

    Part 1
    Part 2