Is it possible to create a SharedGenericList?

caleidon

Member
Hi,

I'm trying to create a SharedVariable which would hold a value of a generic list so that I can have type safety.

Here's what I tried:

C#:
[Serializable]
public class SharedGenericList<T> : SharedVariable<List<T>>
{
    public static implicit operator SharedGenericList<T>(List<T> value) { return new SharedGenericList<T> { mValue = value }; }
}

However, I seem to get errors during runtime and even during editing nodes in the Behavior Designer.
What did work was this:

C#:
[Serializable]
public class SharedGenericList : SharedVariable<List<object>>
{
    public static implicit operator SharedGenericList(List<object> value) { return new SharedGenericList { mValue = value }; }
}

However, I have to cast everything and there isn't really type safety. Is there any way to achieve this?

I need this to pass a list of custom objects (classes) between behavior tree tasks. I am aware there is already a SharedObjectList but most of the time the objects I'm trying to pass aren't Unity Objects, but regular C# ones.
 
Due to serialization limitations you aren't able to save a templated object like your first example. In later versions of Unity they have released a [SerializeReference] attribute which attempts to solve it but it still doesn't work with generic objects. Our custom serializer follows similar rules as Unity's serializer so your first case isn't possible.
 
Top