x


Help converting this to C# - a few issues

Search for "FIX_ME". I need to know what to do with the var inventory : Array, because only Javascript can use that class. And there's one issue with FIX_ME newItem= new InventoryItem(). I think that might need "InventoryItem" before it, but I'm not sure.

Original Javascipt

//Our inventory array
var inventory : Array;

public var emptyTex : Texture;           //This will be drawn when a slot is empty
public var inventorySizeX = 8;           //the size of the inventory in x and y dimension
public var inventorySizeY = 5;

var iconWidthHeight = 20;            //The pixel size (height and width) of an inventory slot
var spacing = 4;                  //Space between slots (in x and y)

public var offSet = Vector2( 100, 100 ); //set the position of the inventory

// TEST VARIABLES
// Assign these to test adding Items with mouse clicks (see Update())
public var testTex : Texture;
public var testTex2 : Texture;

//Our Representation of an InventoryItem
class InventoryItem
{
    //GameObject this item refers to
    var worldObject : GameObject;
    //What the item will look like in the inventory
    var texRepresentation : Texture;
}

function Awake() 
{ 
    inventory = new Array(inventorySizeX);

    for( var i = 0; i < inventory.length; i ++ ) 
    { 
        inventory[i] = new Array(inventorySizeY);
    } 
} 

function OnGUI() 
{ 
    var texToUse : Texture;
    var currentInventoryItem : InventoryItem;

    //Go through each row 
    for( var i = 0; i < inventory.length; i ++ ) 
    { 
        // and each column 
        for( var k = 0; k < inventory[i].length; k ++ ) 
        { 
            texToUse = emptyTex;
            currentInventoryItem = inventory[i][k];

            //if there is an item in the i-th row and the k-th column, draw it 
            if( inventory[i][k] != null ) 
            { 
                texToUse = currentInventoryItem.texRepresentation;
            } 

            GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), texToUse );
        } 
    } 
} 

function AddItem( item : InventoryItem )
{
     //Go through each row 

    for( var i = 0; i < inventory.length; i ++ ) 
    { 
        // and each column 
        for( var k = 0; k < inventory[i].length; k ++ ) 
        { 
            //If the position is empty, add the new item and exit the function
            if( inventory[i][k] == null ) 
            {
                 inventory[i][k] = item;
                 return;
            }
        }
    }   

    //If we got this far, the inventory is full, do somethign appropriate here  
}

function AddItem( worldObject : GameObject, texRep : Texture )
{
    var newItem = new InventoryItem();
    newItem.worldObject = worldObject;
    newItem.texRepresentation = texRep;

    AddItem( newItem );    
}

function Update()
{
    if( Input.GetMouseButtonDown( 0 ) ) 
    {
       AddItem( gameObject, testTex );   
    }

    if( Input.GetMouseButtonDown( 1 ) )     
    {    
       AddItem( gameObject, testTex2 );  
    }  
}

C#

using UnityEngine;
using System.Collections;
public class InventoryTestCS : MonoBehaviour {

    //Our inventory array
    FIX_ME Array inventory;

    public Texture emptyTex;           //This will be drawn when a slot is empty
    public int inventorySizeX= 8;           //the size of the inventory in x and y dimension
    public int inventorySizeY= 5;

    int iconWidthHeight= 20;                  //The pixel size (height and width) of an inventory slot
    int spacing= 4;                          //Space between slots (in x and y)

    public Vector2 offSet = Vector2 (100, 100); //set the position of the inventory

    // TEST VARIABLES
    // Assign these to test adding Items with mouse clicks (see Update())
    public Texture testTex;
    public Texture testTex2;

    //Our Representation of an InventoryItem
    class InventoryItem
    {
        //GameObject this item refers to
        GameObject worldObject;
        //What the item will look like in the inventory
        Texture texRepresentation;
    }

    // Create the Inventory
    void Awake()
    {
        inventory = new Array(inventorySizeX);
        for (int i= 0; i < inventory.length; i++)
        {
            inventory[i] = new Array(inventorySizeY);
        }
    }

    void OnGUI()
    {
        Texture texToUse;
        InventoryItem currentInventoryItem;
        //Go through each row
        for (int i= 0; i < inventory.length; i++)
        {
            // and each column
            for (int k= 0; k < inventory[i].length; k++)
            {
                texToUse = emptyTex;
                currentInventoryItem = inventory[i][k];
                //if there is an item in the i-th row and the k-th column, draw it
                if (inventory[i][k] != null)
                {
                    texToUse = currentInventoryItem.texRepresentation;
                }
                GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), texToUse );
            }
        }
    }

    void AddItem(InventoryItem item)
    {
        //Go through each row
        for (int i= 0; i < inventory.length; i++)
        {
            // and each column
            for (int k= 0; k < inventory[i].length; k++)
            {
                //If the position is empty, add the new item and exit the function
                if (inventory[i][k] == null)
                {
                    inventory[i][k] = item;
                    return;
                }
            }
        }
        //If we got this far, the inventory is full, do somethign appropriate here
    }

    void AddItem(GameObject worldObject, Texture texRep){
        FIX_ME newItem= new InventoryItem();
        newItem.worldObject = worldObject;
        newItem.texRepresentation = texRep;
        AddItem(newItem);
    }

    void Update()
    {
        if( Input.GetMouseButtonDown( 0 ) )
        {
            AddItem( gameObject, testTex );
        }
        if( Input.GetMouseButtonDown( 1 ) )
        {
            AddItem( gameObject, testTex2 );
        }
    }
}
more ▼

asked May 26 '11 at 07:35 PM

Stardog gravatar image

Stardog
344 30 34 41

You may want to repost/re-format your code. Not many are going to look at it like that so spaced out.

May 26 '11 at 07:37 PM Meltdown

It's only really two lines that need looked at: "var inventory : Array;" to C#, and "var newItem = new InventoryItem();" to C#.

May 26 '11 at 07:42 PM Stardog
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

They are implicitly creating an array of arrays, so you have a few options:

  1. A jagged array:

       InventoryItem[][] inventory;
    
  2. You could use a multi-dim array. This is no longer an array of arrays, but since all the arrays are the same size in the js above, you are essentially creating a box anyway.

       InventoryItem[,] inventory;
    
  3. A list of lists:

       List<List<InventoryItem>> inventory = new List< List<InventoryItem>> ();
    
  4. A list of Arrays. This is a possibility, but it would be a bizarre solution for your case.

       List< InventoryItem[] > inventory = new List< InventoryItem[] > ();
    
  5. Or an Array of lists, same as number 4, probably not the best choice:

       List<InventoryItem>[] inventory;
    

If you using a list then you will have to make some other adjustments to match the syntax of a list. Just looking at the js, it doesn't look like they resize the array except for the beginning so I would choose any of the first three. Numbers 4 and 5 though would be a little confusing for what you are trying to do so I would probably avoid them since there isn't really any reason in your example to switch types so it would just look strange.

And your other line looks ok since type inference should be able to figure out the type from the right hand side of the assignment operator.

more ▼

answered May 26 '11 at 08:05 PM

Peter G gravatar image

Peter G
17.3k 22 51 149

Deserves a good review. Thanks CSDG

May 26 '11 at 08:38 PM UniteMage
(comments are locked)
10|3000 characters needed characters left

in C# you need to strictly type your array- from what I saw, it's an array of type InventoryItem, so you'd define it like:

InventoryItem [] inventory; // this will create an uninitialized InventoryItem array.

Although for something like this that may have dynamic sizes attributed to it, you may want to consider using a list instead. Also, here's a useful read on arrays/lists/hashtables, etc:

http://robotduck.wordpress.com/2009/11/04/88/

more ▼

answered May 26 '11 at 08:01 PM

testure gravatar image

testure
4.6k 21 28 54

@testure they are creating an array of arrays, just not expressing it in the variable declaration:

inventory = new Array(inventorySizeX);

for( var i = 0; i < inventory.length; i ++ ) 
{ 
    inventory[i] = new Array(inventorySizeY);
} 
May 26 '11 at 08:07 PM Peter G
(comments are locked)
10|3000 characters needed characters left

This is a conversion of Der Dude's JavaScript code. I noticed you removed/changed some things in the OnGUI function. But here is his code converted for C#. The only addition I made was the "isInventoryOpen" boolean which you'll have to implement somewhere with a keypress or what have you. (You'll notice the class name is different, InventoryTestCS)

Also...this is a bit clunky. Lists would work best, but As long as you don't edit the size of the inventory in real-time, you should be okay.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class InventoryTestCS : MonoBehaviour {

    //public static InventoryTestCS statInventory;

    public bool isInventoryOpen = false; //my addition (used to show the inventory)

    public Texture emptyTex;           //This will be drawn when a slot is empty
    public const int inventorySizeX= 8;           //the size of the inventory in x and y dimension
    public const int inventorySizeY= 5;

    //Our inventory array
    InventoryItem[,] inventory;

    int iconWidthHeight= 20;                  //The pixel size (height and width) of an inventory slot
    int spacing= 4;                          //Space between slots (in x and y)

    public Vector2 offSet = new Vector2 (100, 100); //set the position of the inventory


    //Our Representation of an InventoryItem
    public class InventoryItem
    {
        //GameObject this item refers to
        public GameObject worldObject;
        //What the item will look like in the inventory
        public Texture texRepresentation;
    }

    // Create the Inventory
    void Awake()
    {
       inventory = new InventoryItem[inventorySizeX, inventorySizeY];
    }

    void OnGUI()
    {
       if(isInventoryOpen)//if the inventory is open display it
       {
         InventoryItem currentInventoryItem;
         //Go through each row
         for (int i= 0; i < inventorySizeX; i++)
         {
          // and each column
          for( int k = 0; k < inventorySizeY; k ++ )
          {
              currentInventoryItem = inventory[i,k];

              //if there is an item in the i-th row and the k-th column, draw it
              if( inventory[i,k] == null )
              {
                 GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), emptyTex );
              }
              else
              {
                 GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), currentInventoryItem.texRepresentation );
              }

              if(currentInventoryItem != null && 
              GUI.Button( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), "", new GUIStyle("label") ))
              {

                 currentInventoryItem.worldObject.transform.position = transform.position;
                 currentInventoryItem.worldObject.transform.rotation = transform.rotation;
                 currentInventoryItem.worldObject.active = true;

                 if(Input.GetMouseButtonUp(0))
                 {     
                   //Equip it
                   currentInventoryItem.worldObject.transform.parent = transform;
                   currentInventoryItem.worldObject.collider.enabled = false;

                 } else if(Input.GetMouseButtonUp(1))
                 {
                   //Drop it
                   inventory[i,k] = null;   
                   currentInventoryItem.worldObject.transform.parent = null;

                 }
              }
          }

         }
       }
    }

    public void AddItem(InventoryItem item)
    {
        //Go through each row
        for (int i= 0; i < inventorySizeX; i++)
        {
            // and each column
            for (int k= 0; k < inventorySizeY; k++)
            {
                //If the position is empty, add the new item and exit the function
                if (inventory[i,k] == null)
                {
                    inventory[i,k] = item;
                    return;
                }
            }
        }
        //If we got this far, the inventory is full, do something appropriate here
    }

    public void AddItem(GameObject worldObject, Texture texRep){
        InventoryItem newItem= new InventoryItem();
        newItem.worldObject = worldObject;
        newItem.texRepresentation = texRep;
        AddItem(newItem);
    }

    void Update()
    {

       if(Input.GetKeyDown("i"))
       {
         isInventoryOpen = !isInventoryOpen;
       }
    }
}

Also, if you are looking for a conversion of the InventoryWorldItem here it is:

using UnityEngine;
using System.Collections;

public class InventoryWorldItem : MonoBehaviour {

    public Texture iconTex;
    public InventoryTestCS inventory;

    void Start()
    {
       //make sure the Player has the inventory script attached otherwise this won't work
       inventory = GameObject.FindWithTag("Player").GetComponent<InventoryTestCS>();
    }

    void OnMouseDown()
    {
       gameObject.active = false;

       InventoryTestCS.InventoryItem item = new InventoryTestCS.InventoryItem();
       item.worldObject = gameObject;
       item.texRepresentation = iconTex;
       inventory.AddItem(item);
    }
}
more ▼

answered Sep 03 '12 at 12:45 AM

ThunderAwesome gravatar image

ThunderAwesome
61 1 4

Thank you so much Thunder, saved me a lot of time ! you are awesome !

Jan 19 '13 at 11:09 PM ElectroSphere
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x7721
x5407
x1888
x322

asked: May 26 '11 at 07:35 PM

Seen: 2119 times

Last Updated: Jan 19 '13 at 11:09 PM