Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sdgd · Mar 08, 2013 at 09:08 PM · c#functionfunction callpassthrough

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

Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Mar 08, 2013 at 09:32 PM 1
Share

Sounds like you should look into delegates (or Actions).

avatar image sdgd · Mar 08, 2013 at 09:50 PM 0
Share

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

avatar image Eric5h5 · Mar 08, 2013 at 09:57 PM 0
Share

Actions, in the .NET docs.

avatar image sdgd · Mar 08, 2013 at 10:03 PM 0
Share

ok thanks

ohhhh... I hate their site I can never read it properly but thanks anyway

avatar image $$anonymous$$ · Mar 09, 2013 at 02:05 AM 0
Share

here is a beter example of actions: here. Btw this is my favorite reference site, i would recommend it to anyone

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sdgd · Mar 19, 2013 at 12:43 AM 0
Share

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

avatar image
2

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sdgd · Mar 09, 2013 at 06:13 AM 0
Share

I'm not sure if I understand everything but it's 7:13 here and I haven't went to bed yet

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

function that returns multiple variables (in this case, lists) 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

how to put in function any variables? like object? 2 Answers

Function being called on multiple objects when just one is clicked 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges