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 dansav · Aug 02, 2011 at 02:04 AM · javascriptnewthis

help converting javascript this function

I'm trying to convert a few html/javascript array functions into unityscript for a statistics project in unity. This one is confusing me because they use the "this" keyword. It creates a 2 dimensional array. Any help rewriting this in unityscript would be appreciated...I just need help with the "this" line. Later there is a line that calls this using New. Like xy=new makeArray2(). I don't think either of these are allowed in unityscript right?

 function makeArray2 (X,Y)
         {  
         var count;  
         for (var count = 0; count < X; count++)  
                 this[count] = new makeArray(Y);  
         }   
   
 function makeArray (Y)  
         {  
         var count;  
         this.length = Y+1;  
         for (var count = 0; count <= Y+1; count++)  
                 this[count] = 0;  
         }   
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
3
Best Answer

Answer by Peter G · Aug 02, 2011 at 03:37 AM

As of 3.2, Unityscript has built-in support for multidimensional arrays. It looks like you're creating a box shaped jagged array so a multi-dim array is slightly different, but it should be what you want:

 var multiDimArray : int[x , y];

if you do need a jagged array, the best way to do them is to use this helper script. Unityscript has syntax for using them, but none for creating them so you need a C# script to create your instance.

Another technique that will work is to use js arrays. Its worth noting that js arrays aren't as fast as .Net arrays so you should generally try and avoid them in Unity:

  var arrayOfArrays : Array;
  for (var i = 0 ; i < 6 ; i ++) {
        arrayOfArrays.Push( new Array(i) );
 }
 //Creates an array full of 6 more arrays each one unit longer than the previous.

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 dansav · Aug 02, 2011 at 05:41 PM 0
Share

thanks for the update on arrays and solution. I was confused about what "this" was referring to, but I guess it doesn't matter if there's a better solution.

avatar image Peter G · Aug 03, 2011 at 02:49 AM 0
Share

The this keyword should do the same thing as it does in web js. It refers to the owner of the function. The difference probably comes from the fact that web js uses prototyping rather than class based OOP so I'm not sure what the this in your function is referring to. I've never bothered with web js or Lua or an prototype based language.

avatar image CHPedersen · Aug 03, 2011 at 09:53 AM 0
Share

The this-keyword in an OOP context is always a reference to the current instance of an object of the type defined by that class. If used in a script, it should always refer to an instance of that script (since all scripts are objects that derive from $$anonymous$$onoBehavior). You can verify it by simply writing Debug.Log(this). It'll print the script's name to the console after the GameObject to which it is attached. Other languages sometimes call the this-keyword "me" (Visual Basic) or "self" (Objective-C).

See http://en.wikipedia.org/wiki/This_%28computer_program$$anonymous$$g%29

avatar image dansav · Aug 03, 2011 at 06:34 PM 0
Share

Great thanks for all the help and pointers Christian and Peter.

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

Help with javascript texture change 2d platformer 3 Answers

Windows Script error upon opening a new javascript behavior script. 4 Answers

Setting Scroll View Width GUILayout 1 Answer

Open Link in new window or tab 1 Answer

Restart Level not working... 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