Search Unity

Is there a way I just don't know of to assign a struct in the inspector to a container?

Discussion in 'Entity Component System' started by MostHated, Mar 18, 2019.

  1. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    Hey all,
    I have been having a bit of a hard time with something. I am using a few containers for things including a List, Dictionary, and NativeMultiHashMap to store some data to use in a job, but the issue I was running into was the actual HashCode ended up not being the same for a particular object by the time it got to the job.

    When I try to make a List<> of a struct type, the inspector just, of course, ends up showing the actual fields that the struct has as seen below:



    I had been trying to use GetComponent<WaypointComponentWrapper>().Value from the ComponentDataProxy on the waypoint itself and then putting that into the key of a NativeMultiHashmap, then the value ends up being whatever other waypoints it was directly connected to which is from a List<WaypointComponentWrapper> which I also used GetComponent<WaypointComponentWrapper>().Value.

    The problem I have been having is, the HashCode of what comes from GetComponent<WaypointComponentWrapper>().Value directly from on the waypoint itself seems to differ from when I have another waypoint which has a List<WaypointComponentWrapper> and I drag a few points in, then try to do the same GetComponent<WaypointComponentWrapper>().Value ends up being different even though the same WaypointComponentWrapper object was used internally in the script to get it's value vs. getting the WaypointComponentWrapper.Value from the point that was dragged into a list. Hopefully that makes sense?

    So what I was hoping was that I could have a list that was just the actual struct in which I was trying to get the WaypointComponentWrapper.Value from of the ComponentDataProxy, and just drag that into the inspector, but I am not sure if that is even possible since when you serialize a struct field you don't end up with a slot in which you can drag one.

    Is there a way I can possibly do that while still keeping the same HashCode so that when I try to check the key of the NativeMultiHashMap using the contents of the value of another key it ends up being the same as it all technically should be coming from that getcomponent of the same Proxy item in the hierarchy.

    Thanks!
    -MH
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    It is not clear how you are mapping waypoints to entities. If you have a waypoint per entity, use the entity for the key rather than the waypoint struct. Otherwise you will want to generate your own hashcode for the waypoint. An easy solution is to store the proxy's HashCode into the waypoint itself and override the waypoint struct's HashCode function to return the proxy's HashCode.
     
  3. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    Oh great, thank you! I wasn't aware you could do that. That should work out just fine. I will give it a go. I may end up changing it to use the entity (originally they were not even entities, I have been changing things as I go along) as you mentioned though. I am just close to having it working as is, once I am sure it works, I will refactor.

    Thanks again!