Invoke with parameters?

Possible stupid question but I couldn’t find an answer. Can someone help me?

Is it possible to invoke a function while passing parameters?

like:

Invoke(“mymethodname”, parameter1, parameter2, invoketime);

function mymethodname(parm1,parm2)
{
etc…
}

Thanks in advance.

4 Likes

I’m pretty sure no. If you only need to call it once why not use the norm.

FunctionName(i : index, r : String) { }

2 Likes

Nope, but you can use a coroutine.

//In Start() or wherever
StartCoroutine(Foo(p1,p2));

function Foo(param1,param2) 
{ 
yield WaitForSeconds(timer);
//Do stuff
}

This is kind of pseudocodey, cause I’ve not used JS for a long time.

6 Likes

You could probably get around it by passing an array.

No, since you can’t pass any parameters to Invoke. As mentioned above, use something else instead, like a coroutine.

–Eric

4 Likes

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.

What? 0_0

6 Likes

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.

–Eric

3 Likes

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.

–Eric

1 Like

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.

Well, it’s used in the Invoke method to determine when to call your method, so no need to feel cheated, it’s still being used.

I wish they supported it.
In my situation, I need to invoke functions according to a object’s type:

System.Type myType=Object.GetType();

Invoke("Load"+myType.Name,0);

void LoadType1()
{}

And pass parameters to the LoadType(n) function, still wondering if there might be any workaround for this

1 Like

The Unity framework GameObject.Invoke method does not accept parameters, however the .net framework does support this via MethodBase.Invoke

First you’ll need to get the correct MethodInfo, which you can access from your type. Assuming the method is on the class calling this code:

var loadingMethod = this.GetType().GetMethod("Load"+myType.Name);

Next, put the arguments you need to send into an array of objects:

var arguments = new object[] { arg1, arg2 };

Finally, invoke the method on this object with the arguments:

loadingMethod.Invoke(this, arguments);
9 Likes

That works !
Thanks for the great suggestion.

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 new to C# and come from Java programming. Is that what u do corresponding to Java’s reflection mechanism?

1 Like

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.

Unless I’m misunderstanding I think this is Unitys way of doing invoking with a parameter Unity - Scripting API: UnityEvent<T0>

easily you can make another method that calls method with parameter. and you can invoke caller method


Invoke (“CallMyMethod”, 1f);

void CallMyMethod()
{
MyMethodWithParams(10f, 5f, “lots of diffetrent params”);
}