- Home /
Dynamic Parameters/Arguments
Hi guys,
In my current project I'd like to pass on parameters in a dynamic fashion. I don't want to use overloads since this makes the code extremely ugly and long for something that should really be very compact. I believe in C# 4.0 one can use the "dynamic" keyword to achieve this, but I am not sure how to make this happen in Unity's C# 3.5.
In my specific case, I'd like to call a sync function which then executes a bunch of RPC calls depending on the dynamic parameter. This parameter could take a variety of types/values, e.g. string, float, Vector2, Vector3, etc.
Example:
private void Sync(string function, int id, int type, dynamic val) {
if (val is float) ......
}
As mentioned, "dynamic" is unfortunately not possible in Unity at this point. What would be another, yet efficient way to achieve this?
Answer by Adamcbrz · Aug 05, 2013 at 04:33 AM
I am assuming that the script calling the method will now the type. If so something like...
void Swap<T>(ref T lhs, ref T rhs)
{
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}
Maybe that will work for your situation
argh, this seems like it could work. Unfortunately my Sync function is using a delegate with yield statement, resulting in this error:
Parameter val' cannot be used inside
anonymous method' when using ref' or
out' modifier
managed to fix this in the end with lots of convoluted copies and statements like this:
if (val.GetType() == typeof(Vector3)) {
Vector3 parameter = (Vector3) (object) val;
}
thanks