- Home /
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 );
}
}
}
You may want to repost/re-format your code. Not many are going to look at it like that so spaced out.
It's only really two lines that need looked at: "var inventory : Array;" to C#, and "var newItem = new InventoryItem();" to C#.
Answer by Peter G · May 26, 2011 at 08:05 PM
They are implicitly creating an array of arrays, so you have a few options:
A jagged array:
InventoryItem[][] inventory;
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;
A list of lists:
List<List<InventoryItem>> inventory = new List< List<InventoryItem>> ();
A list of Arrays. This is a possibility, but it would be a bizarre solution for your case.
List< InventoryItem[] > inventory = new List< InventoryItem[] > ();
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.
Answer by testure · May 26, 2011 at 08:01 PM
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:
@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);
}
Answer by ThunderAwesome · Sep 03, 2012 at 12:45 AM
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);
}
}
Thank you so much Thunder, saved me a lot of time ! you are awesome !
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
java to C# 2 Answers
convert c# to JavaScript Problem 0 Answers
this script in Javascript?? 0 Answers
Convert c# to Js? 1 Answer