- Home /
is it possible to pass functions through functions?
I'm thinking - is it possible to pass functions through functions?
let say I want to pass 2 functions:
ExecuteWhatever ();
ExecuteRaiseF ();
private void LoopThroughRingF (int Ring, int PosZ, int PosX, bool FalseIt, bool whatever){
if (Ring == 0){
int i = 0;
int ii = 0;
ExecuteRaiseF (i, ii, Ring, PosZ, PosX, FalseIt);
}
else {
for (int i= -Ring; i<=Ring; i++){
int ii=Ring;
ExecuteRaiseF (i, ii, Ring, PosZ, PosX, FalseIt);
ii = -Ring;
ExecuteRaiseF (i, ii, Ring, PosZ, PosX, FalseIt);
}
for (int ii=Ring-1; ii>(-Ring); ii--) {
int i= -Ring;
ExecuteRaiseF (i, ii, Ring, PosZ, PosX, FalseIt);
i = Ring;
if (whatever == true){
ExecuteRaiseF (i, ii, Ring, PosZ, PosX, FalseIt);
}
else {
ExecuteWhatever ();
}
}
}
}
if it's not possible I'd have to pass a bool operator whatever (or int) or whatever
and check if it's same as I want
uhm what
actions?
Actions gives me too wide search can you show me actions what do you mean?
and delegates I don't know how would I use them here without more scripting as I'm already doing with workaround
ok thanks
ohhhh... I hate their site I can never read it properly but thanks anyway
here is a beter example of actions: here. Btw this is my favorite reference site, i would recommend it to anyone
Answer by fafase · Mar 09, 2013 at 05:41 PM
using System;
void SomeFunction(){
//here do some stuff
}
void FunctionThatUsesOtherFunction(Action action){
// Do stuff
action();
}
lets say an example:
using System; // I think you do need to add that one
void SayBigger(int n){
print(n);
}
void CheckAndSayIfBigger(int i , Action action){
if(i>5)action(i);
}
int number = 0;
void Update(){
CheckAndSayIfBigger(number , SayBigger);
number++;
}
Note that the function SayBigger is passed without () because we are passing the address. If you need to pass a function with return value then same again with Func or Predicate if it returns a boolean.
Passing parameters is no big deal. You pass the method the same way and then you pass the parameter to the method reference.
ok thanks am can you re edit the answer please
and yes this is exactly what I'm looking for
void SayBigger(){
print("It is bigger!!!");
}
void CheckAndSayIfBigger(int i , Action action){
if(i>5)action();
}
int number = 0;
void Update(){
CheckAndSayIfBigger(number , SayBigger);
number++;
}
and what do I do if I'd want it this way?
void SayBigger(){
Debug.Log("It's Bigger");
}
void SaySmaller(int i){
Debug.Log("It's Smaller");
// do what ever with i
}
as if I'll give through
action(i);
and I call SayBigger
will it know that SayBigger does not pass anything through? or must all Actions contain same inputs?
and thanks :) for the answer and sorry for late reply wasn't on unity for about 3 weeks
Answer by Owen-Reynolds · Mar 09, 2013 at 06:02 AM
Delegates do seem pointlessly long compared to other (non-C#) ways of passing functions, but they do fully allow you to pass (prototyped) functions. Looks something like this:
// define in your ArrayLooping class:
public delegate void DoStuffSig(Transform T);
public DoStuffSig HandleBadDataFunc; // HBDF is a plug-in void(Transform) func
// create/set in the client code:
void actualBadDataHandler(Transform tt) { ... } // actual func to pass
HandleBadDataFunc = actualBadDataHandler;
// dispatching, back in your ArrayLooping class:
if( ... ) HandleBadDataFunc(dogTransform);
I'm not sure if I understand everything but it's 7:13 here and I haven't went to bed yet