Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can you use ScriptableObject to create a game database without hard coding entries in the editor?

Discussion in 'Game Design' started by cmbgold, Sep 14, 2020.

  1. cmbgold

    cmbgold

    Joined:
    Oct 13, 2019
    Posts:
    4
    I have a strategy game originally written purely in Java. Now I'm converting it to work in Unity, editing the Java code to C#. There are a lot of Java class objects that include all the code they need to work, and I'm mostly using Unity for UI and things like detecting interactions. I'm trying to avoid scrapping/rewriting any more code than is necessary, and so I have MonoBehavior objects as shells containing standard C# classes which contain most of the internal mechanics of the game elements. I've got a lot of experience in Java and C#, but I've only started in Unity recently.

    In Java, I wrote a class called GameLibrary that contained the data for all the units in the game. It contained a couple of HashMaps (Dictionaries in C#) which stocked all the different units as class objects containing values such as attack, speed, etc. When the game spawned a new unit, the spawner would call the GameLibrary, which would return a copy of the requested unit. It's a simple game, and units basically consist of a handful of int values and strings. It worked well in Java and made for a simple design that made it very easy to modify units and add new ones.

    Now that I'm transferring the project to Unity, I'm trying to figure out how to integrate a version of this game library object. It's not hard to rewrite it in C# and use it the way I was previously: the spawner could simply have a reference to the GameLibrary object and request new units as before. I'd like to know if there is a better way to accomplish this, however.

    I've been looking at ScriptableObjects, which seem to be a way of creating a sort of game library or database. I've seen the tutorials on using Scriptable Objects to store values for game cards or enemies all in one place, in order to avoid multiple copies of the same basic data. I like this approach, as it seems to provide ease of use similar to a static variable, but without the mess that statics tend to create it code.

    The thing I don't like about the ScriptableObject tutorials I've seen is that they require you to create and edit objects (cards, enemies, items, etc) in the editor after you define a ScriptableObject. For example, in one of the tutorials I went through, they created a ScriptableObject to contain the attack, defense, initiative, etc, values for cards in a fantasy card game. To create their cards, they then added new cards in the Assets menu, using the editor to define the values for each card.

    In my original design, the GameLibrary reads in its units from XML files. I like the flexibility that doing this provides, as I can just edit the file to modify units or copy and paste to add new units. Having to edit hundreds of units in the editor seems tedious (and goes against my Linux programming upbringing, lol). I'd also like to have the unit values easily editable by both myself and by players, rather than only being able to edit them directly in the editor. I understand that having the values stored in an external file may make it harder to edit them while the game is running (for troubleshooting, etc), but it's not something I found to be a problem in the Java version and I'm not terribly worried about that.

    Is there a way to use ScriptableObject to create a game database without having to hard code entries using the editor? If not, is there any reason in Unity to avoid creating a master game library as a C# object and then providing other objects with references to it so they can call it at will?
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    You'll get more responses to this question in the Scripting section.

    But a few short answers:
    • You can write an editor script to read your XML file and create ScriptableObject assets from it.
    • However, if you want to allow players to edit values, you'll probably want to read the XML at runtime. In this case, there's no need for ScriptableObject assets. Make your master C# object and provide other objects references to it.
    • You could always add a feature in your game that re-reads the XML file in case you've edited it while the game is running.
     
  3. cmbgold

    cmbgold

    Joined:
    Oct 13, 2019
    Posts:
    4
    Cool. I'll swap this over to Scripting. Thanks for the quick answer!
     
    TonyLi likes this.