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

Storing global game data for access in Jobs?

Discussion in 'Entity Component System' started by Codecomp, Apr 24, 2020.

  1. Codecomp

    Codecomp

    Joined:
    Apr 17, 2019
    Posts:
    9
    I'm trying to create a database of information that will never change that can be accessed inside Jobs.

    So simplified in my current test project i have a number of ScriptableObjects including:
    ShipClassification
    - enum size
    Ship
    - float maxSpeed
    - float maneuverability
    - List<Cargo> cargo
    - ShipClassification classification
    Cargo
    - Item item
    - int quantity
    Item
    - string name
    - int cost
    Recipe
    - List<Item> components
    - List<Item> results

    When creating a new ship entity I had a Ship component that has a Ship ScritpableObject parameter, I could pass the ship in and get the data out. However this would only work on Run. So running all my movement logic on a Run and not Schedule ruled that method out.

    While I can easily transfer the ship data directly into a new Ship component that has speed, maneuverability and classification I encounter issues with managing items. And I've always been taught never duplicate data if its not changing, so in my head i should be referencing the ship anyway.

    I've played with Addressables, BlobAssets, Generic definitions for BlobAssets (As all the data has identical use just different data but apparently that's also not allowed).

    So after about a weeks worth of R&D, a couple packets of painkillers for headaches, and deleting everything I've done about 3 times over; I'm stuck.

    How can i set this up so my data is accessible in Jobs? How can I have a ship know its cargo, know what items are in there?

    At this point my only ides is to just scrap the ides of nice data management and just setup a static class for Ships, Items and Recipes and just hardcode all the data in because as far as I'm away that would work inside a Scheduled Job, but knowing my luck that probably would not work either.

    Any help would be appreciated as I'm completely at my wits end on this. I've gone back and forth on the Unity discord with this and had help from several people but after building it there's always something that just ends up not working with Jobs.
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,993
    This almost sounds like you want a SharedStatic which is a BlobAssetReference. But unfortunately I don't know about your problem or what difficulties you ran into with each approach you tried to give you any more insight.
     
  3. Codecomp

    Codecomp

    Joined:
    Apr 17, 2019
    Posts:
    9
    These are the reasons each method failed as far as I'm aware.

    Assigning ScriptableObject directly into a component:
    Just can't use Jobs to handle but works with .Run

    Blob assets:
    Works for storing the data however I end up needing to pass the reference to all the item data all over the game. For example when adding items to a player inventory I need to pass the reference to the item assets, the index into the assets, and the quantity. But I also need to pass that same reference into every recipe, or perhaps every enemy that can steal items.
    It is also massively heavy on copy and pasting, make a data struct, make a array for that data struct, setup a constructor, a builder assign data. And this can't be avoided with generics it seems as it has a fit with strings.

    Addressable I didn't get too far with.

    DynamicBuffer:
    I setup a hard coded some item blob assets for testing, setup a dynamicBuffer, assets a reference to the items, an index and a count. Add a system to loop through the data, and get told once again I can't access strings. "Burst error BC1011: Unable to get field ItemBlobData.name because System.String is a class type" so even hen accessing the blob data from a reference i can't access a string.

    It just seems that every singe avenue I go down the burst compiler either tells me to sod off, or works, with an error that will stop me from building, and this is usually because strings are a thing that exist without any more information. nothing seems to work with BlobStrings or FixedStrings

    All I want is to be able to do is in a system go, this ship, whats its cargo? What are the names of the items in there? without having to assign every bit of information about the item into a ship, mostly because i have no idea how the game will be needing this data in the end.

    I'm at the point where I am literally tearing my hear out over something that should be extremely simple.
     
    GDevTeam likes this.
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,993
    Try representing an item as an entity?

    You weren't using BlobString?

    Honestly it sounds like your real issue is using strings in Burst jobs. Which version of Burst are you using? The latest preview version added support for string literals.
     
  5. Codecomp

    Codecomp

    Joined:
    Apr 17, 2019
    Posts:
    9
    String errors are what seems to end up at the end of me building a while database interface yes, and I'm on 1.3.0 preview 7 currently.

    So I updated to the latest version and it will run but fives a error on closing. So I updated everything else and converted the strings to NativeString64 and ti works.

    However I'm still unsure if this should be a BlobString, or a Fixed string. both of which I'm still not sure on how to properly use as neither seem to be able to be instantiated with a string, as they only seem to have Length and toString properties. Yet I keep seeing them talked about as if they were supposed to be sued as a data type. Yet I see no way to store data with them.
     
    GDevTeam likes this.
  6. GDevTeam

    GDevTeam

    Joined:
    Feb 14, 2014
    Posts:
    90
    Debug.Log(UnsafeUtility.IsBlittable<NativeString64>()); // equals 'true', so we're seemingly onto something. But how to implement? NativeMultiHashMap?
     
    Last edited: Apr 26, 2020
  7. Codecomp

    Codecomp

    Joined:
    Apr 17, 2019
    Posts:
    9
    Literally loaded unity back up today to test blobstrings .. and its not working now.

    I'm pretty damned sure the burst compiler is propped up on some form of human sacrifice at this point ad the dev teams been lacking.