• 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
Question by Santa · Jul 22, 2010 at 02:56 PM · javascriptstruct

Struct in JavaScript

Hello,

Is there a way to make a structure in JavaScript? I mean not class but structure as Vector3 for example. For creating big fast arrays. Something like this:

struct SomeStruct{ var i : int; var f : float; }

var arr = new SomeStruct[1000]; arr[500].a = 3;

In a case of class I need to initialize every member of the array...

Comment
Jean-Fabre
Senhor de todo o Mal
Flynn
nicloay
eelstork
Bunny83

People who like this

6 Show 0
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

4 Replies

  • Sort: 
avatar image
Best Answer

Answer by Senhor de todo o Mal · Jan 24, 2011 at 10:37 AM

A struct is basically a value type so you can try something like this:

 class SomeStruct extends System.ValueType{
   var i : int;
   var f : float;
 
   public function SomeStruct(i:int,f:float){
      this.i = i;
      this.f = f;
   }
 }   

        
Comment
Jean-Fabre
Eric5h5
Santa
Mike 3
runevision
DavidDebnar
Fattie
flamy
nicloay
Vonni
scipiothegreat
eelstork
Bunny83
RodrigoSeVeN

People who like this

14 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 Fattie · Dec 23, 2011 at 11:14 AM 0
Share

One of the best answers ever posted on this site! Thanks.

Just for the record, it appears you CANNOT (as of 2011) make a default constructor.

 public function SomeStruct(i:int,f:float)
 // this is the constructor, if you need one
     {
     this.i = i;
     this.f = f;
     }
 function SomeStruct()
 // would be a default constructor, BUT YOU CANNOT DO THIS!  :)
     {
     this.i = 99;
     this.f = 3.14159265;
     }

If you try the latter, you get the error: "Value types cannot declare parameter-less constructors". So in answer to Santa's question below you will always simply get the normal default values for the members. (Of course, you could make some sort of initialiser which you could run as needed.) Further in answer to Santa's question, yes you can make lovely javascript arrays of these "structs" as in any normal language.

 private var stuff:SomeStruct[];

and then

 stuff = new SomeStruct[42000];

end then

 stuff[28033].i = 6;
 height = stuff[197].f * 99.123;

All of this, thanks to @Senhor !! Merry Xmas.

avatar image

Answer by Mike 3 · Jul 22, 2010 at 02:57 PM

Turns out it is possible (Thanks to Senhor de todo o Mal for pointing that out)

class YourStruct extends System.ValueType
{
    //code
}

Comment
Julian-Glenn
MikezNesh
Santa
Jean-Fabre
DavidDebnar
Fattie
Flynn
eelstork

People who like this

6 Show 3 · 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 Santa · Jul 22, 2010 at 04:54 PM 0
Share

Strange, but if I've placed the structure separately into "Standard Assets" folder. And compiler gives an error "The name 'SomeStruct' does not detonate a valid type'. As if it not present at all. How to compile it earlier other way? Or some way to include or import it?

avatar image Mike 3 · Jul 22, 2010 at 05:34 PM 2
Share

That's exactly how it should work, assuming your javascript file isn't in another earlier compile folder. You should however make your struct public, but it shouldn't give you that error for that

avatar image Santa · Jul 22, 2010 at 05:48 PM 0
Share

With "public" it works :) Thank you!

avatar image

Answer by Ippokratis · Feb 02, 2011 at 11:28 AM

Building on the example given by Senhor de todo o Mal. Save the following script in your Standard Assets folder:

class SomeStruct extends System.ValueType{

//fields var i : int; var f : float;

//Constructor public function SomeStruct(i:int,f:float){ this.i = i; this.f = f; } //Method public function PowerOfI(a:int) {

 for (var index:int = 1; index<a; ++index)
 {
     this.i = this.i * this.i;
 }

}

}

Now create a new js script as follows:

function Start () {
    //Make a new SomeStruct with uninitialized fields
    var myStruct:SomeStruct = new SomeStruct();
    //Set field i
    myStruct.i =5;
    //Test it
    Debug.Log(myStruct.i);
    //Make a new SomeStruct with initialized fields
    var myStruct2:SomeStruct = new SomeStruct(5, 2.3);
    //Test it
    Debug.Log(myStruct2.f);
    //Use a struct method
    myStruct2.PowerOfI(2);
    //Test it
    Debug.Log(myStruct2.i);
}

So yes, you can create structures in Unity JavaScript.

Comment
Santa

People who like this

1 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 Santa · Feb 02, 2011 at 08:39 PM 0
Share

But what if I create huge array without initialization of each item with "new"? Will I be able to access their members ".i" or ".f"? As far as I understand it's still pointers. Or not? PS: Can't check it out quickly by myself because I moved all scripts in C# long ago.

avatar image Senhor de todo o Mal · Feb 03, 2011 at 10:57 AM 0
Share

Its basically an array of value types so each item will have the default value of that value type. For example, int will be 0, float will be 0.0f and boolean will be false.

avatar image Q_B · Feb 17, 2011 at 05:37 PM 0
Share

Great info you guys got here, thx! Unfortunately, when trying to declare an array of structs created like this, they won't show up on the editor. For instance, if I put a " var w : float[] ", it shows on the editor allright. But if I put a " var w : mystruct[] ", it does not reports errors, but it doesn't shows on the editor :( Any idea how to solve this, if it's even possible?

avatar image Ippokratis · Feb 20, 2011 at 10:49 AM 0
Share

Senhor de todo o Mal did it single handed. I was just too excited it worked and I wanted to post it ! No code formating was available in comments so I posted it as answer. So to be clear: All kudos go to Senhor. Regarding your question : Try to set the Inspector in Debug mode (from the drop-down menu on the upper right corner).

avatar image

Answer by kblood · Apr 26, 2013 at 07:25 PM

This has annoyed me for days. Doing it the way Senhor de todo o Mal did it does not make the struct show up in the editor. Then I found this in another answer:

http://docs.unity3d.com/Documentation/ScriptReference/Serializable.html

 class Test extends System.Object {
     var p = 5;
     var c = Color.white;
 }
 var test = Test ();

And now it shows up in the editor as well. I hope it saves others some time.

Comment
Eric5h5

People who like this

-1 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 Fattie · Apr 26, 2013 at 07:32 PM 1
Share

be super-careful with this gotchya !

http://answers.unity3d.com/questions/261419/you-own-structs-passed-by-reference.html

what you are doing there IS NOT a struct, it's just a class. Totally different eh!! Take care!

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

custom struct not showing up in inspector? 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

UnityScript struct causes unity to crash 1 Answer

Errors in script cant figure out?? 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