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

NativeContainers that contain interfaces.

Discussion in 'Entity Component System' started by Blargenflargle, Jun 3, 2020.

  1. Blargenflargle

    Blargenflargle

    Joined:
    Feb 24, 2019
    Posts:
    92
    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:
    92
    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:
    679
    I think that FunctionPointer is what you are looking for
     
  4. WAYNGames

    WAYNGames

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

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    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:
    92
    @Micz84 Yeah, that makes sense. This means I'm gonna have to seriously refactor things but that's okay.