Creating an player inventory


#1

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


#2

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


#3

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

Best,
Noa


#4

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