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 sgmongo · Oct 10, 2014 at 03:31 PM · arraymethod

Array.map Support

Prewarning: While I did see a similar post referencing C languages... and an answer that drew a line towards Dictionary for .net and Associative array for java-script, I was unable to find an answer.

Essentially I'm looking to add support for the Array.map method on my Unity(JS) script. I'm hoping its as easy as #import magic but, I haven't been able to dig anything up.

This Page Provides a script that would supposedly enable it directly, but alas does not survive the MonoDevelop compile process.

If someone could point me in the correct direction that would be wonderful. Please, be my programming batman!

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

1 Reply

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

Answer by Baste · Oct 10, 2014 at 03:57 PM

The problem here is that Unity's JavaScript (UnityScript for clarity) is not JavaScript. In particular, UnityScript uses 'proper' objects, while JavaScript uses prototypes. Thus, the prototype-based 'fix' in your link won't work.

The solution is to just replace the map method with the old-fashioned way; iteration! So where you'd do this in normal JavaScript:

 array.map(function(x) { 
     print(x);
 });

you're going to be doing this in UnityScript:

 for (var x in array) {
     print(value);
 }

I know, it's a bummer. In my opinion, you'll want to jump to C# eventually, because the disconnect between JavaScript and UnityScript means that there's pretty much nobody that can help you with your code other than other Unity users, while in C# you have the entire C# community to draw on.

If you're looking for the Map data type, on the other hand, use the Hashtable type. Test it with:

 #pragma strict
 var foo = new Hashtable();
 
 function Start () {
     foo['a'] = 5;
     Debug.Log(foo['a']);
 }
Comment
Add comment · Show 2 · 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 Eric5h5 · Oct 10, 2014 at 04:27 PM 0
Share

You should really use Dictionary, not Hashtable. Hashtable is obsolete and has the same issues that the similarly obsolete ArrayList has. By now there's quite a lot of info about Unityscript that can be found pretty easily with searching (mostly here and on the forums), so support isn't really an issue.

avatar image sgmongo · Oct 10, 2014 at 06:38 PM 0
Share

Thank you Baste for your helpful information. Could I bother you with a follow up question? I'm interested in learning to write my own methods as I've written my own encoder, xml parser, etc and would like to utilize them in that way rather than calling static functions (for stability reasons). I am planning to learn C# next but after I've found a solid base in UnityScript/derpyJS. Can you recommend some tutorials?

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

29 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 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

2D Array and other Script method access 1 Answer

Alternative for array.contains() method 1 Answer

how subtract total value element of array ?? 1 Answer

Static array with custom class? 1 Answer

How can I make an NPC follow a path specified by an array? 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