Shops and Stackables

Hello, I am using both Shoplib and Stacklib. But I made a stack called "food" and I put bread in the market. I want the bread to be stored in the food stack when you buy it. How can I do this?


It's been almost 2 days and I still have not received an answer. I will be pleasured if you help me.


I'm not familiar with either stacklib or shoplib, but can usually answer people's questions by searching for those libraries and taking a quick look over them.

I'll take a look after I've done my work today, but I expect this will be a large job. It might be worth looking over the code for the two libraries, and see if you can understand how they work. The Pixie usually includes some comments to explain how his libraries work. You might find that it's beyond your abilities; but if you get stuck on one part, you can ask for help with that – which is likely to get a quicker response than asking for someone to undertake a larger coding project for you.


OK… it looks like this might not be such a big problem. ShopLib has a function DoBuy, which adds an item to the player's inventory; while StackLib relies on giving a stackable object a take script that moves it to the container instead.

It should be possible to add a couple of lines to the DoBuy function so that it will run the take script rather than just giving the item to the player. Basically, manually running the "take" script on an object that's just been bought.

This may have weird effects on other items that have a script to run when they are taken – if you have any such items, it would be a good idea to test if this causes problems before putting them in the shop. If necessary, it might help to move bought objects to game.pov.parent (put them on the floor next to the player) instead of game.pov (the inventory), and then run DoTake on them, to simulate the whole process of the "take" command.

I believe the desktop editor has a way to copy library functions into your own project so you can edit them; but if you have problems with that you could ask someone who uses the desktop version.

Here's my first attempt at the modified function:

  <function name="DoBuy" parameters="obj">
      if (not HasInt (obj, "price")) {
        msg (DynamicTemplate("NotForSale", obj))
      }
      else {
        buyingprice = BuyingPrice(obj)
        if (buyingprice > game.pov.money) {
          msg (DynamicTemplate("CannotAfford", obj))
        }
        else {
          if (GetBoolean(obj, "cloneonpurchase")) {
            obj = CloneObjectAndMove(obj, game.pov)
          }
          else {
            obj.parent = game.pov
          }
          if (HasScript (obj, "take")) {
            do (obj, "take")
          }
          game.pov.money = game.pov.money - buyingprice
          P (DynamicTemplate("BuySuccessful", obj))
        }
      }
  </function>

This may cause problems for other libraries. If so, I can put together a more flexible system; but it might take a few tries to come up with something that works neatly.

(edit: Actually my second attempt at the function. On the first try I missed the extra obj = which needs to be added so that it will run the take script for the correct clone, rather than the prototype.


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

Support

Forums