I have a feeling we might implement this down the line, but it was easy and fun:
Basically just have that TMP item on the ShopUI, Serialize, and disable it on Start. Then in RefreshUI set the enable to !HasInventorySpace.
I have a feeling we might implement this down the line, but it was easy and fun:
Basically just have that TMP item on the ShopUI, Serialize, and disable it on Start. Then in RefreshUI set the enable to !HasInventorySpace.
Weeee… guess who also coded a clear button?
Gross… didn’t realize how off center it was til now. That was fixed.
Well done!
Oooooooh… My clear button came in handy… Now that I am at the lecture with switching between selling and buying… I call the function I made for clear when that button is pressed also… So no lingering quantities there when you switch!
I still have a funny feeling these things may get implemented. If not, add them to your TA notes! They are super simple to implement if a doofus like me managed it!
Just wanted to drop my (possibly) final code for Start, CancelTransaction, and RefreshUI as I am sort of proud of it. It has the insufficient space warning, clear transaction, and updates the color of the Total in the transaction based on buying/selling(Quick edit. Added insufficient funds text like the inventory space one since I use red text to show debit anyway):
private void Start()
{
outOfSpaceText.enabled = false; //Disable out of space indicator from default layout.
noFundsText.enabled = false; //Disable out of gold indicator from default layout.
originalTotalTextColor = totalField.color;
shopper = GameObject.FindGameObjectWithTag("Player").GetComponent<Shopper>();
if (shopper == null) return;
shopper.activeShopChanged += ShopChanged; //Listen for changes to shop/transaction
confirmButton.onClick.AddListener(ConfirmTransaction);
switchButton.onClick.AddListener(SwitchMode);
clearButton.onClick.AddListener(ClearTransaction);
ShopChanged(); //Call ShopChanged to hide the UI panel by default.
}
private void ClearTransaction()
{
currentShop.CancelTransaction();
}
private void RefreshUI()
{
// Remove all existing shop items if they exist.
foreach (Transform kiddo in listRoot)
{
Destroy(kiddo.gameObject);
}
foreach (ShopItem item in currentShop.GetFilteredItems())
{
RowUI row = Instantiate<RowUI>(rowPrefab, listRoot);
row.Setup(currentShop, item);
}
totalField.text = $"Total: {currentShop.TransactionTotal():n0} G"; //Update transaction total
outOfSpaceText.enabled = !currentShop.HasInventorySpace(); //Alert player that they do not have enough inventory space for transaction.
noFundsText.enabled = !currentShop.HasSufficientFunds();
clearButton.interactable = !currentShop.TransactionIsEmpty(); //Disable clear button for no items, enable for items.
confirmButton.interactable = currentShop.CanTransact(); //Enable/disable confirm button if valid transaction.
//Get Swtich Button text and set to current state. Also updates confirm button and total text color.
TextMeshProUGUI switchText = switchButton.GetComponentInChildren<TextMeshProUGUI>();
TextMeshProUGUI confirmText = confirmButton.GetComponentInChildren<TextMeshProUGUI>();
if (currentShop.IsBuyingMode())
{
switchText.text = "Switch To Selling";
confirmText.text = "Buy";
totalField.color = Color.red;
}
else
{
switchText.text = "Switch To Buying";
confirmText.text = "Sell";
totalField.color = Color.yellow;
}
if(currentShop.TransactionIsEmpty()) totalField.color = Color.white; //Set an empty transaction total color to white.
}
Then over in Shop I have a clear transaction. It’s funny because I was not sure if just .Clear() on the dictionary works or not. When I was testing I was dumb and didn’t have my button hooked up to anything when I was trying that way so I wrote this longer method to clear it out as well. Both seem to work. So it was a fun learning experience
public void CancelTransaction()
{
//transaction.Clear(); this will also do the job of the below. Just a fun way to look at it
foreach (ShopItem shopItem in GetAllItems())
{
InventoryItem item = shopItem.GetInventoryItem();
int quantity = shopItem.GetQuantityInTransaction();
AddToTransaction(item, -quantity);
}
if (onChange != null)
{
onChange();
}
}