Search Unity

NativeContainers that contain interfaces.

Discussion in 'Data Oriented Technology Stack' started by Blargenflargle, Jun 3, 2020.

  1. Blargenflargle

    Blargenflargle

    Joined:
    Feb 24, 2019
    Posts:
    21
    So, say you have IGeneric, a GenericStruct, and a GenericStruct2, both of which implement IGeneric. It's possible to have a NativeArray<GenericStruct> but not NativeArray<IGeneric> to hold both GenericStruct and GenericStruct2. Why? Is there any workaround? I tried using generics with type constraints but that still only lets me declare a list of either GenericStruct or GenericStruct2 which is functionally the same as just declaring a list of either of those types.

    Is there some way to use generics in reverse? For instance, generics let you say "This containers (one) type MUST BE a struct and IGeneric" where I'd like to say "This container can only contain things that are type struct and generic." if that makes sense...
     
  2. Blargenflargle

    Blargenflargle

    Joined:
    Feb 24, 2019
    Posts:
    21
    I guess as an additional question: Is there ANY way to pass code into a job? I've tried lambdas, interfaces, actions... etc. Basically I want to be able to hand a job some code and have it run it. The closest I've gotten to getting it to work is interfaces, so that's the focus of this post. If there's a better way of doing things let me know.
     
  3. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    609
    I think that FunctionPointer is what you are looking for
     
  4. WAYN_Games

    WAYN_Games

    Joined:
    Mar 16, 2019
    Posts:
    726
    Last edited: Jun 5, 2020
  5. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    375
    Native containers can't store interface because they reserves memory for each element and interface implementations can have different sizes. Moreover, it can't be passed by value.
     
    Blargenflargle likes this.
  6. Blargenflargle

    Blargenflargle

    Joined:
    Feb 24, 2019
    Posts:
    21
    @Micz84 Yeah, that makes sense. This means I'm gonna have to seriously refactor things but that's okay.
     
unityunity