[JS] Using a class with selection grids

Hey all!

I have created a basic class to store the properties of each grid on a selection grid. However, it will not let me link my class to the actual selection grid. Is this possible or do I have to find another way?

I would prefer to not have to link my class to another grid array then onto the screen but if its the only way then I shall.

Code for the class (on the same script):

var Inventory : Grid[] = new Grid[40];

class Grid
{
	var name : String;
	var buy_price : int;
	var icon : Texture2D;
	
	function Grid (na : String, bp : int, ico : Texture2D)
	{
		name = na;
		buy_price = bp;
		icon = ico;
	}
}

Code for the selection grid:

private var GridValue : float = -1;
GridValue = GUI.SelectionGrid(Rect(512, 512, 500, 500), GridValue, Inventory, 5);

Error received:

BCE0023: No appropriate version of 'UnityEngine.GUI.SelectionGrid' for the argument list '(UnityEngine.Rect, float, Grid[], int)' was found.

Another attempt at selection grid:

private var GridValue : float = -1;
GridValue = GUI.SelectionGrid(Rect(512, 512, 500, 500), GridValue, Inventory.icon, 5);

Error received:

BCE0023: No appropriate version of 'UnityEngine.GUI.SelectionGrid' for the argument list '(UnityEngine.Rect, float, Grid[], int)' was found.

It is not letting me get the icon’s because I am not specifying an array number, but if I did it would lose the purpose of storing data in each slot.

Another attempt at selection grid:

private var GridValue : float = -1;
GridValue = GUI.SelectionGrid(Rect(512, 512, 500, 500), GridValue, Inventory[].icon, 5);

Error received:

BCE0044: expecting ), found '['.

BCE0043: Un expected token: ].

UCE0001: ';' expected. Insert a semicolon at the end.

BCE0043: Unexpected token: icon.

BCE0043: Unexpected token: 5.

It is not letting me get the icon’s because I am not specifying an array number, but if I did it would lose the purpose of storing data in each slot.

Please help! thank you!

Working code:

private var GridValue : float = -1;
var grids : Grid[] = new Grid[40];

class Grid
{
    var name : String;
    var buy_price : int;
    var icon : Texture2D;
 
    function Grid (na : String, bp : int, ico : Texture2D)
    {
       name = na;
       buy_price = bp;
       icon = ico;
    }
}

function GetIcons()
{
	var icons : Texture2D[] = new Texture2D[40];
		
	for(var i = 0; i < 40; i++)
	{
		//icons *= null;*

icons = grids*.icon;*
* }*
* return icons;*
}

function OnGUI()
{
* GridValue = GUI.SelectionGrid(Rect(512, 512, 300, 300), GridValue, GetIcons(), 5);*
}

I would suggest that you create a class called Inventory inside of which you have a Grid array (this is also neat considering Object Orientated Programming). Create a function to the class called GetIcons() or something along those lines and make it return all the textures of the Grid objects.

I haven’t really coded UnityScript but this code will probably give you the right idea (I don’t promise it will work though :stuck_out_tongue_winking_eye: ):

class Inventory
{
    var grids: Grid[] = new Grid[40];
 
   function GetIcons() : Texture2D[]
   {
      var icons : Texture2D[40];
      for(var i = 0; i < 40; i++)
      {
          icons _= grids*.icon;*_

}
return icons;
}
}
And now you can just create an inventory and use the function in the SelectionGrid:

GridValue = GUI.SelectionGrid(Rect(512, 512, 500, 500), GridValue, inventory.GetIcons(), 5);