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 sparkzbarca · Jan 16, 2013 at 07:15 AM · programming

how to pass a function to a function

I have a function that differs in only one real way for various methods. How it sorts input. For example if you want the lowest 2 points on the face of a object it finds all objects in the face and sorts them by Y. If you want the farthest in an X or Z axis direction it sorts by that.

I would like to pass to the function the sort method to be passed to sort.

so like

 int CompareY(int first, int second)
 {
   ....
 }

later i'll use that method via

facevertices.sort(CompareY)

the thing is i'd like to be able to do something like

 int mymethod(void sortmethod)
 {
 facevertices.sort(sortmethod)
 }

and then be able to pass it compareY or compareZ or compareX as the sort method.

is that possible to pass a function itself?

Comment
Add comment · Show 3
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 cdrandin · Jan 16, 2013 at 07:29 AM 0
Share

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine.html is the only one I know so far with Unity.

There are probably more derived from $$anonymous$$onoBehaviour

avatar image Fattie · Jan 16, 2013 at 07:45 AM 0
Share

for the record, in Unityscript (javascript),

simply use the incredibly useful Function type

avatar image sparkzbarca · Jan 16, 2013 at 08:09 AM 0
Share

not using java, C#

It seems my answer $$anonymous$$AY BE delegates

Func I think is the C# equiv of javascipt Function type but as my comment on the answer suggests. It doesnt seem to be built in to the base monobehavior methods.

i'm not sure if i need to include something besides system.collections and system.collections.generic to access the func declaration.

1 Reply

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

Answer by iwaldrop · Jan 16, 2013 at 07:36 AM

While you could use a switch to handle the different methods of sorting this is actually a pretty good use case for playing with the Action and Func features of the C# language. Check this out!

http://simpleprogrammer.com/2010/09/24/explaining-what-action-and-func-are/

Comment
Add comment · Show 4 · 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 sparkzbarca · Jan 16, 2013 at 08:06 AM 0
Share

Func doesnt seem to be supported ( i happened apon that googling).

I wanted to do

 int sortfunction1(int first, int second)
 {
 ...
 }
 int sortfunction2(int first,int second)
 {
 ...
 }
 
 int otherfunction(func<int,int,int> sortmethod)
 {
 blah.sort(sortmethod);
 }
 
 but 
 
  

func doesn't seem to actually be allowed. is there something i need to include to be able to use this? (that is to say autocomplete isn't showing it as a option atm).

avatar image iwaldrop · Jan 16, 2013 at 08:22 AM 0
Share

make sure you put this at the top!

 using System;
avatar image sparkzbarca · Jan 16, 2013 at 08:49 AM 0
Share

figured that out finally which is useful for later, but turns out i dont quite need it atm. Really what i need to pass is an Icomparer

i'm going to open a seperate question for that if you could maybe help. thanks for you help with this one though :)

avatar image fafase · Jan 16, 2013 at 09:34 AM 0
Share

func is actually Func with cap F. Could it be your error?

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Problem with 3D platform tutorial 2 Answers

Best Language for Unity 0 Answers

Problem with getting a value from a enum 2 Answers

Can be mesh asset saved with an editable parameter? 0 Answers


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