OSC Generic Type at Runtime (Object)

Hello, trying to make a system with OSC where I would need Generic Type at Runtime (Objects).

By default OSCReceiver is generic but it’s only at compile time, I need the OSCReceiver to be able to return an Object that I can later cast to the type I need.

This does not sound possible because OSC may need the type to actually convert the raw OSC data to something coherent in C#, but maybe there is a way?

Thanks.

not sure if it would suit your case but there is indeed an OSCReceiver (Object). see its helppatch. if it doesn’t suit, please elaborate your case.

Checked it and it does not do what I want, but after some thinking I don’t think it’s possible.

What I wanted is a way to get a spread of objects that I can cast later to the type I wanted at runtime. But just thinking about a scenario where I can either receive float or vector4 would result in different number of elements and thus don’t make a lot of sense for dynamic casting.

Would make sense for int vs float but not really for float vs vector2/3/4.


What would work is to have an OSCReceiver with an extra input of type Enum (with value like float32, vector2, vector3, int, etc.) where we can set the return type of the received data at runtime. This node would then be able to return Spread<Object> that can be cast at runtime safely. (This is made possible because the Receiver knows the “Size” of each Object thanks to the enum input which gives that information).


Don’t know if the proposition makes a lot of sense; I was just making a system that needed dynamic datatype over OSC at runtime and got stuck.

You can do this, with something like adaptive pattern, basically:

public T Get(object packet)
{
    if (typeof(T) == typeof(Vector2))
    {
         Vector2 v2 = OSCDecode<Vector2>(packet);
         
         // Unsafe.As avoids the boxing penalty of (T)(object)v2
         return Unsafe.As<Vector2, T>(ref v2);
    }
    if (typeof(T) == typeof(Vector3)) ...

    throw new ArgumentOutOfRangeException(nameof(packet), "Unsupported type requested.");
}

But osc decoder seems to be done in the patch so prolly you have to use some other osc decoder like GitHub - VolcanicArts/FastOSC: Fast and memory-optimised C# OSC library for .NET8+ · GitHub

Note also: this would be a type it’s connected to, so if you connect this to Vector2 it would be only Vector2. it’s sounds like what you really want is something that would convert anything to the object internally. So:

public object Get(object packet, Type targetType)
{
    if (targetType == typeof(Vector2))
    {
         Vector2 v2 = OSCDecode<Vector2>(packet);
         
         // Unsafe.As avoids the boxing penalty of (T)(object)v2
         return v2;
    }
    if (targetType == typeof(Vector3)) ...

    throw new ArgumentOutOfRangeException(nameof(packet), "Unsupported type requested.");
}

Interesting, but seems a bit too advanced for me; do you have any resources on Adaptive Pattern in VVVV?
And where would you put the snippet of code you gave?

(Never done custom c# in VVVV)

Thanks

Basically you can do some sort of typed decoder.

Do new C# File > Static Enum

Define something like:

public enum MyDataTypes 
{
   Float,
   Vector2
}

Then do switch: