Hi, i was wondering if someone could show or point me in the right direction of making my own delegate/callback function. Basically I want to make something that fires as a result of something, for instance if you have used the prime31 plug-ins (social networking ones in particular) they have delegates that fire once you have logged in or if you’ve failed to log in, hope i explained this well enough as im not the most experienced of programmers, any help would be greatly appreciated, thanks.
4 Answers
4I found the answer of that question through those examples.
http://forum.unity3d.com/threads/another-noob-question-callback-functions-solved.27998/
public delegate void WhateverType(); // declare delegate type
protected WhateverType callbackFct; // to store the function
public void something(WhateverType myCallback){
callbackFct = myCallback;
}
void callProcess(){
something(dosomething);
}
void dosomething(){
//...
}
Another faster way to do the same thing would be to use the Action class from System using System; public Action myAction; public Action<string> anotherAction; void Start(){ myAction += callback; anotherAction += callbackString; myAction(); anotherAction("foo"); } void callback(){ ... } void callbackString(string aStringParam){ ... } and you can subscribe multiple function to the same Action. myAction += aCallback; myAction += bCallback; myAction(); // will call a and b
– ABerlemontYou could have a get function that returns the value of the variable if you want another script to be able to read the variable or look into properties. I am just saying that by having that variable public it is hard to keep track on when the variable is getting changed.
– henkehedstromHave you tried Microsoft’s delegate tutorial? That’s pretty much all you need to know to start creating delegates. It has code samples, links to other resources and good explanation.
That prints the variable sent when the enemyScore(int pointsUp) is called from another script, also idk why i called it enemyScore it just triggers when an enemy dies and ups the score, I've since removed that print it was just making sure different enemies sent the right increase amount
– ChaosGirlEvarefer to this article: 1, using System.Action is the fastest way to implement callback function.
public IEnumerator FetchResponseFromWeb (System.Action<bool> callback) {
WWWForm form = new WWWForm();
WWW www = new WWW(url, form);
// ‘done’ will be null until the information has been fetched from the web or there is an error
yield return www;
// Make sure all code paths set the callback
if(www.error != null) {
callback(false);
} else {
print(www.text);
callback(true);
}
}
Start() {
StartCoroutine(FetchResponseFromWeb(callbackFunc);
}
public void callbackFunc(bool done) {
if(done != null) {
if(done)
print(“successfully fetched something from the web”);
else
print(“There was an error”);
}
}
Weird, it shouldn't go back to 0 again. Could you send the script that calls on the "enemyScore" function?
– henkehedstromI found this on another discussion, felt drop it here too.
currently I'm just restarting it with the play button in unity, but i added a print("Game Logic Start Ran"); to the start function and it is running every time, also when I print the score from any method other than enemyScore it prints as 0, but then as higher from the enemyScore method
– ChaosGirlEva