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 Polinator · May 30, 2017 at 07:18 PM · javascriptarrayslistsextendsubclassing

Converting JS Array to Generic List

At the moment, due to legacy code, I have used JS arrays to build an array of subclasses. This is an example:

Superclass: Animal

Subclasses: Dog, Cat, Turtle

    var Animals : Array;
     
    class Animal{
         var legs : int; //var shared between animals
    }
     
    class Dog extends Animal{
         var bonesGathered : int; //var unique to Dog
    }
    class Cat extends Animal{
         var whiskers : int; //var unique to Cat
    }
    class Turtle extends Animal{
         var age : int; //var unique to Turtle
    }

With JS Arrays, I can add the subclasses to the same array:

    Animals.Push(new Dog());
    Animals.Push(new Cat());
    Animals.Push(new Turtle();

I'm trying to create better coding practices so I've read that JS arrays are typically poor design. I want to use Lists instead.

 var Animals : List.<Animal>;
 
 Animals.Add(new Dog());
 Animals.Add(new Cat());
 Animals.Add(new Turtle());

With Arrays, I was able to do

 Animals[0].legs = 4;
 Animals[0].bonesGathered= 12;
 Animals[1].whiskers = 4;
 Animals[2].age = 202;

How would I go about doing this with Lists? Attempting to do the same thing gives me an error of "bonesGathered" is not a member of "Animal". If it's not possible to keep them in the same array/list should I just stick with the Array? I need them to be in the same group, as I have a large portion of code that references this original JS Array. As I understand it currently the issue is because the compiler doesn't know the type at runtime, so there is an error when trying to change a subclass value when the item in the array/list is not of the same subclass. I'm not too sure as to why this is okay for an Array to determine the type at runtime and not for a List.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ShadyProductions · May 30, 2017 at 08:23 PM

use var Animals : List.<T>;

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 Polinator · May 30, 2017 at 10:17 PM 0
Share

I am using that, I thought, with this:

  var Animals : List.<Animal>;
  
  Animals.Add(new Dog());
  Animals.Add(new Cat());
  Animals.Add(new Turtle());

And like I said Animals[0].bonesGathered does not work. Do you $$anonymous$$d explaining what I did incorrectly?

avatar image
0

Answer by Bunny83 · May 30, 2017 at 11:06 PM

That's not possible. The only reason it is possible in UnityScript is because it allows dynamic typing which is extremely slow. When you use static typing you have to know the type in order to access class specific members.

In your example case the best solution would be to either implement a proper constructor or use a class initializer when you create the classes. I'm not sure if UnityScript actually supports class initializers but constructors will work

 class Cat extends Animal{
     var whiskers : int; //var unique to Cat
     function Cat(aWhiskers : int)
     {
         whiskers = aWhiskers;
     }
 }
 
 // [ ... ]
 Animals.Add(new Cat(4));

Comment
Add comment · Show 7 · 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 Polinator · May 30, 2017 at 11:44 PM 0
Share

Yes, constuctors work fine, I think. But if I wanted to read the amount of whiskers on the cat:

 print("Animals[0].whiskers");

This wouldn't work, as "'whiskers' is not a member of 'Animals'". At least that is what my compiler says. Are you stating that this would be impossible to get from the Animals List? Hopefully I'm not confusing you too much, I really appreciate your help.

avatar image ShadyProductions Polinator · May 31, 2017 at 06:51 AM 0
Share

Consider C# since it is much less limited :P

avatar image Polinator ShadyProductions · May 31, 2017 at 03:48 PM 0
Share

Sorry but this project is very old and was written entirely in UnityScript. I am just looking for a way to make it more efficient without spending weeks rewriting all the scripts to C#. If there is no way to achieve this goal in UnityScript I am fine with leaving it the way it is, as it functions properly at runtime. $$anonymous$$y issue was that the compiler takes a long time since upgrading to Unity 5 and I suspected dynamic typing was the reason.

avatar image bobisgod234 Polinator · Jun 02, 2017 at 03:40 AM 0
Share

If you absolutely know that the animal at 0 is a cat, you can typecast it to a cat:

 (Animals[0] as Cat).whiskers

Though I wonder, how do you absolutely know what type of animal is at what index?

avatar image Polinator bobisgod234 · Jun 02, 2017 at 09:22 PM 0
Share

Hey Bob,

Thanks for this! I didn't know I could typecast after the array like that. It definitely helped.

I absolutely know what type of animal it is at the index because of a switch statement looking for a variable of "animalType : String" in the Animal superclass. This was just an example piece of code as I didn't want to reveal my actual code but it worked the same nonetheless. Do you think I'm doing anything too inefficient from what you've noticed? I've actually managed to shave off roughly 8 seconds of compile time from this List issue scattered throughout my project. In total I had a 45 second compile time, now it's down to ~28. In Unity 4 it was only 12-13 seconds, without optimization.

Show more comments
avatar image Polinator · Jun 01, 2017 at 09:43 PM 0
Share

Also, after using

 Animals.Add(new Cat(4));

Would I be able to get the variable of whiskers from that List? Something like

 var firstCatWhiskers : int = Animals[0].whiskers; 

I'm assu$$anonymous$$g this isn't possible since you said that I can't even set the value, so getting it would lead to the same reasoning?

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

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

How do I create and loop through a completely generic list in javascript? 1 Answer

Add GameObjects to a list using OnTriggerEnter 0 Answers

How to modify array values? 1 Answer

Arrays Progressive Difficulty 0 Answers

Organizing variables in the inspector 5 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