Forum begins after the advertisement:
[Part 19] Tooltip not showing when leveling up
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 19] Tooltip not showing when leveling up
- This topic has 5 replies, 2 voices, and was last updated 4 months, 2 weeks ago by Terence.
-
AuthorPosts
-
July 5, 2024 at 3:38 am #15190::
Hello,
After finishing part 19, I wanted to go back and fix some of the things that I wasn’t able to figure out fully. After reading through everything and rewatching multiple times, everything seems correct and I’m unsure what might be the issue here. When Leveling up, the tooltip doesn’t show and this is because there are 4 upgrades to be chosen. I’m unsure why there is is 4th and why it isn’t visible. Another thing not mentioned in the topic title is that when leveling up, the only items that show are passives.public void SetUpgrades(PlayerInventoy inventory, List<ItemData> possibleUpgrades, int pick = 3, string tooltip = "") { pick = Mathf.Min(maxOptions, pick); //if there is not enought upgrade option boxes, create them if (maxOptions > upgradeOptions.Count) { for (int i = upgradeOptions.Count; i < pick; i++) { GameObject go = Instantiate(upgradeOptionTemplate.gameObject, transform); upgradeOptions.Add((RectTransform)go.transform); } } //if a string is provided, turn on the tooltip tooltipTemplate.text = tooltip; tooltipTemplate.gameObject.SetActive(tooltip.Trim() != ""); /*activate only the number of upgrade options needed. arm the buttons and the different attributes like discriptions, ect.*/ activeOptions = 0; int totalPossibleUpgrades = possibleUpgrades.Count; //how many possible upgrades to choose from foreach (RectTransform r in upgradeOptions) { if (activeOptions < pick && activeOptions < totalPossibleUpgrades) { r.gameObject.SetActive(true); //select one of the possible upgrades then remove it fromt he list ItemData selected = possibleUpgrades[Random.Range(0, possibleUpgrades.Count)]; possibleUpgrades.Remove(selected); Item item = inventory.Get(selected); //Inset the name of the item TextMeshProUGUI name = r.Find(namePath).GetComponent<TextMeshProUGUI>(); if (name) { name.text = selected.name; } //Insert the current level of the item, or "New!" text if it is a new weapon TextMeshProUGUI level = r.Find(levelPath).GetComponent<TextMeshProUGUI>(); if (level) { if (item) { if (item.currentLevel >= item.maxLevel) { level.text = "Max!"; level.color = newTextColor; } else { level.text = selected.GetLevelData(item.currentLevel + 1).name; level.color = levelTextColor; } } else { level.text = newText; level.color = newTextColor; } } //Inset the description of the item TextMeshProUGUI desc = r.Find(descriptionPath).GetComponent<TextMeshProUGUI>(); if (desc) { if (item) { desc.text = selected.GetLevelData(item.currentLevel + 1).description; } else { desc.text = selected.GetLevelData(1).description; } } //Insert the icon of the item Image icon = r.Find(iconPath).GetComponent<Image>(); if (icon) { icon.sprite = selected.icon; } //Insert the button action binding Button b = r.Find(buttonPath).GetComponent<Button>(); if (b) { b.onClick.RemoveAllListeners(); if (item) { b.onClick.AddListener(() => inventory.LevelUp(item)); } else { b.onClick.AddListener(() => inventory.Add(selected)); } } activeOptions++; } else { r.gameObject.SetActive(false); } } //sizes all the elements so they do not exceed the size of the box RecalculateLayout(); }
//Determines what upgrade options should appear void ApplyUpgradeOptions() { /*<availableUpgrades> is an emply list that will be filtered from <allUpgrades>, which is the list of all upgrades in PlayerInventory. Not all upgrades can be applied, as some may have already been maxed out or the player may not have enough inventory slots*/ List<ItemData> availableUpgrades = new List<ItemData>(); List<ItemData> allUpgrades = new List<ItemData>(availableUpgrades); allUpgrades.AddRange(availablePassives); //how many weapon and passive slots are left int weaponSlotsLeft = GetSlotsLeft(weaponSlots); int passiveSlotsLeft = GetSlotsLeft(passiveSlots); /*Filters through the available weapons and passives. Adds those that can possible be an option*/ foreach (ItemData data in allUpgrades) { /*if a weapon of this type exists, allow for the upgrade if the level of the weapon is not already maxed out*/ Item obj = Get(data); if (obj) { if (obj.currentLevel < data.maxLevel) { availableUpgrades.Add(data); } } else { /*if the item is still not in the inventory, check if there still is enough slots to take the new item*/ if (data is WeaponData && weaponSlotsLeft > 0) { availableUpgrades.Add(data); Debug.Log("Weapons data: " + data.ToString()); } else if (data is PassiveData && passiveSlotsLeft > 0) { availableUpgrades.Add(data); Debug.Log("Passives data: " + data.ToString()); } } } //show the UI upgrade window if there are still availabel upgrades left int availUpgradeCount = availableUpgrades.Count; if (availUpgradeCount > 0) { bool getExtraItem = 1f - 1f / player.Stats.luck > UnityEngine.Random.value; if (getExtraItem || availUpgradeCount < 4) { upgradeWindow.SetUpgrades(this, availableUpgrades, 4); } else { upgradeWindow.SetUpgrades(this, availableUpgrades, 3, "Increase you Luck stat for a chance to get 4 items!"); } } else if (GameManager.instance != null && GameManager.instance.choosingUpgrade) { GameManager.instance.EndLevelUp(); } }
July 5, 2024 at 11:29 am #15195::For the missing tooltip, add the following line and show me what it prints:
public void SetUpgrades(PlayerInventoy inventory, List
possibleUpgrades, int pick = 3, string tooltip = "") { pick = Mathf.Min(maxOptions, pick); //if there is not enought upgrade option boxes, create them if (maxOptions > upgradeOptions.Count) { for (int i = upgradeOptions.Count; i < pick; i++) { GameObject go = Instantiate(upgradeOptionTemplate.gameObject, transform); upgradeOptions.Add((RectTransform)go.transform); } } print("Tooltip: " + tooltip); //if a string is provided, turn on the tooltip tooltipTemplate.text = tooltip; tooltipTemplate.gameObject.SetActive(tooltip.Trim() != ""); As for the passives, add the following line to show all your
availableWeapons
on the Console, so that we can see if active weapons are selected://show the UI upgrade window if there are still availabel upgrades left int availUpgradeCount = availableUpgrades.Count; if (availUpgradeCount > 0) { print("Avail upgrades: " + String.Join(", ", availableUpgrades.ToArray())); bool getExtraItem = 1f - 1f / player.Stats.luck > UnityEngine.Random.value; if (getExtraItem || availUpgradeCount < 4) { upgradeWindow.SetUpgrades(this, availableUpgrades, 4); } else { upgradeWindow.SetUpgrades(this, availableUpgrades, 3, "Increase you Luck stat for a chance to get 4 items!"); } }
Show me the Console output for both of these.
July 5, 2024 at 1:54 pm #15196July 5, 2024 at 3:58 pm #15200::For the tooltip not showing up, it is because your condition is wrong, so you are always going to the 4 item branch:
if (availUpgradeCount > 0) { bool getExtraItem = 1f - 1f / player.Stats.luck > UnityEngine.Random.value; if (getExtraItem
|| availUpgradeCount < 4) { upgradeWindow.SetUpgrades(this, availableUpgrades, 4); } else { upgradeWindow.SetUpgrades(this, availableUpgrades, 3, "Increase you Luck stat for a chance to get 4 items!"); } }As for the passives, they are not showing because you have not added the active weapons to the list of available weapons:
/*
is an emply list that will be filtered from , which is the list of all upgrades in PlayerInventory. Not all upgrades can be applied, as some may have already been maxed out or the player may not have enough inventory slots*/ List<ItemData> availableUpgrades = new List<ItemData>(availableWeapons); List<ItemData> allUpgrades = new List<ItemData>(availableUpgrades); allUpgrades.AddRange(availablePassives); July 6, 2024 at 1:10 am #15205::This works! Although, adding availableWeapons to the list is creating some duplication issues with the weapons. Would this a the fix?
List<ItemData> availableUpgrades = new List<ItemData>(); List<ItemData> allUpgrades = new List<ItemData>(availableWeapons); allUpgrades.AddRange(availablePassives);
July 6, 2024 at 10:59 am #15206 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: