Another money question

J_J

I realize this is probably really obvious, but I can't figure it out. For some reason the player can buy items they don't have enough money for, and they just end up with a negative balance. Is there a built in feature that I can turn on to stop that, or do I need to actually write that in to the code somehow?

Thanks >_>'


There is nothing built-in to handle buying and shopping but there is a tutorial:
http://docs.textadventures.co.uk/quest/shop.html


J_J

Thank you!

I think I'm a bit confused about where I put this code. Every room in my game involves things you can buy, and you often have to pay money in order for objects to function properly. But none of the rooms are shops or function as shops in a conventional sense. Is there a way to tell the game never to let someone buy something if they don't have the money, or do I need to somehow put this in manually attached to every object you can buy? Also, I have multiple if scripts already running on objects based on choices you've made that change how you can interact with them. If the code has to be attached to individual objects where do I put it in comparison to the other if scripts?

Honestly, everything is working how I want for buying things (in terms of how the objects move and interact with each other when you pay money), the only issue is that you can buy things when you don't have money. Really what I want is just something that pops up a message if a player tries to buy something they can't afford, and stops the buy from happening...

Basically, I don't know what I'm doing ; _ ;


Use an else script.

This is my own code. This was copy-pasted from my deviant art journal, so the brackets are way off. But you get the gist of it. Next are the buying commands. These are a little complicated. Before you start, make an object to be the shop first. There is also an option involving "price," which I have not figured out yet. But if you change the "player.gold" to "player.price," it should work itself out, somehow.

Buy command
I'm pretty sure "buy" is already a command that comes with quest, so it will have to be capitalized or "buy1". But name it whatever you wish.

Edit: It is possible to disable an existing default command.
How to make new commands/replace commands:
http://textadventures.co.uk/forum/quest/topic/zhzl_m8hd0qoookndtqrqq/inheriting-commands-from-the-direct-parent-object-is-it-possible
Hh
Type Buy1 #object# as the command pattern. (Or whatever you want.)

Paste this (below) in.
msg ("See something that catches your eye?")
options = Split("Potion (50);Hyper Potion (200)", ";")
ShowMenu ("Shop", options, true) {
switch (result) {
case ("Potion (50)") {
if (player.gold >= 50) {
player.gold = player.gold - 50
player.potion = player.potion + 1
msg ("You bought a Potion.")
}
else {
msg ("You don't have enough gold.")
}
}
case ("Hyper Potion (200)") {
if (player.gold >= 200) {
player.gold = player.gold - 200
player.hyper_potion = player.hyper_potion + 1
msg ("You bought a Hyper Potion.")
}
else {
msg ("You don't have enough gold.")
}
}
}
}

Sell command
I'm pretty sure "sell" is also a command in Quest, so you'll have to name it something else.
Type Sell #object# as the pattern.
Paste this (below) in.
Derp
msg ("What do you have?")
options = Split("Potion (50);Hyper Potion (200)", ";")
ShowMenu ("Shop", options, true) {
switch (result) {
case ("Potion (50)") {
if (player.potion > 0) {
player.gold = player.gold + 50
player.potion = player.potion - 1
msg ("You sold a potion.")
}
else {
msg ("You don't have any Potions.")
}
}
case ("Hyper Potion (200)") {
if (player.hyper_potion > 0) {
player.gold = player.gold + 200
player.hyper_potion = player.hyper_potion - 1
msg ("You sold a Hyper Potion.")
}
else {
msg ("You don't have any Hyper Potions.")
}
}
}
}
Derp
There's a neat trick you can do where you go up to the "Move" option in the command (top-right corner), select the room of the shop, and move the command there. This makes sure that you can only use the command in that room. It doesn't do much else as far as I know, just neat if you only want a player to buy in a certain area.

You can also make multiple buy and sell commands for different shops. The name 'purchase' works, and so does 'trade'.


transactions (buying/selling), a scripting example (in code):

buying:

// katana.parent = shop_owner // the shop owner initially has the katana

if (player.currency >= katana.price) { // checking if you got enough to buy the katana

  player.currency = player.currency - katana.price // buying the katana (subtracting your currency by the price of the katana)

  shop_owner.currency = shop_owner.currency + katana.price // buying the katana (adding the price of the katana to the shop owner's currency)

  katana.parent = player // you now have (via moving/setting) the katana in your possession/inventory

  msg ("You buy the katana")

} else {

  msg ("You don't have enough currency to buy the katana")

}

selling:

// (as is common in RPGs, you sell something at half price, hence why I got the: katana.price / 2, in the code below for selling)

// katana.parent = player // you initially have the katana

if (shop_owner.currency >= (katana.price / 2)) { // checking if the shop owner got enough to buy the katana from you

  shop_owner.currency = shop_owner.currency - (katana.price / 2) // selling the katana (subtracting shop owner's currency by half of the price of the katana)

  player.currency = player.currency + (katana.price / 2) // selling the katana (adding half of the price of the katana to your currency)

  katana.parent = shop_owner // shop owner now have (via moving/setting) the katana in his possession/inventory

  msg ("You sell the katana")

} else {

  msg ("The shop owner doesn't have enough currency to buy the katana from you")

}

here's a more detailed guide on Attribute and the 'if' Script usage:

http://textadventures.co.uk/forum/samples/topic/5559/attributes-and-if-script-guide-by-hk

ask if you got any questions and/or need help with anything


J_J

Thank you! You guys are the best. It looks like I was putting the code in the wrong place, but everything is now working as it should. I have never been so happy to be told I couldn't afford something. Ha.


Log in to post a reply.

Support

Forums