Hi, I need to create a data structure that can hold multiple different types of variables, i then need to make an array using this structure. However I am new to Javascript. basically what I want to do is the following C++ code but in java script.
struct ActionBar
{
Button Buttons[6];
}
struct Button
{
Rect Button;
Texture2D Icon;
bool onCoolDown;
float CDLeft;
}
public int main()
{
Texture2D ButtonIcon;
ActionBar ActionBars[2]
for(int i = 0; i <= 2; i++)
{
for(int j = 0; j <= 6; j++)
{
ActionBars[i].Buttons[j].Button = new Rect(10,10,10,10);
ActionBars[i]Buttons[j].Icon = ButtonIcon;
ActionBars[i].Buttons[j].onCoolDown = false;
ActionBars[i].Buttons[j].CDLeft = 4.0f;
{
{
}
Hope you can see what i am getting at, i know that java script has no function to create structs but there is surely a way to create something similar or get around the lack of functionality. Thanks in advance.
You can extend System.ValueType to make a struct.
class Button extends System.ValueType
However Button shouldn’t be a struct, it should be a class.
–Eric
perfect, thanks for quick response. Pretty stupid not to think of using a class instead of a struct 
here is the class i made
class Buttons extends System.ValueType
{
var Button : Rect;
var SpellID : int;
var SpellImage : Texture2D;
var OnCoolDown : boolean;
var GlobalCoolDown : boolean;
var CoolDownLeft : float;
var GlobalCoolDownLeft : float;
function GetButton()
{
return Button;
}
function GetSpellID()
{
return SpellID;
}
function GetSpellImage()
{
return SpellImage;
}
function GetOnCoolDown()
{
return OnCoolDown;
}
function GetGlobalCoolDown()
{
return GlobalCoolDown;
}
function GetCoolDownLeft()
{
return CoolDownLeft;
}
function GetGlobalCoolDownLeft()
{
return GlobalCoolDownLeft;
}
}
Here is how i declare an array and assign its elements to use the class i defined
var ActButtons = new Array(5);
*
*
*
var X = 77;
var Y = VerticleResolution - 64 - 38;
var xAdd = 94;
switch (PlayerStatusInfo.CharacterClass)
{
case "Mage":
for (i = 0; i <= 5; i++)
{
ActButtons[i] = Buttons;
ActButtons[i].Button = new Rect(X + PlayerHUDInfo.ActionBarOffset.x, Y + PlayerHUDInfo.ActionBarOffset.y, 64, 64);
ActButtons[i].SpellImage = SpellInfo.GetMageSpellIcon(i);
ActButtons[i].SpellID = ClassInfo.GetMageSpellID(i);
ActButtons[i].OnCoolDown = false;
ActButtons[i].GlobalCoolDown = false;
ActButtons[i].CoolDownLeft = 0.0f;
ActButtons[i].GlobalCoolDownLeft = 0.0f;
X = X + xAdd;
}
break;
}
For Some reason when i use this line of code:
if (GUI.Button(ActionButtonsInfo.ActButtons[i].GetButton(), ActionButtonsInfo.ActButtons[i].GetSpellImage()))
{
SpellInfo.CastSpell(ActionButtonsInfo.ActButtons[i].GetSpellID());
}
i get error “InvalidCastException: Cannot cast from source type to destination type.
(wrapper dynamic-method) Buttons.Buttons$GetButton$ (object,object[ ]) <IL 0x00001, 0x00035>
Boo.Lang.Runtime.RuntimeServices.Invoke (object,string,object[ ]) <IL 0x0004c, 0x00125>
PlayerHUD.DrawActionBar () (at Assets/Custom Assets/Scripts/PlayerHUD.js:83)
PlayerHUD.OnGUI () (at Assets/Custom Assets/Scripts/PlayerHUD.js:56)”
also i get a warning that my array is not used, i think i am doing something wrong when it comes to assigning the element of the array to hold values dictated by the Buttons class
Never use the Array class, which is slow and untyped (which causes problems like you encountered). Use List instead, or built-in arrays if the size of the array doesn’t need to change.
Also, as I mentioned, it should be a class and not a struct. You have a Texture2D in there for one thing. Also there’s not really any point to the GetButton, GetSpellID, etc. functions, since the variables are all public.
–Eric
Sorry for the the "ActButtons = new Buttons();