making a script recognize if you have enough money.
Chocolatepeanut223
11 Feb 2023, 03:01I'm making a mining game on quest and I can't seem to do it. I need to be able to buy items but I can't make the npc realise if you have the money or not. If possible, please just send the code for the if statement and not the whole script for selling.
GeminiNeule
11 Feb 2023, 06:47The concrete code pretty much depends on how you set up your store. One way of doing it would be to only add items that you actually afford to a list and update it every time something is purchased.
Short answer:
if(player.money >= shopitem1.price)
Long answer with examples:
One way I did set up a shop:
shoplist = NewStringList()
if(player.money >= shopitem1.price) {
list add (shoplist , shopitem1.alias)
}
Later on display a menu and have a switch statement to determine which item was bought. Only do it like that for small shops though - there are more efficient ways of doing it. There is also a tutorial on how to setup a shop. You probably want to loop through a list instead of objects in some kind of inventory instead of writing code for every item yourself...
Another way would be to check when trying to buy - just use the if statement above in another location of your script:
switch (result) {
case (shopitem1.description) {
if(player.money >= shopitem1.price) {
// Actually purchasing the item. Assuming that you only can buy that item once. You also need to remove that item from the shop in that case.
DecreaseMoney (shopitem1.price)
AddToInventory (shopitem1)
}
else {
msg("You can not afford " + shopitem.alias)
}
}
}
This example assumes you are using the money feature on the game object and have setup up your shop items with prices (inventory tab of the items).

DarkLizerd
13 Feb 2023, 05:33If you just list what the player can afford, you are not telling him that there are more stuff here.
You would be telling the player that they can buy that +2 dagger, but not tell them that for just 5 more GP, they could buy the +3 dagger.
(Just my 2 copper's worth.)
GeminiNeule
13 Feb 2023, 16:29True, but you can fix this by having two lists: One list with the stuff that you can buy and a preview list of stuff that is available, but can not be afforded.
But I prefer the "check afterwards" option as well.