Sitemap
Sean Duggan

Sean Duggan

Functional Item Selection

2 min readJan 5, 2025

Alright, so there are a number of ways that we could handle the buttons. I would like the player to be able to select an item to buy by clicking on it. First thing that came to mind, especially if I want only one item to be selectable at a time, is Toggles. I don’t particularly need the checkbox part, so I just removed the Button component and replaced it with a Toggle.

What’s the difference? For one, the button will now track whether it’s selected or not. For two, I can set up a Toggle Group that will allow me to make only one item active at a time. But without the checkbox, how do I tell if it’s been selected? Well, that’s fairly simple. We add a Panel to the button, with the Fill positioning, and the color that we want as an overlay. Then, we set the Graphic to that Panel. And, it works.

Get Sean Duggan’s stories in your inbox

Join Medium for free to get updates from this writer.

Press enter or click to view image in full size

If I want only one item selectable at a time, as aforementioned, I could assign them to a Toggle Group (which is actually done by creating the Toggle Group and then pointing to it from each Toggle). Turning on Allow Switch Off will let you click on the currently active item to turn it off. But, this still leads to the question of, how do we make these select the correct items? Well, easiest way is to simply poll our Toggle Group for the active items.

public void BuyItems()
{
foreach (Toggle item in _itemsToggleGroup.ActiveToggles())
{
switch (item.name)
{
case "Flaming Sword Button":
Debug.Log("Flaming Sword");
break;
case "Boots of Flight Button":
Debug.Log("Boots of Flight");
break;
case "Castle Key Button":
Debug.Log("Castle Key");
break;
}
}
}

Actual code would deduct the price of the item and enable it for the character. But that’s something that we’ll set up in the near future. As it is, a part of my brain is itching to refactor this so that all of the item info is in one place so that I don’t need switch statements to translate…

Press enter or click to view image in full size

--

--