Starting with Equiped items

Hi guys, I have a few questions about Equipment

Firstly I hava a question about starting with iteam already equiped. I understand how items are equipped but I don’t really know how to make player start with an already equipped sword for example. I’m creating team-based game, so I can’t really put it in script responsible for equipment. I thought about creating seperate script for that, but again I’m not sure how to do it in a way that won’t make me create a separate script for every character.

Second question is how to make items that can be equipped only by specific person/class or iems that player can’t unequip.

And last question whould be if it is possible with what we created in this course to allow items to grant skills. For example a ring that allow player to cast healing magic or bow that allows to shoot fire arrows and ‘normal’ arrows

perhaps make a public method in the class that equips items, that you can call from other classes and run it on Start() same with acquiring the item, it might have to be instantiated first.

You can have a special tag for special characters that use specific tagged weapons.

I’m not sure what course you are taking and I dont believe I have taken it but im sure you can write some extra code to grant special abilities.

But wouldn’t that approach cause problems? Going with example, right now I have 6 characters with mostly the same scripts. The same script is for equipment unit behavior etc. Diffrence here lies with abilities they can use which are also seperate scripts. Calling method that equips items in Start() would give the same starting items to everyone, at least I understand it like that. The only option would be to create another script that would setup starting items, for 6 characters not a big of a problem, but the mor characters I create the more additional scripts I need to create just for this

I didn’t think about that

I’m taking RPG bundle courses and Turn Based Strategy course

I’m curious to see your project I will probably be able to help you , but the best way would be to connect on discord and use gamedev.tv’s voice chat channel and you can share your screen with me, and you can explain what these scripts are doing, and if they are doing that same thing you can condense it all into one. and the will be a way to do what you are asking to start with an item, You could even put it right in front of your player character in the beggining if needed, if they are aquired from a chest or something.

Anyways my usenamre on discord is : bryanblaze00

feel free to add me and we can take a look together, I dont mind!

It’s much easier than you might think, and with only one script required.

using GameDevTV.Inventories;
using UnityEngine;

namespace RPG.Inventories
{
    public class EquipmentLoader : MonoBehaviour
    {
        [SerializeField] private EquipableItem[] itemsToLoad;

        void Awake()
        {
            if (TryGetComponent(out Equipment equipment))
            {
                foreach (EquipableItem item in itemsToLoad)
                {
                    equipment.AddItem(item.GetAllowedEquipLocation(), item);
                }
            }
        }
    }
}

This will load any equipment found in the itemsToLoad onto the character. Then it’s a matter of adding the items to load on each individual character.

This is trickier at this phase in the course (in the last course, Shops and Abilities, we’re going to introduce a change to Equipment that allows conditions to be met before the item can be equipped, but the mechanism for that is fairly complex at this stage.

We’ll start with a simpler way. Covering items that can only be equipped by certain classes requires a slight change to the Equipment script, adding a field

[SerializeField] List<CharacterClass> allowedClasses = new List<CharacterClass>();

public bool IsEquippableByClass(CharacterClass characterClass)
{
    if(allowedClasses.Count==0) return true; //no classes chosen == all classes chosen
    return allowedClasses.Contains(characterClass);
}

Then in EquipSlotUI, you’ll need to get the character class from BaseStats, and poll the EquipableItem to see if it’s equipapble

if (!equipableItem.IsEquippableByClass(playerEquipment.GetComponent<BaseStats>().GetCharacterClass()))
                return 0;

And of course, this means that you’ll need to expose the characterClass within BaseStats.cs

public CharacterClass GetCharacterClass() => characterClass;

The last part of that, not allowing unequipping… that’s doable, but use with caution!
First, in EquipableItem, you’ll need to add a bool

        [SerializeField] private bool unRemovable;

        public bool UnRemovable() => unRemovable;

Then in Equipment.RemoveItem(), add this as the first line:

if (equippedItems.TryGetValue(slot, out EquipableItem item) && item.UnRemovable()) return;

This will test the item and refuse to unequip if it is marked UnRemovable(). It’s more likely, you want that unremoveablity to be based on some condition, but that’s saved for the last course.

With the help of BryanBlaze I succesfully created a way to start a game with some items already equipped, it is not as compact but it still works. BasicallyI added to StatsEquipment possibility to add items with the help of inspector.

With the rest it’s basically needed for the same reason, just diffrent methods. I want to create a weapon that will be only used by one character and I thought that if it will be hard to implement locking it in one slot then I would just make it so only his special class will be able to use it.

And about the last question, because I don’t see you mentioning it in your replay. With the Shops and Abilities is it possible to create weapon allowing player to add abilities to the player? Or if not what would you do to implement this kind of mechanic?

That one’s a lot trickier. I don’t have a quick answer for making this happen. Probably the simplest way would be to have a field for the ability itself in the EquipableItem… when the item is equipped, that ability could be added to a special slot in the ActionStore, and when the item is unequipped, that ability would then be removed from the Action Store. Of course, if these abilities are on different equipment slots, this would crate some interesting conflicts.

Ok, that makes things difficult as I’m not exactly using Action Store, then last question. Would it be possible to somehow make item add component when we equip it and destroy this component after removing that weapon?

If you’re not using the ActionStore, then how are you using any abilties?

You can add a component with AddComponent. The only way to remove a component is with Destroy

For the fighting elements I used ActionSystem introduced in Turn Based Strategy course, so every abilitie is a separete script. I’m aware of AddComponent, but I don’t know if i can use it in this context. What I mean is after equipping specific item, this item adds script responsible for ability assigned to this item.

It’ll have to be something similar to the WeaponConfig’s Spawn. The tricky part is that you really need to know what class you want to spawn… It might be better to add a GameObject, so that the ability would be on a child GameObject. It doesn’t matter if it’s on the same GameObject or on a child GameObject, as long as the ability has access to the Unit, and the ability is in the Unit’s action list…

First create a prefab with just the ability class on it, you’ll link this in a field in your EquipableItem.
When the item is equipped, you’ll need to call some sort of spawn event on the newly installed Equipment that instantiates the prefab. Since we want to be able to uninstantiate it as well, a handler might be the best way to go here…

An EquipmentActionManager class could listen for equipment being loaded/unloaded.

If a new piece of equipment is installed, then call Spawn on it. The Spawn method will need to return a reference to the newly installed action, here’s how I’d do Equipment.Spawn()

public BaseAction Spawn(Unit unit)
{
    if(baseActionPrefab==null) return null;
    BaseAction action = Instantiate(baseActionPrefab, unit.Transform);
    action.Setup(unit); //you'll need to create a setup in BaseAction that stores the unit.
    return action;
}

Now in your EquipmentActionManager, you’ll want to tell Unit that you’ve added the action so it adds it to it’s list (It’s an array in the course, just change it to a list). You’ll also want to add it to a Dictionary to keep track of what actions have been installed via equipment

Dictionary<EquipableItem, BaseAction>equippedActions = new();
void HandleAddedEquipment(EquipableItem item)
{
    BaseAction action = item.Spawn(unit);
    if(action!=null)
    {
         unit.AddAction(action);
         equippedActions.Add(item, action);
     }
}

void HandleItemUnequipped(EquipableItem item)
{
     if(equippedActions.TryGetValue(item, BaseAction action))
     {
          unit.RemoveAction(action);
          equippedActions.Remove(action);
          Destroy(action.GameObject);
      }
}

I think I understand, I will try it tomorrow and If I will get stuck somewhere I will ask for help

I wonder if having the component on the character already but have it disabled until the item is equipped then you just set the component active to true, maybe its too simple but just a thought.

Ok so I added the option for items to be locked by using UnRemovable but there is one issue. When I drag item from equipment slot to inventory slot, item is duplicated. It stays in the equipment and at the same time it goes to inventory. I guess it has to be something with how draging work, but I don’t really now what I need to change

Unremovable is a tricky one, for sure… You could try adding a check in InventorySlotUI that returns zero if the item is Unremoveable. Logically speaking, the only time it should be being dragged is from equipment to Inventory, so that should do the trick.

It’s possible, yes. It does make your characters less modular.

Okay, it works. Item stays in place, except it can still be placed in another equipment slot. For example I have 2 slots for weapons and 2 slots for rings and when I put unremovable item in one slot, it can still go to another, or rather, it will copy itself to another slot. But that’s something I will figure out later.

Quick question about equipment, if I have item A in equipment slot and I replace it with item B using code what happens with item A? It’s not going to inventory so is it destroyed? I ask because I don’t know if I will have to deal with some unexpected events in the future

And about adding components earlier but disabling them as long as player doesn’t equip an item, wouldn’t this kind of approach make everything messier in the inspector? With that I would have to add component for every ability, you can have from items on every character. At least that’s what I’m thinking

You’re correct, if you equip an item through code, the item in the slot will be forgotten. The solution to this is to get any item currently in the equipment slot, and then place the new item in the equipment slot and Inventory.AddtoFirstEmptySlot the item that was in the equipment slot to begin with.

Yes, that’s what I was getting at with my rather short answer earlier. It is less modular, because you have to add the ability to every character, and it adds to the number of items on the character. If, instead, you simply instantiate a GameObject when an ability is to be added, and remove it when it’s no longer valid, this keeps things tidier.

Hint: The same logic applies to the Equipment slot as the Inventory… if it’s not removeable, then it also isn’t equippable through the UI interface (you’ll have to equip an item like that through code). The EquipmentSlotUI should reject the item if it’s not removeable (and reject removing the item if it is not removeable).

I don’t know why I didn’t use the same solution in Equipment, but it works without any problems. The only thing left to do is to add items that grant abilities.

I think I’m lost, with Setup on BaseAction and how to correctly create EquipmentActionManager which I guess should be attached to a character. Also I see that unit should have AddAction and RemoveAction methods which I understand what should do, but not exactly how they should be written. Additionaly in HandleItemUnequipped there is an error in if statment, or at least that’s what Unity says

LOL, there is an error there, yes, I forgot the out keyword.

The code was overall abstract to begin with.

The EquipmentActionManager needs to do a couple of things. When the game starts up, in Start(), it needs to read all of the items in the Equipment to determine if any of them spawn abilities.
It needs to listen to events from Equipment to determine if an item has been added or removed…

Now Equipment already comes with an equipmentUpdated script, which fires whenever anything changes. It might, however, be handy to add two events to Equipment to make determining the actual changes a bit easier.

public event System.Action<EquipableItem> EquipmentItemAdded;
public event System.Action<EquipableItem> EquipmentItemRemoved;

Now in Equipment.AddItem, fire the added event within AddItem and the removed event within RemoveItem

EquipmentItemAdded?.Invoke(item);

The EquipmentManager will then subscribe to Equipment in Awake() to each of these methods.

Those are the methods I made at the top, with the out parameter, of course.

The baseAction.Setup() just needs to set the Unit, or it could be overridden if the action needs more.

public virtual void Setup(Unit unit)
{
    this.unit = unit;
}

Privacy & Terms