Inventory system: Eat food to restore health

How to create an inventory system that restores health when you eat food


So here is the scenario. There is a selection of food scattered throughout your world. If your player eats the food then he will restore some of his health points. Different food has different restorative values. The player can choose when to eat the food, and what to eat.

This tutorial creates an inventory system where you get to equip each of your food items in your hand, and you can choose to eat them, view another, or quit. It will also put back any weapon or other object you were holding when you accessed your food inventory.


The food
Start by placing 3 food objects in your world.
A green apple
A red apple
A hunk of meat

You will see inside their brains that they already have healing qualities. They restore one quarter of the maximum health of the player that interacts with it.
So, if you wanted to you could just change the 0.25 value to whatever you wish and your player would be healed as soon as they interact with the food. But we want the player to choose the moment of healing. So we want him to pick up the food instead.

Remove all Kode and replace with

WHEN [interacted] DO [it][pick up][me]
WHEN [once] DO [numvar:heal value][=][20] //I am using actual numbers for this, but you could use multipliers if you wish

So now, the food objects are placed in the inventory. But how do we get at them to eat them?

First, we need to identify that these are food items. Add at line 1

WHEN [once] DO [global][objset: food][increment by][me]

This identifies the object as a food item.

Your player
Now lets look at the player. Because we are going to be swapping pages in his brain this will cause problems with his follow camera, so delete the line
WHEN DO [follow camera]

Create a logic cube and add

WHEN DO [IWP: your player][follow camera]

Now back to the player brain.

Page 1
For the purposes of testing our healing food, reduce the starting health of the player to 50. You dont need this for the Kode to work, it is just to demonstrate.
WHEN DO [display][health][above]

We are going to use the Left trigger to view our food.

WHEN [LT][pressed] DO [switch page] [2]

on page 2
WHEN DO [display][health][above]
WHEN [page entered] DO [objset:food in inventory][=][inventory][intersect][global][objset:food]

This identifies whether you have any food in your inventory.

WHEN [page entered][equipment]
…WHEN [it][not equal to][objvar:object in hand] DO [objvar:original item][=][it]
…WHEN DO [unequip][it][to inventory]

This identifies if you already have something in your hand (as long as it isn’t your first food item), notes what it is for later and puts it in your inventory.

WHEN [for each of][food in inventory]
…WHEN [current index][=][1] DO [objvar:object in hand][=][it]

This goes through the food items in your inventory and picks one to put in your hand

…/…WHEN DO [equip][objvar:object in hand]

This places the chosen food item in your hand

…/…WHEN [A][pressed] DO [switch page][3]

This is what happens when you want to eat the item – we will Kode this page next

…/…WHEN [LT][pressed] DO [food in inventory][decrement by][objvar:object in hand]
…/…/…WHEN DO [unequip][objvar:object to hand][to inventory]

So, if you dont want to eat it and you press LT trigger again, it removes the item from the food in inventory set, so another one is chosen. It then puts the item back in your main inventory

…/…/…WHEN [objset:food in inventory][count][=][0]
…/…/…/…WHEN [started to] DO [unequip][objvar:object in hand][to inventory]
…/…/…/…WHEN DO [equip][objvar:original item]
…/…/…/…/…WHEN DO [switch page][1]

Now it checks if there are any more items in the food inventory. If not, then it puts the last food item in your hand back into the inventory, equips your original equipped object and goes back to page 1.
If there are more items in the food inventory then it will go through these lines again, placing the next item from the food object set into your hand.

WHEN [RT][pressed DO
…WHEN DO [unequip][objvar:object in hand][to inventory]
…WHEN DO [equip][objvar:original item]
…/…WHEN DO [switch page][1]

This means that if you press Right Trigger the look through the inventory is cancelled.

page 3

WHEN DO [display][health][above]

Here is the Kode for eating.
If you have a Kinect then you can make this more interesting by creating an eating emote. I had the player raise his right hand to his mouth and down again.

WHEN [page entered] DO [play sound][Voice Karlsnor Thanks]
WHEN [page entered] DO [emote][user made: eat]

(if you have this, or any other sound you like, or use the sound on your kinect emote if it has any)

…WHEN DO [numvar:heal value][=][object in hand][heal value]
…WHEN DO [heal][numvar:heal value]

This will heal the player to the value specified in the object it is eating.

WHEN [countdown timer][4] DO [destroy][object in hand]
…WHEN DO [equip][objvar:original item]
…WHEN DO [switch page][1]

And you are done. Test.

Comments are closed.

Powered by

Up ↑

%d bloggers like this: