delegate keyword

So i was checking how to send value of slider to code.

researchInvestment.onValueChanged.AddListener(delegate { ResearchInvestmentChange(); });

this is code at start function that creates listner which automatically calls ResearchInvestmentChange when slider value is changed.

My question is what “delegate” means. I found some info about Delegate which is i guess custom delegate function and here in my code is maybe Mono delegate, but don’t really understand it. Anyone has link or short description what this delegate in code means?

Thanks.

In this context here the delegate is declaring an anonymous function:

You’ve effectively declared an unnamed/anonymous function, in the shape of ‘void method();’, that calls the function ResearchInvestmentChange.

…

Note, this was added in C# 2.0, and is somewhat considered a little out dated syntax wise. People usually write lambda expressions these days:

researchInvestment.onValueChanged.AddListener(() => ResearchInvestmentChange());

…

Lastly, I will point out that in this context, the anonymous function is unnecessary. You could just as easily have said:

researchInvestment.onValueChanged.AddListener(ResearchInvestmentChange);

This is because ResearchInvestmenChange is already shaped like a UnityAction… ‘void method();’. So you could just pass it along instead.

1 Like
researchInvestment.onValueChanged.AddListener(() => ResearchInvestmentChange());
Delegate UnityAction<float> does not take 0 arguments

researchInvestment.onValueChanged.AddListener(ResearchInvestmentChange());
also doesnt work

but i get what delegate does but still dont get it completely why you have to make fake funtion to call this one, because of return value if it has one or what?
thanks

researchInvestment.onValueChanged.AddListener(ResearchInvestmentChange);

NOT

researchInvestment.onValueChanged.AddListener(ResearchInvestmentChange());

Note that my version does not call the method… the () part…

When get a reference to a method delegate you access it by name, rather than call it with the closing parens.

var d = obj.Method; //get a delegate of Method
var o = obj.Method(); //calls Method and gets its result if any

yeah i done

researchInvestment.onValueChanged.AddListener(ResearchInvestmentChange);

says cant convert “method group” to “UnityAction”
so delegate is used when you want to call function with another fake function and thats because mono want it like that.

UnityAction means it needs to pass a float value to the delegate.

So your ResearchInvestmentChange function needs to accept a float parameter.

public void ResearchInvestmentChange(float a)

fixes

researchInvestment.onValueChanged.AddListener(ResearchInvestmentChange);

but why, i dont need to use flaot value so i just need to have one for no reason?

It’s part of the contract agreement you made with the Slider. The Slider will send notifications to a delegate along with the new slider value. You create a function that matches the delegate that the Slider expects to call.

Whether you use it or not is irrelevant. You’re free to ignore it if you don’t need it.

Similarly for toggle buttons, you’d need to create a function that accepts a bool parameter, and so on.

1 Like

oh okay, i get it now, so it automatically sends float value of slider and i dont need to check it with direct reference.
like this

public void ResearchInvestmentChange(float a)
    {
        RD.DB.researchInvestment = researchInvestment.value;
        researchInvestmentText.text = (int)(researchInvestment.value * 100) + "%";
        Debug.Log(a);
    }

both slider.value and float a return the same value,

Thanks

also lordofduct, really like artstyle of Murder Mansion on your blog, better than ludum dare one which was “crisper”.

Thanks, yeah, my artist specifically is pushed the art style int he direction he wanted the ludum dare to be… but you know, time crunch on that prototype.

Games coming out real soon. Busy busy crushing bugs.

1 Like