Shop and Inventory System (Buy, Sell and Equip)

Video tutorial is at end.

For this tutorial you will need:

A player
A shopkeeper
A shop prop (Market stall, table)
A mixture of weapons and shields
A coin
A logic cube
A camera gizmo

You will be building an inventory and shop system. You can use the inventory system without the shop and vice versa, but it all works better as a combined system.

Your weapon and shield brains

Change the kode to read as so:

  1. WHEN [not][equipped][and][not][attached]
  2. …WHEN [interacted] DO [it][pick up][me]
  3. WHEN [once] DO [#sale price][equals][5] // this number is the buy and sale price of the item. If you want to you can separate these into 2 variables or use a percentage for the buying price if you want them to be different.

Coins

Create a logic cube, call it Coins and add the Coin Counter brain to it. Change it like this. Then make it a template object.

  1. WHEN [boolvar:shop] DO [display][Coin][x][plus][numvar:coin counter][x large font][screen center]
  2. …WHEN [else] DO [display][Coin][x][plus][numvar:coin counter][x large font][screen top right]

This will display the coin counter underneath your shop items rather than top right as normal. You can also use this to reduce the size or the way the coin count is displayed by altering the kode here.

Coin brain

We do not want the coins to be part of our inventory. Change it like this:

  1. WHEN DO [turn slowly]
  2. WHEN [detect][player]
  3. …WHEN DO [objvar:player][equals][it]
  4. …WHEN [not][objvar:player][has brain][iwp:Coins] DO [objvar:player][add brain][iwp:Coins] //the logic cube you just made
  5. …WHEN DO [objvar:player][numvar:coin counter][increment by][1]
  6. …WHEN DO [destroy][me]

Delete page 2.

INVENTORY

Player brain

Change it like this.

Page 1 @Player controls – Adventurer

  1. WHEN [page entered] DO [boolvar:exit][equals][false]
  2. WHEN [once] DO [team][equals][team 1]
  3. …WHEN DO [add brain][iwp:Coins]
  4. WHEN [not][global][boolvar:shop]
  5. …WHEN DO [follow camera]
  6. …WHEN DO [display meter][health][screen top left]
  7. …WHEN [left stick] DO [move]
  8. …WHEN [button A][pressed] DO [jump}
  9. …WHEN [button X][pressed] DO [attack]
  10. …WHEN [button Y] DO [shoot]
  11. …WHEN [detect][interactable][objects in front]
  12. …/…WHEN DO[highlight it yellow]
  13. …/…WHEN [is using keyboard] DO [display][middle mouse button][ above ][ it]
  14. …/…WHEN[using controller][or][is using touch] DO [display][icon:B][above] [it]
  15. …/…WHEN [button B][pressed] DO [interact]
  16. …/…WHEN [else]
  17. …/…/…WHEN [button B][pressed] DO [dodge][with strafing][invulnerable]
  18. …WHEN [left bumper][pressed] DO [switch page][@Inventory]

page 2 @ Inventory

  1. WHEN DO [display][Inventory][screen center]
  2. WHEN [equipment][count][greater than][0] DO [display][X large font][text:Press][plus][icon:A][plus][text:If you want to unequip items][screen center][hud stack down]
  3. WHEN [inventory][count][greater than][0] DO [display][X large font][text:Press][plus][icon:B][plus][text:If you want to equip items][screen center][hud stack down]
  4. …WHEN [else] DO [Display][text: You have nothing in your inventory]
  5. WHEN DO [Display][X large font][text:Press][plus][icon:LB][plus][text:If you want to exit the inventory][screen center][hud stack down]
  6. WHEN [button:A][pressed] DO [switch to page][page:@Unequip]
  7. WHEN [button:B][pressed] DO [switch to page][page:@Equip]
  8. WHEN [button:LB][pressed DO [switch to page][1]

page 3 @Unequip

  1. WHEN DO [display][to unequip][screen center]
  2. WHEN DO [Display][X large font][text:Press][plus][icon:LB][plus][text:to exit the inventory][screen center][hud stack down]
  3. WHEN [countdown timer][5][in frames]
  4. …WHEN [button:LB][pressed] DO [switch to][page 1]
  5. WHEN [page entered] DO [#stock number][equals][1]
  6. WHEN [for each of][equipment]
  7. …WHEN DO [it][stock number][equals][current index]
  8. WHEN [button: D pad][right direction][pressed] DO [#choice][incremented by][1]
  9. WHEN [button: D pad][left direction][pressed] DO [#choice][decremented by][1]
  10. WHEN DO [#max][equals][inventory][count]
  11. WHEN DO [#choice][clamp][max][#max][min][0][#choice]
  12. WHEN [for each of][equipment]
  13. …WHEN [#choice][not equal to][it][stock number] DO [display][it][gray][scale][0.5][screen center left] // you can reduce the scale depending on how many stock items you are likely to have
  14. …WHEN [#choice][equal to][it][stock number] DO [display][it][screen center][no HUD stack][priority][2]
  15. …/…WHEN DO [objectvar:item to unequip][equals][it] // this selects the item to unequip from your equipment
  16. WHEN DO [display][icon:Button style][scale][3][screen center][priority][1][no HUD stack]
  17. WHEN [#choice][equals][0]
  18. …WHEN [equipment][count][greater than][0] DO [display][text:Choose an item with the D pad][screen center][hud stack down]
  19. …/…WHEN [else] DO [display][xtra large font][You have nothing equipped][screen center][HUD stack down]
  20. …WHEN [else]
  21. …/…WHEN DO [display][xtra large font][text:Press][plus][icon:A][plus][to unequip][screen center][HUD stack down]
  22. WHEN [countdown timer][5][in frames]
  23. …WHEN [button:A][pressed]
  24. …/…WHEN DO [unequip][to inventory][objvar:item to unequip]

page 4 @Equip

  1. WHEN DO [display][to equip][screen center]
  2. WHEN DO [Display][X large font][text:Press][plus][icon:LB][plus][text:to exit the inventory][screen center][hud stack down]
  3. WHEN [countdown timer][5][in frames]
  4. …WHEN [button:LB][pressed] DO [switch to][page 1]
  5. WHEN [page entered] DO [#stock number][equals][1]
  6. WHEN [for each of][inventory]
  7. …WHEN DO [it][stock number][equals][current index]
  8. WHEN [button: D pad][right direction][pressed] DO [#choice][incremented by][1]
  9. WHEN [button: D pad][left direction][pressed] DO [#choice][decremented by][1]
  10. WHEN DO [#max][equals][inventory][count]
  11. WHEN DO [#choice][clamp][max][#max][min][0][#choice]
  12. WHEN [for each of][equipment]
  13. …WHEN [#choice][not equal to][it][stock number] DO [display][it][gray][scale][0.5][screen center left] // you can reduce the scale depending on how many stock items you are likely to have
  14. …WHEN [#choice][equal to][it][stock number] DO [display][it][screen center][no HUD stack][priority][2]
  15. …/…WHEN DO [objectvar:item to equip][equals][it] // this selects the item to equip from your inventory
  16. WHEN DO [display][icon:Button style][scale][3][screen center][priority][1][no HUD stack]
  17. WHEN [#choice][equals][0]
  18. …WHEN [equipment][count][greater than][0] DO [display][text:Choose an item with the D pad][screen center][hud stack down]
  19. …/…WHEN [else] DO [display][xtra large font][You have nothing in your inventory][screen center][HUD stack down]
  20. …WHEN [else]
  21. …/…WHEN DO [display][xtra large font][text:Press][plus][icon:A][plus][to equip][screen center][HUD stack down]
  22. WHEN [countdown timer][5][in frames]This kode ensures that you do not drop any items when you equip another, and that they go into the inventory
  23. …WHEN [button:A][pressed]
  24. …/…WHEN [for each of][equipment]
  25. …/…/…WHEN [it][is shield] DO [objvar:shield][equals][it]
  26. …/…/…WHEN [else] DO [Objvar:weapon][equals][it]
  27. …WHEN [objvar:item to equip][is shield]
  28. …/…WHEN DO [unequip][to inventory][objvar:shield]
  29. …/…WHEN DO [equip][objvar:weapon]
  30. …/…WHEN [else]
  31. …/…/…WHEN DO [unequip][to inventory][objvar:weapon]
  32. …/…/…WHEN DO [equip][objvar:shield]
  33. …/…WHEN DO [equip][item to equip]

SHOP

Place your stall, shopkeeper in place. Add a camera gizmo and use the Fixed Camera Editor to look at the shopkeeper.

Camera 

  1. WHEN [has power] DO [Fixed camera][Fixed Camera 1]

Stall/Shop/Table

  1. WHEN [interacted] DO [boolvar:shop][equals][true]
  2. …WHEN DO [objvar:shopper][equals][it]
  3. …WHEN DO [iwp:shopkeeper][objvar:shopper][equals][it]
  4. WHEN [boolvar:shop]
  5. …WHEN DO [iwp:Camera Gizmo][power on]
  6. …WHEN DO [objvar:shopper][visible][equals][false]
  7. …WHEN DO [iwp:shopkeeper][boolvar:shop][equals][true]
  8. …WHEN DO [objvar:shopper][boolvar:shop][equals][true]
  9. WHEN [button:right trigger][pressed]
  10. …WHEN DO [iwp:Camera Gizmo][power off]
  11. …WHEN DO [objvar:shopper][visible][equals][true]
  12. …WHEN DO [iwp:shopkeeper][boolvar:shop][equals][false]
  13. …WHEN DO [objvar:shopper][boolvar:shop][equals][false]
  14. …WHEN DO [boolvar:shop][equals][false]

Shopkeeper

Decide which objects are going to be in the shopkeepers stock.

page 1

  1. WHEN [once] DO [objset:stock][equals][iwp:item][plus][iwp:item] etc….
  2. …WHEN [for each of][objset:stock] DO [pick up][it]
  3. WHEN [boolvar:shop]
  4. …WHEN DO [display][icon@B][screen top right]
  5. …WHEN DO [display][text:Selling][screen top right]
  6. …WHEN DO [display][icon@A][screen top left]
  7. …WHEN DO [display][text:Buying][screen top left]
  8. …WHEN DO [say][text:Are you buying or selling?][screen bottom center]
  9. …WHEN DO [display][icon:RT][text:to exit shop][screen top center]
  10. …WHEN [countdown timer][5][in frames]
  11. …/…WHEN [button:A][is pressed] DO [switch page][page:@Selling]
  12. …/…WHEN [button:B][is pressed] DO [switch page][page:@Buying]

page 2 @Buying

  1. WHEN DO [display][large font][text:Buying][screen top right]
  2. WHEN DO [display][icon:RT][text:to exit shop][screen top right]
  3. WHEN DO [display][icon:LT][text: to sell][screen top right]
  4. WHEN [button:RT][pressed DO [switch page][1]
  5. WHEN [countdown timer][5][in frames]
  6. …WHEN [button:LT][pressed] DO [switch to page][page:@Selling]
    Selecting an item
  7. WHEN [page entered] DO [#stock number][equals][1]
  8. WHEN [for each of][inventory] DO [it][#stock number][equals][current index]
  9. WHEN [D pad][right direction][pressed] DO [#choice][incremented by][1]
  10. WHEN  [D pad][left direction][pressed] DO [#choice][decremented by][1]
  11. WHEN DO [#max][equals][inventory][count]
  12. WHEN DO [#choice][equals][clamp][max][#max][min][0][#choice]
  13. WHEN [for each of][inventory]
  14. …WHEN [#choice][not equal to][it][#stock number] DO [display][it][color:gray][scale][0.5][screen center left][HUD stack right]
  15. …WHEN [#choice][equal to][it][#stock number] DO [display][it][screen center][no HUD stack][priority][2]
  16. …/…WHEN DO [objvar:item for sale][equals][it]
  17. WHEN DO [display][icon:Button style][scale][3][screen center][no HUD stack][priority][1]
  18. WHEN [#choice][equal to][0]
  19. …WHEN [inventory][count][greater than][0] DO [display][x large font][text: Choose an item with the D pad][screen center][HUD stack down]
  20. …/…WHEN [else] DO[display][x large font][text: Sorry we have no stock][screen center][HUD stack down]
  21. …WHEN [else] DO [display][objvar:item for sale][#sale price][plus][text: gold coins][screen center][HUD stack down]
  22. …/…WHEN DO [display][text:Press][plus][icon:A][plus][text:to buy][screen center][HUD stack down]
    Buying an item
  23. WHEN [button:A][is pressed]
  24. …WHEN DO [#sale][equals][objvar:item for sale][#sale price]
  25. …WHEN [objvar:shopper][#coin count][greater than or equal to][#sale]
  26. …/…WHEN DO [play sound][UI Collect coin]
  27. …/…WHEN DO [drop][objvar:item for sale]
  28. …/…WHEN DO [objvar:shopper][pick up][item for sale]
  29. …/…WHEN DO [objvar:shooper][#coin count][decremented by][#sale]
  30. …/…WHEN [else] Do [play sound][Alarm Clock Beep]

page 3 @Selling

  1. WHEN DO [display][large font][text:Selling][screen top right]
  2. WHEN DO [display][icon:RT][text:to exit shop][screen top right]
  3. WHEN DO [display][icon:LT][text: to buy][screen top right]
  4. WHEN [button:RT][pressed DO [switch page][1]
  5. WHEN [countdown timer][5][in frames]
  6. …WHEN [button:LT][pressed] DO [switch to page][page:@Buying]
    Selecting an item
  7. WHEN [page entered] DO [#stock number][equals][1]
  8. WHEN [for each of][objvar:shoppper][inventory] DO [it][#stock number][equals][current index]
  9. WHEN [D pad][right direction][pressed] DO [#choice][incremented by][1]
  10. WHEN  [D pad][left direction][pressed] DO [#choice][decremented by][1]
  11. WHEN DO [#max][equals][shopper][inventory][count]
  12. WHEN DO [#choice][equals][clamp][max][#max][min][0][#choice]
  13. WHEN [for each of][shopper][inventory]
  14. …WHEN [#choice][not equal to][it][#stock number] DO [display][it][color:gray][scale][0.5][screen center left][HUD stack right]
  15. …WHEN [#choice][equal to][it][#stock number] DO [display][it][screen center][no HUD stack][priority][2]
  16. …/…WHEN DO [objvar:item for sale][equals][it]
  17. WHEN DO [display][icon:Button style][scale][3][screen center][no HUD stack][priority][1]
  18. WHEN [#choice][equal to][0]
  19. …WHEN[shopper] [inventory][count][greater than][0] DO [display][x large font][text: Choose an item with the D pad][screen center][HUD stack down]
  20. …/…WHEN [else] DO[display][x large font][text: You have nothing to sell][screen center][HUD stack down]
  21. …WHEN [else] DO [display][objvar:item for sale][#sale price][plus][text: gold coins][screen center][HUD stack down]
  22. …/…WHEN DO [display][text:Press][plus][icon:A][plus][text:to buy][screen center][HUD stack down]
    Selling an item
  23. WHEN [button:A][is pressed]
  24. …WHEN DO [#sale][equals][objvar:item for sale][#sale price]
  25. …WHEN DO [play sound][UI Collect coin]
  26. …WHEN DO [shopper][drop][objvar:item for sale]
  27. …WHEN DO [pick up][item for sale]
  28. …WHEN DO [objvar:shooper][#coin count][incremented by][#sale]

3 thoughts on “Shop and Inventory System (Buy, Sell and Equip)

  1. i am super confused, are people still making these games? I thought it closed down? I had hundreds of hours invested in this platform… ?

    Like

    1. Yes, people are still making games in Project Spark. They are sharing their creations as visual presentations through YouTube and the Xbox Clubs (the best one is Project Spark Team). We are currently experimenting with Mixer and the Shared controller feature, but this is still in beta test and has some lag problems which makes playing each others games a bit problematic. On PC they can use SparkShare to share game files and prop files so they can play each others creations. SO, if you want to share anything you have made please join the club, make a video through capture and post it to the club feed.

      Liked by 1 person

      1. you are actuallu bowing my mind. when I started playing in 2014 I spent hundreds of hours playing with environments and Kode, nobody even knew what I was going on about. Friends I knew that were going to school for game design didnt even know. I woukd so much like to learn more aboit this but i will take me a while to read your blog. uou have done an excellente Job documenting! And for that we thank you! Please try my twitch.tv/outlawtour channel for a three hour stream of one of the environments

        Like

Comments are closed.

Website Powered by WordPress.com.

Up ↑