ShopLib error when typing 'buy' in the shop

Burgric
09 Dec 2022, 17:37

Hi there,

I have followed the tutorial to create a shop (created a room, and then a stock room inside, then added an object attribute in the shop for 'shopstock', and set it to the stock room.

I then created an item in the stock room, and set its price in the 'inventory' tab.

When I type 'buy' in the shop, however, I get this error and the price of the item just reads '0 gold pieces':
'Error running script: Error compiling expression 'price * (rate + game.pov.parent.markup) / 100': ArithmeticElement: Operation 'Add' is not defined for types 'Int32' and 'Object'
Item to purchase (have 0 gold pieces)'

My thinking is that ShopLib is having difficulty presenting the price for the item, and this is causing the error.

Am I doing something wrong?

Thanks


mrangel
09 Dec 2022, 22:10

I haven't used ShopLib, but I know how to read error messages.

'Error running script: Error compiling expression 'price * (rate + game.pov.parent.markup) / 100'

This tells you that there is an error in the expression quoted

ArithmeticElement: Operation 'Add' is not defined for types 'Int32' and 'Object'

The expression is failing because it's trying to add an Int32 (a whole number) to an Object (which isn't a number, so it doesn't make sense to try addition on it)

The only addition in the expression is (rate + game.pov.parent.markup) … which tells us that rate is a number (good), and game.pov.parent.markup is an object.

As far as unpacking that…
game is the special object representing the game
game.pov is an attribute pointing to the object the player is currently controlling
game.pov.parent is the room that game.pov is currently in (presumably the shop)

So… this script expects shops to have an attribute named markup with a numeric value. I'd guess it means how much that shop marks up the prices by. So try giving the shop a markup attribute, and see if it works.

(The reason it says "Object" is that the special value null (which means "no such attribute") is technically an object)


Burgric
13 Dec 2022, 10:29

Hi,

Thanks for the breakdown, much appreciated.

I have added a markup attribute to the shop, and set it to an integer.

When I attempt to use the 'buy' command in the text box, I now get this error:

'Error running script: Error compiling expression 'price * (rate + game.pov.parent.markup) / 100': ArithmeticElement: Operation 'Multiply' is not defined for types 'Element' and 'Int32'
Item to purchase (have 0 gold pieces)'.

The first part of the error appears the same as before, but the latter includes the new 'Multiply' operation.


mrangel
14 Dec 2022, 08:32

I'd guess the problem is now with the price variable. I'll try to look at the library when I get to my computer, and see where that comes from.


Burgric
01 Jan 2023, 16:20

Hi, I'm still stuck on this. Wondered if anyone had a solution?

Thanks!


mrangel
01 Jan 2023, 23:07

The version of ShopLib I can find doesn't include the lines of code shown in your error message. There's no reference to a markup variable.

Have you modified the BuyingPrice function? Or can you give a link to the version of ShopLib you are using?


Burgric
03 Jan 2023, 08:31

Oh, that's weird. I haven't modified the BuyingPrice function, just installed the library as normal.

This is the version I have been using, as linked on The Pix's Github page, originally following the links from the text adventure documentation page:

https://github.com/ThePix/quest/blob/master/ShopLib.aslx


mrangel
03 Jan 2023, 10:25

Yeah… the expression price * (rate + game.pov.parent.markup) / 100 is not on that page.

I would expect to see an expression like that if someone had modified the library to include shops which charge more than others (although in that case, I would expect it to be obj.price * (rate + game.pov.parent.markup) / 100).

See if you can find it in the function BuyingPrice. If not, try searching your game for that expression; or search for it in your copy of the library. I'm not sure if the offline editor has a search function; if not, you could open the .aslx file in Notepad or similar and just search for that piece of code.

If we can see the function that includes that expression, it would be easier to work out what's wrong with it.


Burgric
16 Jan 2023, 20:05

Okay. I should start by saying this is fixed now. Huzzah!

The short answer: I uninstalled and reinstalled the ShopLib library. I don't know how I missed something this simple, but this was the solution. Before trying this, though, I did some investigating into the BuyingPrice function, and found the expression in question. Out of interest, I wonder how this conflicted with ShopLib before reinstalling it.

The 'BuyingPrice' function was in 'Items.xml', one of 9 extra files that come with CombatLib.aslx in the install zip file:

      if (game.pov.status = "Merchant's Tongue") {
        rate = 75
      }
      else {
        rate = 100
      }
      return (price * (rate + game.pov.parent.markup) / 100)
  ]]></function>```

  <!--
  Sets the mark up for selling goods across the world to be their price attribute.
  -->
  <function name="SellingPrice" parameters="price" type="int"><![CDATA[
      if (game.pov.status = "Merchant's Tongue") {
        rate = 125
      }
      else {
        rate = 100
      }
      return (price * (rate - game.pov.parent.markup) / 200)
  ]]></function>

I had thought, when comparing this to ShopLib's aslx file, that CombatLib maybe had its own shop code, and so ShopLib wasn't needed. I uninstalled ShopLib and made a shop but the 'buy' command did nothing, so I reinstalled ShopLib, thinking I could copy the BuyingPrice code from ShopLib's aslx to overwrite that in CombatLib's. But I didn't need to in the end.

Thanks for all your help with this. Can't believe it was such a simple solution!

mrangel
17 Jan 2023, 09:08

That looks like it's designed to work with a different version of shoplib… the parameter to BuyingPrice is the item's base price, rather than the object being bought. To make the "Merchant's Tongue" status effect work with ShopLib, it would need to be:

  <function name="BuyingPrice" parameters="obj" type="int"><![CDATA[
      if (game.pov.status = "Merchant's Tongue") {
        rate = 75
      }
      else {
        rate = 100
      }
      return (obj.price * (rate + game.pov.parent.markup) / 100)
  ]]></function>```

  <function name="SellingPrice" parameters="obj" type="int"><![CDATA[
      if (game.pov.status = "Merchant's Tongue") {
        rate = 125
      }
      else {
        rate = 100
      }
      return (obj.price * (rate - game.pov.parent.markup) / 200)
  ]]></function>

Burgric
18 Jan 2023, 18:54

Oh! I thought I downloaded the most recent versions of these libraries, from Pix's links. That makes sense now.

Thanks for the fix. I may change this code in the future if/when I implement the spell.

I have since fixed another issue, and wonder if this is also because of a version incompatibility. The 'sell junk' command did not work as expected, as items flagged for 'destroy on sale' would technically 'sell', as the player was given money, but the item remained in the inventory and an error occurred with the dynamic template for 'selljunk'.

I looked at the code featured in the error and found in the ShopLib file:

(DynamicTemplate("SellSuccessful", obj))
object.parent = null

I changed this to:

(DynamicTemplate("SellSuccessful", object))
object.parent = null

And it now works. Do you think this is to do with the version, also?


Lesternixon
29 Jan 2023, 15:57

I reinstalled the ShopLib library after uninstalling it. This was the answer, and I don't see how I could have overlooked something so obvious. I first looked at the BuyingPrice function and discovered the problematic expression before trying this. Before reinstalling it, I was curious as to how this interfered with ShopLib.