cptscrimshaw
Member
Going through the process of updating to 1.1.3 (from 1.1.1), and everything went pretty smoothly, however, I'm having trouble with the hotbar. I went through the instructions on the 1.1.2 update to adjust the hotbar and I am able to assign items to it with no problem. I'm currently having two separate issues:
- When hitting the number key (or clicking) to perform the ItemAction, nothing happens. I've fooled around with it for a while and am having no luck. Any ideas? Here are a few of screenshots of my setup:
- I wrote a custom script to save the hotbar and also highlight the active item until another is selected. The save/load portion is working fine, but the highlighting isn't doing anything at all anymore. I'm wondering if this has to do with the hotbar now inheriting the ItemViewSlotsContainer, but that doesn't really make sense. I'm guessing it has to do with this line somehow, but all the code is below:
Code:
HotBarItemViewSlotContainer.ItemViewSlots[slotNumber].transform.GetChild(0).GetComponent<Image>().sprite = highlightedSlotBackground;
Code:
using System.Collections;
using System.Collections.Generic;
using Opsive.UltimateInventorySystem.UI.Item;
using Opsive.UltimateInventorySystem.UI.Item.ItemViewModules;
using UnityEngine;
using UnityEngine.UI;
using PixelCrushers;
using Opsive.UltimateInventorySystem.Core;
using Opsive.UltimateInventorySystem.Core.InventoryCollections;
using Opsive.UltimateInventorySystem.UI.Panels.Hotbar;
public class HotbarExtensions : MonoBehaviour
{
[SerializeField] private ItemHotbar hotbar;
[SerializeField] private ItemViewSlotsContainer HotBarItemViewSlotContainer;
[SerializeField] private Sprite defaultSlotBackground;
[SerializeField] private Sprite highlightedSlotBackground;
private List<string> hotbarItems = new List<string>();
private int currentlySelected = -1;
private KeyCode[] keyCodes = {
KeyCode.Alpha1,
KeyCode.Alpha2,
KeyCode.Alpha3,
KeyCode.Alpha4,
KeyCode.Alpha5,
KeyCode.Alpha6,
KeyCode.Alpha7,
KeyCode.Alpha8,
KeyCode.Alpha9,
};
private void Awake()
{
SaveSystem.saveDataApplied += OnSaveDataApplied;
EventHandler.ScenePortalTriggered += SaveBeforeChangingScenes;
HotBarItemViewSlotContainer.OnItemViewSlotClicked += HandleItemViewSlotClicked;
}
void OnSaveDataApplied()
{
if (ES3.KeyExists("hotbarItems"))
{
hotbarItems = ES3.Load<List<string>>("hotbarItems");
LoadHotbar();
}
}
void SaveBeforeChangingScenes()
{
SaveHotbar();
ES3.Save("hotbarItems", hotbarItems);
}
void OnApplicationQuit()
{
SaveHotbar();
ES3.Save("hotbarItems", hotbarItems);
}
private void OnDestroy()
{
SaveSystem.saveDataApplied -= OnSaveDataApplied;
EventHandler.ScenePortalTriggered -= SaveBeforeChangingScenes;
HotBarItemViewSlotContainer.OnItemViewSlotClicked -= HandleItemViewSlotClicked;
}
private void HandleItemViewSlotClicked(ItemViewSlotEventData slotEventData)
{
if (slotEventData.ItemViewSlot.Index == currentlySelected)
{
slotEventData.ItemViewSlot.transform.GetChild(0).GetComponent<HotbarItemView>().m_Default = defaultSlotBackground;
currentlySelected = -1;
}
else
{
if (currentlySelected != -1)
{
HotBarItemViewSlotContainer.ItemViewSlots[currentlySelected].transform.GetChild(0).GetComponent<Image>().sprite = defaultSlotBackground;
}
slotEventData.ItemViewSlot.transform.GetChild(0).GetComponent<HotbarItemView>().m_Default = highlightedSlotBackground;
currentlySelected = slotEventData.ItemViewSlot.Index;
}
}
private void UpdateSelectedItem(int slotNumber)
{
if (slotNumber == currentlySelected)
{
HotBarItemViewSlotContainer.ItemViewSlots[currentlySelected].transform.GetChild(0).GetComponent<Image>().sprite = defaultSlotBackground;
currentlySelected = -1;
}
else if(currentlySelected == -1)
{
HotBarItemViewSlotContainer.ItemViewSlots[slotNumber].transform.GetChild(0).GetComponent<Image>().sprite = highlightedSlotBackground;
currentlySelected = slotNumber;
}
else
{
foreach(var slot in HotBarItemViewSlotContainer.ItemViewSlots)
{
slot.transform.GetChild(0).GetComponent<Image>().sprite = defaultSlotBackground;
}
HotBarItemViewSlotContainer.ItemViewSlots[slotNumber].transform.GetChild(0).GetComponent<Image>().sprite = highlightedSlotBackground;
currentlySelected = slotNumber;
}
}
private void SaveHotbar()
{
hotbarItems.Clear();
foreach (var itemSlot in HotBarItemViewSlotContainer.ItemViewSlots)
{
if (itemSlot.ItemInfo.Item != null)
{
hotbarItems.Add(itemSlot.ItemInfo.Item.name);
}
}
}
private void LoadHotbar()
{
for (int i = 0; i < hotbarItems.Count; i++)
{
var tempItem = InventorySystemManager.CreateItem(hotbarItems[i]);
var retrievedItemInfo = Statics.player.transform.GetComponent<Inventory>().GetItemInfo(tempItem);
hotbar.ToggleAssignItemToSlot((Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo)retrievedItemInfo, i);
}
}
private void Update()
{
for (int i = 0; i < keyCodes.Length; i++)
{
if (Input.GetKeyDown(keyCodes[i]))
{
int numberPressed = i;
UpdateSelectedItem(numberPressed);
Debug.Log(numberPressed);
}
}
}
}