It takes a float parameter. Generally there’s no distinction between that and an array, so it could take more than one float, unless it didn’t work. It depends how low level the array is. If it sees an object, then it probably wouldn’t work.
The float is used for the time that the function will be invoked. It’s not a parameter that’s passed to the function.
Er…no. If the parameter type is a float, then you can never use anything else for that parameter aside from a float. A few types will implicitly convert to a float (like an int), but the parameter will still be a float in the end after being converted, and can certainly never be any type of array.
In c, an array is just a pointer to the first member memory location. I have no idea how java script works. I think it would work in c, not even sure about that. Anyway, I was just musing.
Unity does not use C, so that’s irrelevant. Even if it did, the float is used to launch the function at a specific time, it’s not passed to it, as I mentioned.
Yeah, I saw that. Haven’t used it yet. Interesting that it wouldn’t actually be used in the function, I almost feel cheated. I’m just learning Unity and mainly reading the boards for that purpose.
In my case, I was getting network requests coming back on a different thread, so I needed a way to push the results back to the main thread (messing with Unity objects in another thread was causing wacky results). I solved this with an array of pending functions that got cleared in Update(), like this.
using System;
using System.Collections.Generic;
using UnityEngine;
public class Invoker : MonoBehaviour
{
private struct PendingInvoke
{
public Action<object[]> invoke;
public object[] parameters;
public PendingInvoke(Action<object[]> invoke, object[] parameters)
{
this.invoke = invoke;
this.parameters = parameters;
}
public void Invoke()
{
this.invoke(this.parameters);
}
}
private static Invoker instance;
public void Awake()
{
if (instance != null && instance != this)
{
Destroy(gameObject);
return;
}
instance = this;
DontDestroyOnLoad(gameObject);
}
private List<PendingInvoke> pendingInvokes = new List<PendingInvoke>();
public static void Add(Action<object[]> invoke, object[] parameters)
{
instance.pendingInvokes.Add(new PendingInvoke(invoke, parameters));
}
private void Update()
{
foreach(PendingInvoke invoke in pendingInvokes)
{
invoke.Invoke();
}
pendingInvokes.Clear();
}
}
/////example
public void InvokedAddFunction(object[] parameters)
{
Debug.Log(((int)parameters[0]) + ((int)parameters[1]));
}
public void Start()
{
//instead of Invoke(target,"InvokedAddFunction")...
//the following will print "13" in the next update
Add(InvokedAddFunction, new object[]{5, 8});
}
Also, even though it’s years later, for any who stumble on this thread I really want to reply to what fire7side said above about arrays and floats. He’s completely wrong on all fronts – in C#, types are strict and so there is a major difference between an array and a float. In fact, I think in every single language I can think of, that’s the case in terms of how they’re stored in memory. When he’s talking about them being the same in C, he’s referring to arrays being chunks of continuous memory that have a single integer pointer to where they start (hence you needing to manually keep track of their length). A floating point number (float) is completely different. So don’t take that post as anything real!
I’m not familiar with Java, but yes, this is known as reflection. It comes with some caveats, such as slower performance and may not work at all in scenarios that don’t support just-in-time compiling.