Help writing an expression?

I’m implementing a shop into my game and I’m trying to use an object variable structure to have each item’s name, price, description, etc show up on a new layer when the player touches the item in a shop. I set up the variable structure, entering the data for each item. What I want to be able to do is to have each shop item’s name, description, and price pop up on the new layer, dynamically changing the item’s data (name, description, price) with the minimal amount of code, rather than having to go through and make several different events to show the proper item data.

I know this can be done with an expression, but I’m having trouble figuring out how to write a proper one. Below are some screenshots which should help to make my goal clear:

This first screenshot shows the variable structure I’ve set up for the shop item’s hitbox object, which will be the trigger object to get the text to pop up showing the item name, description, and price.

This second one shows the variable structure I’ve set up to control the price of an item depending on what shop it is for sale at.

This final screenshot shows the code I’m using now, which works but would have to be repeated for every single item that will be on sale at every shop. This seems clunky and could be avoided with an expression, if I could get one working.

Any tips for how to write an expression to achieve the result I’m looking for? I know I might have to rework the variable structures to get them to work in the expression, so I’m expecting that. Thanks in advance!

You are using an item id (a number) to identify the item with ShopItemSlot.ItemForSale. But your items are uniquely identified by a key (a string) like “ArrowX5”, BombX4" etc (not the ItemName, but the first layer of names in the ShopItemData structure.

You’ll need to have a common identifier first - either the number as the ShopItemData identifier, or change ShopItemSlot.ItemForSale to a string (my preference)

If you do the latter, and change the Number variable ItemID to a string variable ItemName, then you can access the data with ShopItemSlot.VariableString(ShopItemData[VariableString(ItemName)].ItemName) and ShopItemSlot.VariableString(ShopItemData[VariableString(ItemName)].Description)

So instead of ShopItemData.BombX4.ItemName, you are using the ShopItemData["BombX4"].ItemName format, but with the “BombX4” be replaced by VariableString(...).

1 Like

Excellent information, Mr. Men! I’m definitely going to give this a shot. To make one thing clear before I do, in the last sentence of your post you said “BombX4” should be replaced by VariableString(…). What is supposed to go inside the parentheses where you have the “…”? How would it know which item’s data to display the ItemName and Description of?

So if I go back through and change the number variables for 1) ItemForSale of ShopItemSlot, and 2) ItemID of ShopItemHitbox to strings which are the same…the hitbox’s variable will match the ShopItemSlot’s variable once again, except as a string instead of a number. I guess I’m just failing to see how when the player steps on the hitbox, it will generate the proper ItemName and Description for the item the hitbox is for. Could you please explain this for me?

To be clear, it’s not that I don’t trust your reply…I certainly do. I’m just looking to gain a better understanding of how these things work so I can hopefully start writing more complex expressions on my own. I don’t just want the solution, but an understanding of why it works so I can learn.

Thank you so much once again, and I look forward to hearing your response!

Sorry for the long delay - I didn’t realise I had left this unanswered.

The values as shown in the preceding paragraph in my post:
image

1 Like