Ok, well here is the script.
DISCLAIMER: Users may only use this code as a learning device. This work shall not be used in any commercial or non-profit product, unless given permission by the license owner (Shadonyx Entertainment). Violation of these terms may be subject to lawsuit. This is not meant to be a finished product.
Don’t plagiarize! That means no copying and saying you made it! Don’t do anything illegal! Oh, and don’t give this to random people! Just to be safe, it’s pretty much for your eyes only Nercoe. I hope other users have some discretion and decency. For some reason, I have a feeling that this will be used/plagiarize by hundreds of new users. -_-
If you avoid all that, go ahead!
There’s a few things that you should keep in mind (other than possible legal issues). This script is placed on an empty game object. Items are other game objects (with an Item script, but for this example that is irrelevant). Items that are place inside the inventory become children to the inventory and become deactivated. Vice-versa happens when you drop an item.
//Copyright 2013, Shadonyx Entertainment
//Written by Jordan Abrahams
//Strictly for learing use only! Do not plagiarize!
#pragma strict
class Inventory extends MonoBehaviour {
var slots = new GameObject[20];
var rows : int = 5;
var collumns : int = 4;
var totalWeight : float = 0.0;
var gold : int = 0;
private var player : GameObject;
function Start () {
player = GameObject.FindWithTag("Player");
}
function AddItem (obj : GameObject) {
var item : Item = obj.GetComponent.<Item>();
for(var i : int = 0; i + 1 <= slots.length; i++) {
if(item.stack && slots*) {*
_ if(slots*.name == obj.name) {_
_ slots.GetComponent.().stackCounts += item.stackCounts;
Destroy(obj);
break;
}
}
if(!slots) {
slots = obj;
totalWeight += item.weight;
obj.transform.parent = transform;
obj.SetActive(false);*_
* var abilityArray = new Array(player.GetComponent(AbilityCaster).abilities);*
* abilityArray.Add(item.ability);*
* player.GetComponent(AbilityCaster).abilities = abilityArray;*
* break;*
* }*
* else if(i + 1 >= slots.length) {*
* Debug.Log(“Your inventory is full. Drop something to open a slot.”);*
* break;*
* }*
* }*
* }*
* function DropItem (obj : GameObject) {*
* for(var i : GameObject in slots) {*
* if(i == obj) {*
* var itemScript = i.GetComponent(Item);*
* var icon = itemScript.icon;*
* var trans = player.transform;*
* var pos = trans.position;*
* i.transform.parent = null;*
* i.transform.position = pos + trans.TransformDirection(Vector3(0, 0.5, 0.5));*
* i.SetActive(true);*
* totalWeight -= itemScript.weight;*
* if(icon) {*
* Destroy(icon);*
* }*
* i.SetActive(true);*
* i = null;*
* }*
* }*
* }*
* function MoveItem (from : int, to : int) {*
* if(slots[from]) {*
* if(slots[to]) {*
* var switchedSlot : GameObject = slots[to];*
* slots[to] = slots[from];*
* slots[from] = switchedSlot;*
* }*
* else {*
* slots[to] = slots[from];*
* slots[from] = null;*
* }*
* }*
* else {*
* Debug.LogWarning("Attempting to move a missing item; there is no item at slot: " + from);*
* }*
* }*
* function ActivateItem (item : GameObject) {*
* for(var i : GameObject in slots) {*
* if(i == item) {*
* i.SendMessage(“Activate”);*
* }*
* }*
* }*
}
I mentioned before, please remember that this is not a finished product. It is also not guaranteed to be bug free, nor to be the most efficient way. I spent many long hours working on my inventory system. Think of this as a act of empathy.