Multiple Hotbars showing the same items

Grandexed

New member
Hello,

I am trying to setup a hotbar to display in both the gameplay view and the main menu view.

I created a separate hotbar for each view and they both work fine on their own, but I can't figure out how to sync them together so that any item changes I make to one of the hotbars will reflect on the other.

Is there an example scene available for this, or how could I reference one hotbar to another so they show the same items?
 
Unfortunatly we don't have an example for this. It requires some extra code to do the syncing.

There are a few approaches.

1. Use only one Hotbar UI and simply change it's parent and framing so it appears in two different places (only works if you only show one at a time)

2. Use a equipment view instead of a Hotbar. There are example of this in the hotbar feature scene. Since the items are in an itemcollection they get automatically synced. But the functionality between hotbar and equipment view is slightly different so it does not fit all use cases

3. Use a custom script that syncs the two hotbars by listening to events on hotbar


I hope that guides you in the right direction
 
Bummer! I was hoping there was something obvious I was missing.

Thank you for the response! I'll try creating a custom script
 
Hi Sangemdoko,

I was able to solve this by adding the following code to the ItemHotbar.cs script:

SyncHotbars() gets called at the end of the ToggleAssignItemToSlot() and RefreshItemSlotInfos() methods. The variable "m_ItemHotbarToSync" is referencing the other hotbar component.

unitypic4.png


Happy this is working now! But I do have one more question about the hotbar.

Currently, the user is able to drag the same item into multiple hotbar slots. Is there an easy way to prevent this or would this require custom code?

Using the "Assign" item action works as expected and does not allow the user to assign the same item to multiple slots, so this issue only occurs when the user drags and drops items into the hotbar.
 
Hi,

I'm sharing this here:
Code:
/*
 *      Ultimate Inventory System Extension
 *      Blood Cloud Studio
 *      Copyright (c) all rights reserved
 *      https://www.bloodcloudstudio.com
 */

namespace BloodCloudStudio.UltimateInventorySystem.UI.Panels.Hotbar
{
    using Opsive.UltimateInventorySystem.Core.DataStructures;
    using Opsive.UltimateInventorySystem.UI.Panels.Hotbar;
    using UnityEngine;

    public class ReplicatedItemHotbar : ItemHotbar
    {
        [Tooltip("The item hotbar to replicate.")]
        [SerializeField()] protected ReplicatedItemHotbar m_ReplicatedItemHotbar;

        /// <summary>
        /// Replicate assign an item to a slot.
        /// </summary>
        /// <param name="itemInfo">The item.</param>
        /// <param name="slot">The item slot.</param>
        public virtual void ReplicateAssignItemToSlot(ItemInfo itemInfo, int slot)
        {
            base.AssignItemToSlot(itemInfo, slot);
        }

        /// <summary>
        /// Assign an item to a slot.
        /// </summary>
        /// <param name="itemInfo">The item.</param>
        /// <param name="slot">The item slot.</param>
        public override void AssignItemToSlot(ItemInfo itemInfo, int slot)
        {
            base.AssignItemToSlot(itemInfo, slot);

            m_ReplicatedItemHotbar.ReplicateAssignItemToSlot(itemInfo, slot);
        }

        /// <summary>
        /// Replicate use an item from the hot bar.
        /// </summary>
        /// <param name="itemSlotIndex">The item slot index of the item to use.</param>
        public virtual void ReplicateUseItem(int itemSlotIndex)
        {
            base.UseItem(itemSlotIndex);
        }

        /// <summary>
        /// Use an item from the hot bar.
        /// </summary>
        /// <param name="itemSlotIndex">The item slot index of the item to use.</param>
        public override void UseItem(int itemSlotIndex)
        {
            base.UseItem(itemSlotIndex);

            m_ReplicatedItemHotbar.ReplicateUseItem(itemSlotIndex);
        }
    }
}
The ReplicatedItemHotbar works in pairs, so you have to replace 2 ItemHotbars with 2 ReplicatedItemHotbars and add one into the other and vice versa.

Code:
/*
 *      Ultimate Inventory System Extension
 *      Blood Cloud Studio
 *      Copyright (c) all rights reserved
 *      https://www.bloodcloudstudio.com
 */

namespace BloodCloudStudio.UltimateInventorySystem.UI.Panels.Hotbar
{
    using Opsive.UltimateInventorySystem.UI.Panels.Hotbar;
    using UnityEngine;

    public class ReplicatedItemHotbarHandler : ItemHotbarHandler
    {
        protected ReplicatedItemHotbar m_ReplicatedItemHotbar;

        /// <summary>
        /// Initialize replicated item hotbar.
        /// </summary>
        protected override void Start()
        {
            base.Start();

            if (m_ItemHotbar is ReplicatedItemHotbar replicatedItemHotbar) {
                m_ReplicatedItemHotbar = replicatedItemHotbar;
            } else {
                Debug.LogError("Error: item hotbar need to be a replicated item hotbar!");
                enabled = false;
            }
        }

        /// <summary>
        /// Override check for the input in update.
        /// </summary>
        protected override void Update()
        {
            for (int i = 0; i < m_HotbarInput.Length; i++) {
                if (m_HotbarInput[i].Index < 0 || m_ReplicatedItemHotbar.SlotCount < m_HotbarInput[i].Index) {
                    continue;
                }
                if (m_HotbarInput[i].CheckInput(m_PlayerInput)) {
                    m_ReplicatedItemHotbar.ReplicateUseItem(m_HotbarInput[i].Index);
                }
            }
        }
    }
}
As for the ReplicatedItemHotbarHandler, it replaces an ItemHotbarHandler so that a ReplicatedItemHotbar does not use the item twice.

See you soon!
 
Back
Top