i have this code, only the part at the top and the Awake function are relevant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace CoS
{
public class heroInventory : MonoBehaviour
{
public Canvas hinventorycanvas;
public int invcapacity = 50;
//public Item[] inventory;
public ItemHolder[] inventory;
public Canvas hinvequippedcanvas;
public GameObject hero;
public GameObject grounditemprefab;
private bool inventoryflag = false;
// Use this for initialization
void Awake()
{
bool inventoryflag = false;
hinventorycanvas.enabled = false;
hinvequippedcanvas.enabled = false;
ItemHolder[] inventory = new ItemHolder[invcapacity];
//grounditemprefab = new GameObject();
// going to have to refactor this later. can't re-initialize inventory at the beginning of every scene!
//doesn't work anyway
for (int i = 0; i < invcapacity; i++)
{
inventory[i].item = ScriptableObject.CreateInstance("Item") as Item;
inventory[i].item.objname = "Nothing";
inventory[i].item.description = "nothing";
inventory[i].item.price = 0;
inventory[i].item.sprite = "InvIconNothing";
inventory[i].item.type = "";
inventory[i].item.dr_crushing = 0;
inventory[i].item.dr_cutting = 0;
inventory[i].item.dr_impaling = 0;
inventory[i].item.pd_crushing = 0;
inventory[i].item.pd_cutting = 0;
inventory[i].item.pd_impaling = 0;
inventory[i].item.thrusting = "";
inventory[i].item.swinging = "";
inventory[i].item.thrusting_modifier = 0;
inventory[i].item.swinging_modifier = 0;
inventory[i].quantity = 0;
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.I))
{
hinvequippedcanvas.enabled = !hinvequippedcanvas.enabled;
}
if (inventoryflag)
{
ShowInventory();
}
}
public void XButton()
{
hinventorycanvas.enabled = false;
inventoryflag = false;
}
public void InventoryIconButton()
{
hinventorycanvas.enabled = true;
inventoryflag = true;
// ShowInventory();
}
/**********************
* Inventory Functions
* *******************/
// remove from inventory
public void Inv_Remove(Item item, int qnty)
{
//In the script we will remove an item from the inventory
var found = -1;
if (item.objname == "Nothing")
return;
//We will check if the item exists and take its place
for (var i = 0; i < 5; i++)
{
//ItemID eyedee1 = (ItemID)i;
if (inventory[i].item.objname == item.objname) //maybe have a unique id rather than use name? possibility.
{
found = i;
break;
}
}
if (found != -1)
{
//If we found the item, we remove it
inventory[found].quantity -= qnty;//qnty is the amount to remove
if (inventory[found].quantity <= 0)
{
inventory[found].item = ScriptableObject.CreateInstance("Item") as Item;
inventory[found].item.objname = "Nothing";
inventory[found].item.description = "nothing";
inventory[found].item.price = 0;
inventory[found].item.sprite = "InvIconNothing";
inventory[found].item.type = "";
inventory[found].item.dr_crushing = 0;
inventory[found].item.dr_cutting = 0;
inventory[found].item.dr_impaling = 0;
inventory[found].item.pd_crushing = 0;
inventory[found].item.pd_cutting = 0;
inventory[found].item.pd_impaling = 0;
inventory[found].item.thrusting = "";
inventory[found].item.swinging = "";
inventory[found].item.thrusting_modifier = 0;
inventory[found].item.swinging_modifier = 0;
} //If there is no more of the item we change the place to an empty place (Nothing item)
}
}
//In this script we will add items to the inventory
public void Inv_Add(Item item, int qnty)
{
var found = -1;
bool fullflag = false;
if (item.objname == "Nothing")
return;
for (var i = 0; i < invcapacity; i++)
{
if (inventory[i].item.objname == item.objname)
{
found = i; //We affect to found the place of the item if we find it
break;
}
}
//If it doesn't exist
if (found == -1)
{
//Let's check if there is an empty place to add our item
for (var j = 0; j < invcapacity; j++)
{
if (inventory[j].item.objname == "Nothing")
{
found = j;
break;
}
else if (j == (invcapacity - 1) && found != j)
{
fullflag = true;
break;
}
}
}
//should have an "inventory is full" contingency
if (fullflag)
{
// print this out! in a game-friendly gui - depends on whether looting or picking up, or buying
Debug.Log("inventory is full, cannot pick up object");
}
//Now let's add our item to the place we have *
if (found != -1)
{
inventory[found].item = item; //Argument 0 is the id of the item
inventory[found].quantity += qnty; //Argument 1 is the amount of the item to add
}
// need a script for looting, for buying, and for picking up
}
// Drop an item
public void Inv_Drop(Item item1)
{
// item = item clicked in gui - right click and click drop
//Instantiate(hero, item);
//not sure what to use in place of "item"
//use groundobject prefab
if (item1.objname == "Nothing")
{
// show can't drop nothing message or make noise
return;
}
Inv_Remove(item1, 1);
GameObject gogi = new GameObject();
gogi = Instantiate(grounditemprefab, new Vector3(0, 0, 0), Quaternion.identity);
gogi.transform.position = hero.transform.position += new Vector3(1, 1, 0);
GroundItem grounditem = gogi.GetComponent <GroundItem>();
grounditem.item = item1;
}
//Pick up item
public void Pick_Up(Item item)
{
Inv_Add(item, 1);
}
// got to look at GM to figure out the types for the inventory
void ShowInventory()
{
string labeltxt = "";
string labelimg = "";
int counter = 0;
for (int i = 0; i < invcapacity; i++)
{
counter = i + 1;
//update icon and text
labeltxt = "Text" + counter;
labelimg = "Image" + counter;
Text text = GameObject.Find(labeltxt).GetComponent("Text") as Text;
text.text = inventory[i].item.objname + " x " + inventory[i].quantity;
Image image = GameObject.Find(labelimg).GetComponent("Image") as Image;
Sprite invicon = Resources.Load<Sprite>(inventory[i].item.sprite);
image.sprite = invicon;
}
}
}
}
the awake function does not successfully initialize the array with nothings when i run the game. it needs to be initialized or inventory things donât work. i originally, when i was using an array of items instead of item holders, initialized it myself in the editor. but i donât want to make itemholder a scriptable object deriving class. any ideas how i can get this working? sorry i ask so many questions.
edit: also, while iâm at it, i know that some of this code is temporary. i canât re-initialize the inventory at the beginning of every scene. what would be a permanent solution, not that iâll implement it right away? should i use a static variable container? i only have one inventory for the party, and a number of merchant inventories that all use the same items.