Creating an player inventory

Hi!
I’m trying to create a quest and have some minimal experience in block coding. Is there a way to “give” players items, like coins etc and save them in an inventory for them to use later on another object?
Thanks,
Noa

Hi @NoaLahav and welcome to the forums!

you can save items as variables and perform a check at a later time to see if the item that is required has been picked up before. Below is an example on how to achieve that.

Watch example

Blockly code

How it works

  • We make a variable that keeps track of all items in our inventory. We call it “inventory” and make it an empty list at start
  • When the starfish is clicked, it is added to the inventory list. We then hide the item out of sight of the player
  • When the door is clicked, we check if the starfish can be found in the list. If yes, the door opens. If not, the door tells us to take the starfish

(the list ... find first occurence of item - block will return the number 0 if the item we’re looking for is not found. We check if the returned number is not 0 so we know that the item we’re looking for has been found).

Blockly code in action
CollectItem

I hope this example helps! It’s already using programming concepts like lists, which make your inventory system more flexible but a little more challenging to get into. Below, you can find a simpler version in which the inventory can only hold one item at a time.

Watch simplified example code

image

2 Likes

Thanks! I’ll have to learn more coding skills - just for the best. Do you have an example game with an inventory?

Best,
Noa

Hi @NoaLahav!

The Egg Hunt space in our gallery is using similar code to keep track of the toys that the player collects. You can find this space in the public gallery.

You can make a copy of the Egg Hunt - space and read through the Blockly code to learn how it’s been made. Have fun!

Show copy example

MakeCopyOfSpace

1 Like

How do you open inventory?
Thanks

Hi @Kennedy_Kessling,

You need to create an inventory for your game, using variables, or lists. The Egg Hunt CoSpace Stefan refers to above is still available to remix:

Hope that helps! Let me know if you have any further questions about this.

Geoff @ TechLeap

1 Like