Hello everyone, I am currently working on an item shop interface, and noticed that it does have a fatal flaw.
The passage generates a list of links, each triggering the buy menu, allowing the player to enter the amount of items to be bought into a textbox. As long as the player actually types in a number, that works as intended; however, there is no obvious way of preventing the player from trying to buy "zrgdfghjo" amount of potions, which in turn breaks both the player's money and the remaining number of potions in the shop's inventory. Now I'd be perfectly happy if there was a way to limit the damage - say, a check to see if a variable is not actually an integer, and overwriting it with 0 in that case, not buying anything in that particular instance. Is there any solution to this, or should I remove the textbox and replace it with something a bit safer?
Thank you for your time.
<<textbox "_buyThisMany" _buyThisMany>>
<<button "Buy">>
<<if (ndef Math.trunc(_buyThisMany))>> /*this is the part in question*/
<<set _buyThisMany = 0>>
<<else>>
<<set _buyThisMany = Math.clamp(Math.clamp(_buyThisMany, 0, Math.trunc($player.money / $itemList[_activeShop.stock[_k].index].value)), 0, _activeShop.stock[_k].amount)>> /*works flawlessly from here on out*/
<</if>>
<<set _buyThis = {
index: _activeShop.stock[_k].index,
amount: _buyThisMany
}>> /*define the object we're buying*/
/*do the buying magic (code irrelevant to problem)*/
<</button>> (<<print _activeShop.stock[_i].amount>> in stock)