Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 billybobsmojo · Jul 26, 2011 at 06:14 PM · listparametersargument

function arguments list...?

In Unity JS is there any way to get or set the arguments passed to a function?

I’m trying to create an update manager that can trigger a list of attached functions.

So far I've got it working fine as long as the functions it calls don’t require parameters, or at least have a predetermined number of parameters passed in.

Would I be able to add an arguments array to callIt. eg: callIt.arguments[]=[1,2,3];

// CODE ---------------------------------------------------

 var IntroUpdates:UpdateEngine;
 
 function Start(){
     IntroUpdates=new UpdateEngine() as UpdateEngine;
     
     IntroUpdates.AddObject(test);
     IntroUpdates.UpdateAll();
 }

 class UpdateEngine{
     var updateHash: Hashtable;
 
     function UpdateEngine(){
         updateHash = new Hashtable();
     }
     function AddObject(functionPointer):void{
         updateHash.Add(functionPointer, functionPointer);
     }
     function removeItem(removeKey):void{
         updateHash.Remove(removeKey);
     }
     function UpdateAll():void{
             for (var item  in updateHash.Keys){
                 var callIt : Function=item as Function;
                 callIt();
             }
     }
 }
 function test(){
     Debug.Log("test triggered");
 }
Comment
Add comment · Show 2
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 flaviusxvii · Jul 26, 2011 at 06:39 PM 0
Share

$$anonymous$$aybe this will help.

http://en.wikipedia.org/wiki/Variadic_function#Variadic_functions_in_JavaScript

avatar image billybobsmojo · Jul 26, 2011 at 09:34 PM 0
Share

Trouble is "arguments" is not recognised by Unity.

Does anyone know of an equivalent?

2 Replies

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

Answer by billybobsmojo · Sep 04, 2011 at 09:53 AM

Looks like there is no equivalent to arguments. Although I have hit upon an alternative. Simply by knowing how many arguments I want to pass to a function enables me to modify how I call it.

If there's only one argument, only pass one. If two, channel to a different statement that specifically sends two. It's a little awkward but achieves the desired result:

 if(functionParams.length==0){
     callIt();
 }else if(functionParams.length==1){
     callIt(functionParams[0]);
 }else if(functionParams.length==2){
     callIt(functionParams[0],functionParams[1]);
 }
Comment
Add comment · 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
1

Answer by Peter G · Sep 04, 2011 at 12:35 PM

In C#, you can use the params keyword to create a variable number of parameters. Unfortuneately is doesn't work in js, but you might consider writing your trigger script in C#, then calling it from js.

   public void Trigger(Collider col, params int[] list) {
           //do stuff
           for(int i = 0; i < list.Length ; i++ ) {
                Debug.Log(list[i]);
           }
   }

   Trigger( someTrigger , 1 ,3, 4, 8);
   //pass 4 arguments to the list
   Trigger( someTrigger , 1 , 2, 0);
   //pass 3 arguments to the list
   Trigger( someTrigger )
   //list is empty
   Trigger( someTrigger , new int[] {1 , 3, 5} );
   //you can also pass an array of a matching type to the list.
 

MSND page on params

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

List Works But Still Get Argument Out of Range Exception 1 Answer

Not compatible with the argument list 1 Answer

Error: Argument is out of range Parameter name: index 1 Answer

How to use unlimited parameters in a function? (JS) 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