Auto Equip/Replace on right click

thinking of implementing auto equip to designated slots(if available or swap out if existing already in slot) on a right click on the inventory.
just want to double check before i code from scratch. is there an existing code that exist where i can just call from unity event to do said task?

@Brian_Trotter

There isn’t a script or call to do this already…
Your InventoryUI will need to know about the Equipment.cs and EquipableItems… You’ll have to detect the right click, determine if the item in the inventory slot is an EquipableItem, if it is, perform the same action as the Drag and Drop is.

ah nuts, forgot to look at drag and drop that might have save me some time to just copy paste the implementation. So i did something along the lines you suggested however i changed it abit to add an interface to them so i can auto equip a spell/potion/equipment just to fit my usecase. Also i’m abit confused on Events atm. confused in the sense of when should i be using them. Like which particular instance should events be considered?

Events are best used for situations where we don’t want to inspect something every frame to see what it is… a simplistic example is in a Heads Up Display… we want to show the player’s current health. You could put something in an Update that polls the player health every frame or you could subscribe to an event that fires whenever the player’s health actually changes.
We use it in the Inventory system to inform the rest of the game that items have changed… for example, when I equip a weapon, I want Fighter to know that the weapon has changed but who knows, there may be other elements that need to know of that change. An event takes the worry away, any componnent that wants the event can subscribe to it, and all the Equipment has to do is say “onEquipmentChanged?.Invoke();”

so let me see if i get this right.

  1. In an effort to reduce the usage of Update() — optimisation
  2. 1: Many calls where 1 method needs to call several different component
  3. When you don’t want to expose a method/function to a public

atm my healthUI is only triggered when health changes but i felt that it was only the one thing so i just made public function in the UI script thats call when health is changed weather via potion/regen/damage. so i guess many to 1

You’ve got the general idea. :slight_smile:

yeap i see why you suggested events for the health. i was able to remove several Update method from different scripts. Also applied the same for my mana script

Yep, the more void Update() blocks you can outright eliminate from your code, the faster your game will run.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms