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 MrSkiz · Nov 25, 2013 at 11:02 AM · javascriptfunctionoverloadmultiple-values

Is there a more elegant way to get different values (types) from a function

Hi,

I'm scripting some kind procedural level builder acting like a worm going through a 3Darray of cells. In some parts of the code, I just want to check how many cells surrounding the worm's actual position have a certain status (eg. empty or filled), in other ones I'll want to have a precise status of each cells in a certain order.

As Javascript doesn't allow for multiple refs/out, I'm going like this : to check how many cells are empty I call a function sending a Vector3 of the worm's current position and waiting for an int in return:

var zeroes : int = CheckNeighbors(currentWormPos);

To check the status of each cell counterclockwise I call the same function but this time sending the Vector3 as three ints and waiting for a string in return:

var neighbours : String = CheckNeighbors(currentWormPos.x, currentWormPos.x, currentWormPos.x);

And so here's my called function and it's overloaded sister : function CheckNeighbors(coord:Vector3){ var up : int = my3DArray[coord.x,coord.y,coord.z+1]; var left : int = my3DArray[coord.x-1,coord.y,coord.z]; var down : int = my3DArray[coord.x,coord.y,coord.z-1]; var right : int = my3DArray[coord.x+1,coord.y,coord.z];

 var zeroes : int = 0;    
 if (up == 0){ zeroes++; }
 if (left == 0){ zeroes++; }
 if (down == 0){ zeroes++; }
 if (right == 0){ zeroes++; }
 return zeroes;

}

function CheckNeighbors(x:int,y:int,z:int){ var up : int = my3DArray[x,y,z+1]; var left : int = my3DArray[x-1,y,z]; var down : int = my3DArray[x,y,z-1]; var right : int = my3DArray[x+1,y,z];

 var neighborhood : String = up.ToString()+left.ToString()+down.ToString()+right.ToString();
 return neighborhood;

}

As I assume it'll work as is, I just wanted to know if there's any way more elegant to achieve this.

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

0 Replies

· Add your reply
  • Sort: 

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

16 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 avatar image avatar image

Related Questions

Problem accessing variable in javascript 1 Answer

Accessing function from another script won't work 1 Answer

running code outside of update! 3 Answers

Better way to delay a function for a few seconds? Javascript 1 Answer

How would I move an Object after collision? 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