Monday, December 2, 2013

Keeping track of inventory!

There have been many students wondering how to keep track of your inventory in order to win the game.

Here is one way to do it. I created several variables, one for each hidden sprite, and an inventory variable.

Simply click on Data and then press on the Make a Variable button.

You should name your variables in a way that makes sense to you.
The ones I used were:
  • found_sledge
  • found_paints
  • found_key
  • painting_clicked
These are the basic ones that keep track of the four items that a user needs to find in order to escape the room.

My other variables are used to keep track of other things in my game.
Next we need a way to use these variables on our sprites!

In my game I use the key to escape the room. I have two keys, one that is hidden in the room and the other is an inventory key that is used on the door.

Here is what the scripts look like for my hidden sledge in the room (click on the image to enlarge):
First when the green flag is clicked the variables are set to zero.
Next when a user clicks on my sledge sprite a few things happen:
  • inventory increases by one
  • found_sledge increases by one

The found_sledge variable allows me to keep track if the user has found the sledge hammer. I can then use this variable to control if this sprite should be hidden or shown. This is useful when coming back from your art history. If the hidden sledge hammer were to show up again then the user could cheat the game.

I can also use the same hide / show convention on the inventory sledge hammer sprite:
Notice that depending on the value of found_sledge the sprite is either hidden or shown.

When I come back_to_main or when the user does not have 4 inventory items. i.e. lose.

My back_to_main broadcast is when the user comes back from the art history backdrop.

The lose script us used when the user does not have the 4 inventory items. This will make more sense if you keep reading.

Finally lets see how we can check if the user has in fact found 4 items.

Here is what my inventory key script looks like:

First if the user touches the door then I need to check if the user has found 4 items.

This script broadcasts to the stage to run the check script.

If the user has found 4 items then the check script broadcasts that the user wins.

If they haven't found 4 items then the check script broadcasts lose.

If the user loses then the, When I receive lose script runs: First the key goes back to its spot in the inventory box, then it says you need to find (4-inventory) more thing(s).

I keep my check script on the stage with the other scripts that control the backdrops.
Here is my check script:
If inventory = 4 then I broadcast you_win and the backdrop switches to You Win.

However if inventory does not = 4 then I broadcast lose and the backdrop switches back to the main room or in my case to the studio.

I hope these scripts help you in trying to complete your game! If you have any questions please see me in the classroom or in the lab!

Happy Scratching!!!