- Home /
Is this a good way to script an inventory system?
Ok guys I am just wondering if this is an efficient way of implementing an inventory system.
Basically, I will be working with booleans. Say I have a boolean that is "hasSword = true" if the boolean is true, print the 2d graphic in the inventory system and give the user the ability to wield the sword for example:
function Start()
{
sword.gameObject.SetActiveRecursively(false);
}
}
function Update()
{
if (weapon == 1 && hasSword == true)
{
sword.gameObject.SetActiveRecursively(true);
}
}
Is this an efficient way of doing what I want or is there a better way?
I made an inventory script a few days ago... It was quite complex. It was made for a top-down hack-n-slash game. Would you like me to post it? It may be of some use to you, or not. Either way, it is long and will take time to read and understand.
That would be great! If you wouldn't $$anonymous$$d, it would be interesting to read over :)
Hey, this tut might help you out: https://www.youtube.com/watch?v=$$anonymous$$LaGkc87dDQ
Hey here is a great tutorial, that $$anonymous$$ches you how to create an inventory system from scratch with the new unity 4.6 ui: https://www.youtube.com/watch?v=$$anonymous$$LaGkc87dDQ
Answer by Meater6 · Jan 23, 2013 at 08:32 PM
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[i]) {
if(slots[i].name == obj.name) {
slots[i].GetComponent.<Item>().stackCounts += item.stackCounts;
Destroy(obj);
break;
}
}
if(!slots[i]) {
slots[i] = 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.
Fantastic, I am going to haul through this and understand it in full when the morning comes. Thank you very much and it has helped me understand more complex inventory systems.
If you have any questions about it, come back here and ask me. :)
Answer by ryba · Jan 23, 2013 at 11:33 AM
It depends on how complex inventory will be.
If there wont be wide variety of items, but only booleans if there is an item equipped, meaning that there wont be 2 or more different swords, just one sword as weapon, then you could create one InventoryController componenent, and assign it to your player.
Inside this component just set bool fields for every possible item, that will indicate if item is equipped + add methods like isSwordEquipped, isArmorEquipped etc...
Then, in your inventorty window just call those methods to check if items icon should be displayed or not.
If you need more complex equipment system, then nobody will tell you how to implement it best, if you wont describe how your equipment system should work.
Just to clarify I didn't ask for someone to implement it for me, I am confident with my program$$anonymous$$g skills, I was just asking for advice which is what this is for... Right? Anyway, thanks for the advice I guess I will go down the more complex route. I was just wondering if this way would be sufficient.
I didnt tell i want to implement it for you, but just throw an advices how to implement that :) Thats what you asked for, right ? If you still want help just describe how this system should work and then i could give some advices.
Your answer
Follow this Question
Related Questions
how can i make this code more efficient for my inventory system 0 Answers
Key and Door Tutorial Help? 2 Answers
Inventory system Help 2 Answers
Creating an interactable inventory? 1 Answer
Inventory stack system problam 0 Answers