Effective way to implement equip system

Morning, I’m developing an android top down shooter kind of style game and just wanted some pointers regarding an equip system. Currently, I have a beta inventory system that adds items from a database (item database is a list of items). The system as it stands literally just populates a scroll rect with items on the click of a button.

What I would like help with is actually telling my character which weapons it has equipped. On my equipment screen I have 3 weapon slots, and when either of them is pressed it opens up the inventory window (I’d thought of assigning a value for slotID when the inventory window is opened so that when ‘Equip’ is pressed it knows which slot to equip to). Could anyone help me implement this?

Once that is done my plan was to just create another list of EquippedItems, and use Don’tDestroyOnLoad (), and when the game starts I could instantiate the equipped items simultaneously. I’m worried that I’m doing a lot of work in vain at the moment and not really sure if what I want to do is possible using this list as an item database.

Here is what I’m using at the moment:

Item

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Item {

    public string itemName;
    public string itemDescription;
    public int itemID;
    public Sprite itemSprite;
    //public string itemSpriteName;
    public GameObject itemPrefab;
    //public string itemPrefabName;

    public Item (string name, string description, int id, string prefabName)
    {
        itemName = name;
        itemDescription = description;
        itemID = id;
    }

    public Item ()
    {
    }
}

Item Database

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemDatabase : MonoBehaviour {

public List items = new List();

private void Awake ()
{
DontDestroyOnLoad (this.gameObject);
}

public Item GetItemByID (int id)
{
foreach (Item itm in items) {
if (itm.itemID == id) {
return itm;
}
}
Debug.Log (“Can’t find item by ID!”);
return null;
}
}

Inventory

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Inventory : MonoBehaviour {

public int slotsAmount;

public GameObject slotPrefab;
public GameObject itemPrefab;
public GameObject equipButtonPrefab;
public GameObject slotParent;

public List slots = new List();

private ItemDatabase itemDatabase;

private void Awake ()
{
DontDestroyOnLoad (this.gameObject);
}

private void Start ()
{
itemDatabase = GameObject.FindGameObjectWithTag (“ItemDatabase”).GetComponent ();
}

public void CreateSlot ()
{
slots.Add(Instantiate (slotPrefab));

for (int i = 0; i < slots.Count; i++)
{
slots*.transform.SetParent (slotParent.transform);*
}
}
public void AddItem (int id)
{
CreateSlot ();
Item itemAdd = itemDatabase.GetItemByID (id);
for (int i = 0; i < slots.Count; i++)
{
if (slots*.transform.childCount <= 0)*
{
GameObject itemInstance = Instantiate(itemPrefab);
itemInstance.transform.SetParent(slots*.transform);*
itemInstance.transform.localPosition = Vector2.zero;
itemInstance.GetComponent().sprite = itemAdd.itemSprite;
Instantiate (equipButtonPrefab, slots*.transform);*
break;
}
}
}
}

@ItzChris92

Hi, I’m actually trying to create something similar…

In general, I’d do something like this (I didn’t read your code…):

I wouldn’t just have GameObjects as slots.

Have a Slot class. It knows if it’s either empty or has some Item.

Make it possible to get the item in slot.

Then have your equippedItem field somewhere, maybe just in your character, not in inventory.

This makes it possible to know what you have equipped. Maybe you have more than one equipment slots, if you have armor etc.

Then have some way to swap weapon - (for example): if equipped slot is full, and you click it, you open the inventory. You click some inventory item, it swaps the Item in equippedItem slot with the one you clicked in the inventory.

Same if the slot is empty, click opens the inventory, if you click a weapon, it’s put to equippedItem slot. But the details are of course up to you.

Thanks for the input! What you describe is exactly what I would like to be happening lol I just need a few pointers code wise. I’m still a C # noob, and I am mainly curious at how to transfer data and item info from inventory slot to weapon slot, etc etc. I already have 3 weapon slots, and when each of them is clicked it opens the inventory. How could I actually equip a weapon to a slot, and in the correct one?

Once I have that in place I can then work on instantiating the equipped items when the player starts a level, but again I’m unsure of a good way to do this

Still struggling with this. I’ve changed my inventory database to a scriptable object now though. But still clueless as to how I can equip weapons to different slots!

After I have made it possible to equip weapons (and later armor) to slots I plan to make a list of equipped equipment. When a level loads it could iterate through this list and instantiate each item in the correct parent object (the players bones).

I’m currently working through this as well. Funny enough, I went the other route… I had a system for equipping stuff before I had an actual inventory system. Right now, it’s just a bunch of weapon game objects, and I’ve manually assigned them to my weapon slots. Still working on the inventory, for which I’m leaning towards using scriptable objects.

I’m not sure, but I’d think that I could create an item scriptable object which gets attached to the weapon… actually, probably a class that derives from item that is specific to weapons, let’s call it weaponItem. That would get attached to the game object to identify all it’s properties like shoot speed, ammo, and such. I’m thinking this same scriptable object could/would(?) probably get attached to the inventory slot as well, as you arm it in a slot.

Currently (and probably permanently) the player has every single weapon available in the game under their right hand, all disabled. The equip function just enables/disables the appropriate weapons, as required. I am just thinking out loud here, but if the item scriptable object has a reference to the actual game object, then you could do something like this (bear in mind this isn’t actual code):

private weaponItem[] weaponSlots = new WeaponItem[9];
for (int i = 0; i < weaponSlots.Count; i++) {
    weaponSlots.gameObjectVariable.SetActive(true);
}
weaponSlots[slotToArm].gameObjectVariable.SetActive(true);

I currently have interfaces for my Shoot function and reload, and weapon specific stuff. But there’s other ways of doing things that allow you to be able to then do something like this:

weaponSlots[currentlyArmedSlot].gameObjectVariable.GetComponent().Shoot();

Anyways, sorry for the scatterbrained, randomly-thought-out post. Some of this is me just working through my plan, out loud. lol

Thank you for your reply! I should have mentioned actually, I already have gameobjects for my weapons, and they are parented to my player. Then I have a weapon swapping script that enables/disables weapons similar to yours. I wouldn’t call this ‘Equipping’ as such.

My scriptable object is simply a database containing things such as item ID, name, description, sprite prefab, and GameObject prefab. The weapons themselves use classes inherited from a GunBaseClass that apply weapon stats etc.

My character equipment screen will actually be a completely different scene, so I am hoping I can use only item data to equip weapons and armor, and then instantiate them when and game level begins. Not sure if this is a good way or even possible way of doing it, but that is the concept.

I just don’t know how to:

  1. Equip a weapon/ armor using its ID in my Scriptable Object database.

  2. Instantiate weapons/armor (using itemID, and the gameobject prefab stored in the database) via a script when a playable level begins.

Sorry, help me talk in the same language as you here :smile:

When you are talking about “equipping”, you just mean the character having it in their inventory then? (as opposed to actually currently wearing it, or having a weapon armed)

As for your questions, if I understand your meaning of “equip” better now, you might have to clarify what part of these points you’re having problems with.

To me, point 1 is just as “simple” (OK, simple is the wrong word) as having a collection of items as your inventory, attached to the player in some way (so that each player can have their own inventory, if it’s a multiplayer game). When you pick something up, call an add function in your inventory that looks for a free slot in the inventory, and adds the scriptable object to it. This process is described in the Adventure Game tutorial.

If you want to actually arm/wear something from the inventory, then you look at the scriptable object’s game object for the equipment slot. I don’t have an armor concept (at least yet, but don’t think I will in my current game). But for weapons, if your character wants to arm something, however you do that (drag/drop from an inventory UI, or some key/button presses), you’d remove or deactivate anything active in the players right hand (you should know what’s there… you put it there) and then instantiate/parent to the right hand, or activate the object in the right hand. You mentioned that your scriptable object has the game object in it, so you can use that reference to do your instantiate, or SetActive the item childed to your character’s right hand.

I might change this, but right now my player(s) has all the game’s possible weapons in their right hand, just they’re all deactivated. I activate them when the player arms them.

I think your questions are still quite broad, and there’s a lot of different ways you can go about this, based on the requirements of your game, so it’s hard to answer the questions.

Again, I’ll have a player inventory (collection of my items) that stores item scriptable objects. The inventory will have methods to add and remove. The add will look if it already exists, and increment count if it does (where this makes sense), or find a new, empty slot to put it in if it doesn’t exist in the inventory. The remove will null out the scriptable object reference in the inventory. For me, the inventory for weapons is actually just going to be a hotbar of 9 item slots, but the concepts should be fairly similar… when they pick something up, it finds a slot in the hotbar or your inventory that’s empty for the weapon to be added, and when you’re ready to arm, you arm based on either activating the game object that’s already childed to player’s right hand that matches your item data… or you destroy what’s there and instantiate the game object that to the player’s right hand. Your item data stores the game object, so this shouldn’t be a problem to do.

Basically instantiate(selectedItem.gameobject, parenttransform) or selectedItem.gameobject.SetActive(true) depending on the route you’re going about this (again, using pseudo code here).

Sorry if I’m not understanding the trouble that you’re having, but hopefully some of what I’m saying is helping you flesh out what you can do.

My thoughts here are still a bit abstract, although some actually based on work I’ve already done, but that might be why I’m not fully understanding the struggle you’re having with your two points.

Sorry if I haven’t been clear! Understand exactly what you are saying, and for the most part I would be able to implement a system like you describe myself.

My game is a level/stage based game (with an endless survival mode also), so rather than my player having an in-game inventory he will have instead a load out scene. On this scene the user will be able to view their selected character, and equip weapons and armour from a list (his inventory). This inventory will only be accessible from this scene, so when I say an item is equipped, I actually mean that it will transfer the item data from inventory to a player slot (no gameobjects at this point, just raw data). The player can gain new weapons as loot but won’t be able too equip them until they either clear or quit the level they are playing.

As it stands I have a shop panel where a player can purchase weapons (in the same scene as the inventory system). This is done simply by coding a ui button to call an AddItem() method with item ID as a parameter. This functions correctly.

I hope that has explained things a little better. I have played games that have used this kind of system before (SAS zombies for one) so I don’t think I am trying anything too out of the ordinary. I have only been coding in C# for about 3 months, and only in my free time, so this may be a simple thing to do but it is not something I currently know how to do. I’m learning about different unity functionality every day so I really do appreciate any input from people on these forums

EDIT: This is an Android project, so I will probably be making alot of use of UI buttons rather than pointer events and click handlers