Using return values from Methods as argument for Events

Hi,

Is it possible to have events accept return values from methods as an parameter rather than having to “hardcode” a string for example?
I could pass a script so the method the event calls gets the desired value itself.

But that’s not very practical :confused:

I would like to pass a variable or a method with a return type.

Making it a bit more dynamic without having to write scripts to have this behaviour :slight_smile:

I would try to extending the event system or something like that if that’s possible. But maybe i was just missing something

1 Like

Hi, I don’t quite understand the question. Would you be able to show me some code that shows how you would like this to work so I could understand?

I think I have the same problem.
I have this function

public void Click (Object o)

with the editor I add it as follows:

But… If I want to do for code

btn.GetComponentInChildren<UnityEngine.UI.Button> ().onClick.AddListener (Click);

Gives me an error…
The only solution I found was removing the parameter of the function, but I need the parameter.

So, can you help me do this with code?

Sure :slight_smile:

Suppose i have a class like this:

public class RandomStringGenerator : MonoBehaviour {
    public string GetRandom() {
        return // string with random characters;
    }
}

When i click on a button i want to change the text of a Text Component to the value “GetRandom” returns.
Right now i would have to create a new script which is attached to that button:

public class ChangeText : MonoBehaviour, IPointerClickHandler {
    public RandomStringGenerator rsg;
    public Text label;

    public void OnPointerClick(PointerEventData eventData) {
        label.text = rsg.GetRandom();
    }
}

That way i get the desired funcionality, but it’s strange that i have to create a script just to assign a value that could change.

It would be great to be able to set something like this up right in the button component.

I hope i got it this time :smile:

1 Like

Any news on that Front? :slight_smile:

i know this kind of stuff from c++ since your return value is a normal type it should be possible to take this return value and give it as agument to another function like

function1(function2());

or u call function1 inside of function2 like

function2(){
random=function1();
}

I understand your use case, but it’s not something that is easy to support or expose. Take for example you have a hook up with multiple targets that all return something, which one of the targets sets the value on the text?

1 Like

It’d be quite a useful feature.

The same way C# delegates do it, last method invoked

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/using-delegates