4 Replies - 1076 Views - Last Post: 21 March 2013 - 03:55 AM Rate Topic: -----

#1 TrewSx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 07-June 12

Add an item to my inventory - UnityScript / JS

Posted 20 March 2013 - 10:30 AM

Hello i got stuck on inventory script and i really could use some help here. I have three scripts one for loot, second for inventory and third for chest, When i open a chest it will generate a random loot and i want to be able to click on item in this loot window and this item will be transferred to my inventory. I just cant make it work, i managed to remove the item from loot window after i click on it but not move it into my inventory.
I am using UnityScript, could anyone help me please

#pragma strict
//Inventory script 


//Inspector var
public var mySkin					:	GUISkin;

public var inventoryWindowHeight	:	float = 90;

public var buttonWidth				:	float = 40;
public var buttonHeight				:	float = 40;

public var closeButtonWidth			:	float = 20;
public var closeButtonHeight		:	float = 20;
public var spacing 					:   float = 0.1; 																			//Space between slots (in x and y)
public var emptyButtonTexture		:   Texture2D;																			    //This will be drawn when a slot is empty

//Private var
private var _displayInventory	    : boolean = false;
private var _inventoryCols			:   int	  = 15;
private var _inventoryRows			:   int   = 4;	
private var _inventoryWindowRect	:    Rect = new Rect(10,10,Screen.height / 2 + 120,Screen.width / 2 - 200);

private var _offSet 						  = Vector2( 13, 175);


//Static var
private static var _inventoryWindowID		:   int   = 0;


function Start () {

}

function OnGUI() {
	var e = Event.current;
	
	if (e.type == EventType.KeyDown && e.keyCode == KeyCode.I)																//Check the Event.current.type if the current event is a EventType.KeyDown event, and if so, if it is KeyCode.I
    	 ToggleInventoryWindow();  																							//If 'Yes' turn inventory on/off	
    	  
	 if (_displayInventory)
        _inventoryWindowRect = GUI.Window(_inventoryWindowID, _inventoryWindowRect, InventoryWindow, " Inventory ");   		//Draw inventory
	
	
}

function InventoryWindow(_inventoryWindowID) {								
	var texToUse 	: Texture;
	var		cnt 	: int = 0;	
		
		for(var y = 0; y < _inventoryRows; y++) {
			
			for(var x = 0; x < _inventoryCols; x++) { 
				
				if(cnt < (x + (y  * _inventoryCols))) {																		//if our counter is less then number of items in our inventory
					//Draw a button for an item in inventory
					GUI.Button( Rect(_offSet.x + (x * (buttonWidth + spacing)), _offSet.y + (y * (buttonHeight + spacing)), buttonWidth, buttonHeight), (x + (y  * _inventoryCols)).ToString());
				}
				else {
					//Draw a box if slot is empty 
					GUI.Label( Rect(_offSet.x + (x * (buttonWidth + spacing)), _offSet.y + (y * (buttonHeight + spacing)), buttonWidth, buttonHeight), (x + (y  * _inventoryCols)).ToString(), "box");

				}
				
				cnt++;
			}
				
		}	
	GUI.DragWindow();
}

function ToggleInventoryWindow () {
		_displayInventory = ! _displayInventory;

}

function AddItem(Item : GameObject){


	Debug.Log(Item.name + " Has been added to inventory");
	

}






Loot
#pragma strict
//Loot window script

//Inspector variables
public var buttonWidth						:	float = 40;
public var buttonHeight						:	float = 40;
public var closeButtonWidth					:   float = 20;
public var closeButtonHeight				:   float = 20;


//Loot variables
public var lootWindowHeight					:	float = 90;
public var itemsListArray					:   Array = [10];

private var _displayLootWindow				: boolean = false;
private var _lootWindowRect					:    Rect = new Rect(0,0,0,0);
private static var _lootWindowID			:   int   = 1;									//ID number has to be Unique for each window
private var _lootWindowOffSet	    		:   int   = 10;
private var _lootWindowSlider				: Vector2 = Vector2.zero;						//var to hold the position of this slider

function	initialiseLoot() {
	_displayLootWindow = true;
	
	itemsListArray = new Array ();
	
	Populate();
}

function OnGUI() {
	 if(_displayLootWindow) {
	 	//Display Loot window on a screen
		_lootWindowRect = GUI.Window(_lootWindowID, new Rect(_lootWindowOffSet, Screen.height - (_lootWindowOffSet + lootWindowHeight), Screen.width - (_lootWindowOffSet * 2),lootWindowHeight), LootWindow, "Loot Window");
	 }
}

private function LootWindow(_lootWindowID) {//Start LootWindow								
	//Close button
	if(GUI.Button(new Rect(_lootWindowRect.width - 20, 0, closeButtonWidth, closeButtonHeight), "x")) {
		_displayLootWindow = false;
	}
	
	//Slider
	_lootWindowSlider = GUI.BeginScrollView(new Rect(_lootWindowOffSet * .5f, 15, _lootWindowRect.width - 10, 70), _lootWindowSlider, new Rect(0, 0, (itemsListArray.Count * buttonWidth) + _lootWindowOffSet, buttonHeight + _lootWindowOffSet));
	
	//Go through itemsListArray from Populate function
	for(var _cnt : int = 0; _cnt < itemsListArray.Count; _cnt++) {
	   //print("LootWindow - for loop : " + _cnt);
	   
	   //and create that many buttons
	   if(GUI.Button(new Rect(5 + (buttonWidth * _cnt), 5, buttonWidth, buttonHeight), itemsListArray[_cnt].ToString())) {
	   	//print("clicked on is " + itemsListArray[_cnt]);
	   	
	   	itemsListArray.RemoveAt(_cnt);													  //Remove clicked item in LootWindow																					
	   
	   }
	}
	   GUI.EndScrollView ();
}//End LootWindow

private function Populate() {
	
	var		total : int = Mathf.Clamp(Random.value * 10, 4, 10);
	
	for(var _cnt : int = 0; _cnt < total; _cnt++) {
		var		temp : int = Random.value * 24;
		itemsListArray.Add(temp);
		//Debug.Log(_cnt + " -- " + itemsListArray);
	}

}




Is This A Good Question/Topic? 0
  • +

Replies To: Add an item to my inventory - UnityScript / JS

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Add an item to my inventory - UnityScript / JS

Posted 20 March 2013 - 10:49 AM

What do you mean by "it won't work"? How doesn't it work?
Was This Post Helpful? 0
  • +
  • -

#3 TrewSx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 07-June 12

Re: Add an item to my inventory - UnityScript / JS

Posted 20 March 2013 - 11:02 AM

when u click on the item in loot window it does not transfer this item into inventory window
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Add an item to my inventory - UnityScript / JS

Posted 20 March 2013 - 11:44 AM

So have you checked the code that is checking that the button is being clicked is being hit, as well as the code to invoke the inventory window? It's telling you what you need to do to resolve the problem.
Was This Post Helpful? 0
  • +
  • -

#5 TrewSx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 07-June 12

Re: Add an item to my inventory - UnityScript / JS

Posted 21 March 2013 - 03:55 AM

The item in loot window is responding to a hit

[b ]print("clicked on is " + itemsListArray[_cnt]); [/b]

so this part is ok, aslo after hit on button in loot window the item is removed

[b ]itemsListArray.RemoveAt(_cnt); [/b]

i am not sure how to invoke the inventory window. Should i create a static variable in Inventory script and then somehow use this variable in Loot script ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1