- Home /
javascript objects/json question
i want to create an array that contains simple objects. but i can't figure out how to create simple objects.
if this was actionscript i'd do this: var spot:Object = new Object(); spot.open = true; spot.position = new Vector2(x,y);
or normally in javascript i'd just do this: var spot = {open:true, position: new Vector2(x,y)};
how do i do this in unity's javascript?
i was able to work around this using Hashtable ins$$anonymous$$d. spot["open"] is just as useful as spot.open for what i'm doing. as long as hashtables can contain child hashtables i should be set. still... foo.bar.lunch is nicer to type than foo["bar"]["lunch"]
Answer by by0log1c · Jan 06, 2012 at 06:12 AM
The Object class you're creating is - AFAIK - nothing but a base class to extends from, useful as a common base for pretty much every Unity objects but nothing in itself. What you want to do is probably create your own class that derives from it and add the variables you need to the new class/type. Then create an object of MyClass instead of Object. Using JS, its as simple as adding:
 public class MyClass
 {
     public var myString:String = "";
     public var myInt:int = "";
 
     //custom constructor
     public MyClass(someInt:int)
     {
         myString = "default";
         myInt = someInt;
     }
 }
 
 var myObject:MyClass = new MyClass(1337);
 myObject.myString = "neat";
You might want to take a look at Newbie Javascript Guide as UnityScript is not quite true JavaScript.
this is what i ended up using. it works for what i'm trying to do. i wanted a simple object that i could dynamicly add properties to. though i can see this evolving into a class as i flesh out the game.
 spots = new Array ();
 var r = 0;
 var c = 0;
 for(var i = 0; i < 9; i++){
     var spot = new Hashtable(); 
     spot["open"] = true;
     spot["position"] = new Vector2(c*110-150, r*110-150);
     c++;
     if(c >= 3){
         c = 0;
         r++;
     }
     spots.push(spot);
 }
Your answer
 
 
             Follow this Question
Related Questions
How do i best describe an object with modular components in Json or other text? 1 Answer
Accessing JSON in unityscript 0 Answers
Editing avatars using node.js 0 Answers
Application.ExternalCall Issue 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                