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

Question Scriptable Object and Data Base

Discussion in 'Scripting' started by QXTProductions, Mar 31, 2023.

  1. QXTProductions

    QXTProductions

    Joined:
    Mar 20, 2022
    Posts:
    12
    Hi all,

    since a few days ago I have started creating a inventory, with scriptable objects.
    I have found a way to add the scriptable objects to the inventory list, to increase the quantity for said item.
    But how do I store the quantity in a data base and save that data when a player closes the game.

    I copied a save script from the last project I worked on so I am set with the saving script, but I am stuck on how to set the quantity of the object to a database, help is needed and appreciated!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    As I already pointed out to you in your last post, inventories and load/save games are all extremely well-traveled ground. There's basically NOTHING new here to invent.

    If you want to do it, you will need to ramp up your experience with save games and with inventory systems in general.

    Fortunately there are thousands of examples of savegame AND inventory systems already out there for you to study.

    What I posted to you before: (see below for additional notes on loading / saving state)

    These things (inventory, shop systems, character customization, crafting, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.

    They contain elements of:

    - a database of items that you may possibly possess / equip
    - a database of the items that you actually possess / equip currently
    - perhaps another database of your "storage" area at home base?
    - persistence of this information to storage between game runs
    - presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
    - interaction with items in the inventory or on the character or in the home base storage area
    - interaction with the world to get items in and out
    - dependence on asset definition (images, etc.) for presentation

    Just the design choices of an inventory system can have a lot of complicating confounding issues, such as:

    - can you have multiple items? Is there a limit?
    - if there is an item limit, what is it? Total count? Weight? Size? Something else?
    - are those items shown individually or do they stack?
    - are coins / gems stacked but other stuff isn't stacked?
    - do items have detailed data shown (durability, rarity, damage, etc.)?
    - can users combine items to make new items? How? Limits? Results? Messages of success/failure?
    - can users substantially modify items with other things like spells, gems, sockets, etc.?
    - does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
    - etc.

    Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

    Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

    Breaking down a large problem such as inventory:

    https://forum.unity.com/threads/weapon-inventory-and-how-to-script-weapons.1046236/#post-6769558

    If you want to see most of the steps involved, make a "micro inventory" in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).

    Everything you learn doing that "micro inventory" of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.

    Breaking down large problems in general:

    https://forum.unity.com/threads/opt...n-an-asteroid-belt-game.1395319/#post-8781697

    Load/Save steps:

    https://forum.unity.com/threads/save-system-questions.930366/#post-6087384

    An excellent discussion of loading/saving in Unity3D by Xarbrough:

    https://forum.unity.com/threads/save-system.1232301/#post-7872586

    Loading/Saving ScriptableObjects by a proxy identifier such as name:

    https://forum.unity.com/threads/use...lds-in-editor-and-build.1327059/#post-8394573

    When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls
    new
    to make one, it cannot make the native engine portion of the object.

    Instead you must first create the MonoBehaviour using AddComponent<T>() on a GameObject instance, or use ScriptableObject.CreateInstance<T>() to make your SO, then use the appropriate JSON "populate object" call to fill in its public fields.

    If you want to use PlayerPrefs to save your game, it's always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:

    https://gist.github.com/kurtdekker/7db0500da01c3eb2a7ac8040198ce7f6

    Do not use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

    https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide
     
  3. QXTProductions

    QXTProductions

    Joined:
    Mar 20, 2022
    Posts:
    12
    Yes I have read your last comment, same as your current comment.

    But as I already said in my last post was the following, my game is based on text, buttons and a lot of extra UI. Most of the tutorials go about top-down, 2D and 3D games. Since I am quite a beginner this can be overwhelming. So I posted this new post to be pointed in the right direction :)
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Yes, inventories and save games are probably one of the hardest things to start with, no doubt about that.

    That's why all of my tips advise to start small and build up, which is commonly referred to as iterative development: each step is kept manageably small, steadily building upon previous steps.

    The point of tutorials is to get you those first steps.

    Excellent!!

    I shall consider you pointed in the correct direction by everybody in the other thread and by the posts above, whether you decide ultimately to go in that direction or not.

    Good luck!