Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Need some help to conceptualize this/how to pass the right data to the right entity?

Discussion in 'Entity Component System' started by TJaenichen, Feb 18, 2019.

  1. TJaenichen

    TJaenichen

    Joined:
    Oct 12, 2014
    Posts:
    29
    I have a text file with a lot of position descriptions for a bunch of objects over time.

    Once I'm done parsing this I start creating the entities using the EntityManager, set some ComponentData (the mesh for that type of object, starting position...)

    I got my movement job setup and that kicks off fine as well.

    What I am having trouble with is, how do I make sure that in the JobComponentSystem the right set of coordinates is being assigned to the right Job on Execute?

    What I am trying to do is, parse a little bit of text (a few seconds worth of data) and then create/update my entities according to that. But somehow there is a link here that is missing, if have been staring at youtube videos for hours and I know that I didn't "get" some concepts yet

    Edit: Maybe add another component to hold the data (including an Id), assign any new data to that and then use it inside the job?
     
    Last edited: Feb 18, 2019
  2. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    What part is it that you're missing? Are you wanting to track which entity has which bit of data so you can update it? A bit more context on the files and what you're planning might give me better ideas. You might be able to do something with NativeHashMaps, but I'm not sure on how exactly you're parsing that information/using it afterwards if that would be ideal, or if it's possible to just use another component like you said.
     
    Last edited: Feb 18, 2019
  3. TJaenichen

    TJaenichen

    Joined:
    Oct 12, 2014
    Posts:
    29
    I think part of my problem is that I haven't figured out yet if I should track the data in a component attached to the entity, or the job.

    The data roughly looks like this:
    TimeIndexInSeconds, ObjectId, ObjectType, Position

    So you have one line for each object and then there is the next time index:

    1, 1, <type>, <position>
    1, 2, <type>, <position>
    2, 1, <type>, <position>
    ...

    I parse that and create a dictionary<objectId, ObjectData>, where ObjectData contains the type and a list of timesteps and positions whose index correspond to the timestep. So if the object exists in seconds 5 and 6 timesteps would contain [5, 6] and positions [positionAt5, positionAt6] (The structure of the dictionary can and probably will change. I am not married to that)

    It's fairly fast with regular c# to figure out which objects exist at a certain point in time and now I want to offload determining the position at a certain point in time and assigning the position to a job.

    Some other issues... Not all objects exist the whole time. They pop in and out of existence. Also I expect the files to be fairly large, so I don't want to parse everything in the beginning, but only when I need to. This means that the dictionary that holds all that data will change. And entries in that dictionary will have data added/removed.
    So when I pass an array into the job the indexes from the last frame don't match the one from the previous. (Somewhere there is where I have a problem conceptualizing)
     
  4. TJaenichen

    TJaenichen

    Joined:
    Oct 12, 2014
    Posts:
    29
    Figured it out.. stil some things I don't understand though.

    When creating the entities I add a DataComponent that has an id.

    In the JobSystem I allocate arrays for everything I need in the job, including an array of Ids.

    In the job I then search through that array, to find the index of the current id. Then I check at the position of that index in the other arrays for the additional info I need.

    I hope this makes sense.

    What is weird is that I have to declare the arrays as a member variable in the system and then dispose of them in the next frame, before reallocating them. Anything else and ECS screams at me.
     
  5. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    Glad you figured something out, was way too late for me to be thinking about solutions when I posted.

    That's because the safety system can't ensure that you actually dispose the arrays, and you might not if you quit between frames. For TempJob allocators, you can use [DeallocateOnJobCompletion] attribute on thte array member of the last job that uses the array. Alternatively, if you have a nativelist, nativehashmap, etc(something that is resizable, you can use array too but that's more limited), you can use Allocator.Persistent, then you'd create the container by overriding OnCreateManager() in your system, Clear()/rebuild in OnUpdate() if you need to, then dispose in OnDestroyManager().
     
    Last edited: Feb 18, 2019