• 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
2
Question by Fattie · Dec 23, 2011 at 02:47 PM · javascriptcastingjavascript arrays

Javascript array, use with a struct ?

Note! for any future readers -- never ever use arrays, listen to Eric ...


Here's one for Javascript/Unityscript experts! Say you have a struct...

 class Dogs extends System.ValueType
     {
     var height:float;
     var width:float;
     var bark:float;
     }

Say you have a "Javascript" array ... (NOT a List or "built-in" array)

 private var d:Array;

Your plan is to use that Array only for "Dogs". Say you add one of the Structs to the array ...

 d.Add( new Dogs() );

In fact, this "doesn't really work" since you cannot then do this:

 d[0].bark = 2.7;

If you try to do that, you get the error that "bark" is not a member of "Object".

It's easy to do this with built-in arrays...or with Lists....

I'm curious how this is done with "Javascript" arrays.

Thank you in advance, Unityscript experts!

Note ........ Javascript arrays are deprecated rubbish!

As Eric has pointed out a number of times, "javascript arrays" are crap and deprecated; don't use 'em. Use "List". Thus, this question is more of a curiosity: is it possible to cast an element of a "javascript array" to a struct as in the example " d[0].bark = 2.7 ". Everything he ses is critically correct. Do exactly what he ses below.

Note ........and Javascript arrays are 10x slower than Lists!

Eric has also very usefully pointed out that Lists are roughly 10x faster than the old, deprecated Javascript arrays! Use only Lists! Everything he ses is critically correct. Do exactly what he ses below.

Cheers and merry xmas ...

Comment
Add comment · Show 6
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 jahroy · Dec 23, 2011 at 07:58 PM 0
Share

If it's easy to do with a built-in array or list, that's a great reason to use either a built-in array or a list.

Also, you're using a class (not a struct).

avatar image Eric5h5 · Dec 23, 2011 at 09:04 PM 0
Share

@jahroy: A class that extends System.ValueType is a struct. Unityscript doesn't have the "struct" keyword.

avatar image jahroy · Dec 23, 2011 at 09:15 PM 0
Share

I didn't notice (or know about) the System.ValueType class. Thanks, that's awesome to know!

avatar image Eric5h5 · Dec 24, 2011 at 10:06 AM 0
Share

The reason I didn't mention "as" for casting is because you're using a struct, and "as" only works with reference types, whereas a struct is a value type. Unityscript doesn't have the () method of casting, such as var dog = (Dog)d[0];.

avatar image Eric5h5 · Dec 24, 2011 at 10:37 AM 0
Share

Well, you can do

 #pragma downcast
 
 var arr = new Array();
 arr.Add(new Dog());
 var dog : Dog = arr[0];

The #pragma downcast is to get rid of the implicit downcast warning you get otherwise. While all this may be a bit interesting as an intellectual exercise, the correct answer is still "there is no reason to torture yourself like this, just use List". ;)

avatar image Fattie · Dec 24, 2011 at 12:14 PM 0
Share

Hmm, that doesn't appear to do it either:

 #pragma downcast
 class Teste extends System.ValueType
     {
     var i:int;
     var f:float;
     }
 function Awake()
     {
     var r:Array;
     r = new Array();
     
     r.Add( new Teste() );
     r.Add( new Teste() );
     r.Add( new Teste() );
     
     r[0].i = 7; ... does throw an error here, even with the pragma
     }

Nevertheless, thanks for telling about "pragma downcast" !

3 Replies

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

Answer by Eric5h5 · Dec 23, 2011 at 04:45 PM

Say you have a "Javascript" array ...

Well, that's your problem: don't use Javascript arrays. Use List.

 import System.Collections.Generic;
 private var d : List.<Dogs>;

 function Start () {
     d = new List.<Dogs>();
     var dog = new Dogs();
     dog.bark = 2.7;
     d.Add(dog);
 }
Comment
Add comment · Show 6 · 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 jahroy · Dec 23, 2011 at 07:36 PM 2
Share

Not sure what you don't understand about Eric's answer...

Javascript arrays apparently don't work like you want them to and should not be used in the situation you've described.

Just because I want to use an anvil as a raft, it doesn't mean Eric can explain how.

avatar image jahroy · Dec 23, 2011 at 08:05 PM 0
Share

If you won't listen to me, listen to Eric (he's very knowledgeable and has lots of karma).

You can't do what you want to do.

That's why he suggests using a List.

Why are you so interested in using dynamic arrays? Nobody uses them and they're not suitable for your needs.

Just forget they ever existed...

If you $$anonymous$$UST use them and want to manually cast each item you want to modify, my answer explains how.

Believe me, it works. I've actually tried it. Have you?

We're going out of our way to help you.

avatar image Eric5h5 · Dec 23, 2011 at 09:03 PM 0
Share

I can so explain how to use an anvil as a raft! See, you just...um. O$$anonymous$$, what you do is--you just--I'll get back to you.

Anyway, yeah, there's absolutely nothing that you can do with a Javascript array that you can't do with List, plus Lists work better, so there's no reason to use a Javascript array. It's a holdover from the early days of Unity, when there was more of an attempt to make Unityscript "like Javascript", and I imagine it's only there now for backwards compatibility. The only reason you would use it is if you didn't know any better (and the docs don't exactly explain much about $$anonymous$$ono and the options for using List and so on, or at least not yet, so it's unfortunately pretty reasonable that someone new to Unity wouldn't know better).

avatar image Eric5h5 · Dec 24, 2011 at 10:40 AM 0
Share

There's no reason to avoid List. Nobody reading this compellingly needs to use Javascript arrays though. There are plenty of compelling reasons to use List; I use it quite a bit.

avatar image Eric5h5 · Dec 24, 2011 at 12:46 PM 0
Share

No, not at all; Array is much slower than List and always has been. It has to box/unbox everything when accessing it, whereas List is statically typed. For example:

 var foo = new Array();
 for (var i = 0; i < 1000; i++) {
     foo.Add(5);
 }
 for (i = 0; i < 10000; i++) {
     for (var j = 0; j < 1000; j++) {
         var bar : int = foo[j];
         foo[j] = bar+2;
     }
 }

Time that, then replace the array declaration with "`var foo = new List.();`" and time it again. On my computer, List is 7.8X faster than Array in this example. (If you use a fixed-size int[] array ins$$anonymous$$d, then it's fully 35X faster than Array and 4.6X faster than List.)

Show more comments
avatar image
1

Answer by Statement · Jan 16, 2012 at 10:08 AM

I don't understand why you want to use the arrays but here goes...

 #pragma strict
 private var d : Array = new Array();
 function Start() {
     d.Add( new Dogs() );

     // d[0].bark = 2.7    
     var temp : Dogs = d[0];
     temp.bark = 2.7;
     d[0] = temp;
 }
Comment
Add comment · Show 1 · 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 Bunny83 · Jan 16, 2012 at 11:46 AM 0
Share

Yep, that's how it would work ;) Since it's a value-type you can't treat it like a reference type. By using d[0] it returns a copy of the value type. If you change the member bark it changed it only in the copy, but not in the original variable stored in the Array. The only way is to use a temp variable.

+1

avatar image
0

Answer by jahroy · Dec 24, 2011 at 07:09 AM

Okay... I'm ready to admit that I was wrong again.

Because I am so committed to helping Fattie find his answer, I decided to do some searching.

I actually found an answer pretty quickly..

You can cast an object inline with javascript like this:

var theArray : Array = new Array();

 var theDog : Dog = new Dog(3.4, "Fido");

 theArray.Push(theDog);

 (theArray[0] as Dog).bark = 55.0;

 var dogTwo : Dog = theArray[0]; 

 print(dogTwo.bark);

 class Dog
 {
     var bark : float = 2.7;
     var name : String = "Fred";

     function Dog ( b : float, n : String )
     {
         bark = b;
         name = n;
     }
 }

 Debug.Log("the bark: " + dogTwo.bark);

I tested it and it worked.

Sorry if I was over-adamant that this wasn't possible. I really didn't think it was.

I guess I never felt the need to keep hunting for this because the other way has always served my needs pretty well.

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 · Dec 24, 2011 at 10:07 AM 0
Share

Actually you weren't wrong. Using "as" for casting only works with classes, not structs.

avatar image jahroy · Dec 24, 2011 at 01:51 PM 0
Share

Oh yeah... forgot we were talking about structs as I posted that answer at 2:00 am!

I know this thread makes it almost impossible to believe, but I do understand the difference between structs and classes. I just got excited late at night and posted (completely forgetting the question was about js arrays, casting, AND structs).

Thanks for the info guys!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a List Constructor? 1 Answer

Can I convert unityscript to boo? 0 Answers

Dynamically load UnityScript file 1 Answer

Javascript 2d array Help 0 Answers

If you destroy a List of Class objects... 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