Asking about Ask

This is a command sell #object#

if (not HasAttribute(game.pov.parent, "stock")) {
  msg ("You can't sell stuff here.")
}
else if (not object.parent = game.pov) {
  msg ("You're not carrying " + object.article + ".")
}
else if (HasAttribute(object, "buy")) {
  Ask ("Sell it for " + object.price + " gold?") {
    if (result = true) {
      msg ("You sell " + object.article + " for " + object.price + " gold.")
      game.pov.money = game.pov.money + object.price
    }
    if (GetBoolean(object, "cloneme")) {
      RemoveObject (object)
    }
    else {
      object.parent = game.pov.parent.stock
      SetUpMerchandise (object)
    }
  }
}

All lines work up to and including the Ask line. And the object is recognised.
however in the

msg ("You sell " + object.article + " for " + object.price + " gold.")

line, the object is not recognised as a variable or object.
is this kosher or do I have an error in script?


I used the small ask and it works but with a pop-up which is not very pretty. What's up with that?


I pretty sure what's happening here is that while the Ask script is waiting on a response, Quest will keep running the rest of the code that follows your above code. By the time Quest gets there, the object variable no longer exists, so when the player gives a yes/no response, object.article no longer makes any sense to it. I think you have to put an on ready script at the tail end of all the above code.

A more detailed (but a bit confusing) explanation is in the documentation:

http://docs.textadventures.co.uk/quest/blocks_and_scripts.html


Tried the on ready. no luck.
this works:

if (not HasAttribute(game.pov.parent, "stock")) {
  msg ("You can't sell stuff here.")
}
else if (not object.parent = game.pov) {
  msg ("You're not carrying " + object.article + ".")
}
else if (HasAttribute(object, "buy")) {
  msg ("Sell it for " + object.price + " gold?") 
  get input {
    if (result = "yes") {
      game.pov.money = game.pov.money + object.price
      msg ("Sold!")
    }
    if (GetBoolean(object, "cloneme")) {
      RemoveObject (object)
    }
    else {
      object.parent = game.pov.parent.stock
      SetUpMerchandise (object)
    }
  }
}

just need to finish the "no" part of script .

Thanks Dcoder for once again helping me out.


I'm not sure why that works (get input vs. Ask), but I'm glad it does!


You need to store the object in an attribute if you're using Ask or ShowMenu, because the player answering the question is next turn.

Also, I notice that in that script it asks the player if they want to sell the object - and if they select "No", they give away the object for free instead. I think the code for removing the object from the inventory should be inside the if (result) block.

You'd want something like:

if (not HasAttribute(game.pov.parent, "stock")) {
  msg ("You can't sell stuff here.")
}
else if (not object.parent = game.pov) {
  msg ("You're not carrying " + object.article + ".")
}
else if (HasAttribute(object, "buy")) {
  game.selling_object = object
  Ask ("Sell it for " + sell.object.price + " gold?") {
    object = game.selling_object
    if (result) {
      msg ("You sell " + object.article + " for " + object.price + " gold.")
      game.pov.money = game.pov.money + object.price
  • if (GetBoolean(object, "cloneme")) {
  • RemoveObject (object)
  • }
  • else {
  • object.parent = game.pov.parent.stock
  • SetUpMerchandise (object)
  • }
} } }

I did a lot of editing and messed a few things up for sure, but I have been here many years and knew a code master would appear and get my ducks in a row.

Thanks to you both.


(I would actually have used something like sell.object rather than game.selling_object - my common practice is to save variables like that as attributes of the command itself. But nobody else round here seems to do it that way, so I figured it's less confusing to show the more common method.

One alternative I thought of was giving the sell command a pattern like: ^sell (?<object>.+?)(( for | ?@|) ?(?<text_price>\d+)? ?(gold|g|))$. Then the player can type "sell bucket for 25 gold" and the command runs without confirmation. But if they just type "sell bucket", or if they type a price that's higher than what the shopkeeper will actually offer, it displays a message like "The shopkeeper says he'll give you 25 gold for that bucket, and not a penny more." followed by the options "Yes, {command:sell bucket for 25 gold}" and "{command:No, I changed my mind}". (The "no" command in this case would be a command which just says "OK.", just to make sure the player realises it's a choice. Kind of like a menu, but without needing to store any data)


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums