You can probably work out what today’s overhaul will be. 😉
The old inventory was actually pretty basic in the sense that in your inventory list what fills each slot is pre-set. This makes for a pretty ugly system. What you want is for your inventory to stack.
For this, we need to create some user-defined types first. To explain, in a program you get different ‘types’ of variables, like ‘integer’, ‘string’ and ‘boolean’, a user-defined type is where we get to create our own variables (that can use integers, strings, booleans and so on).
To break this down.
Name: This is pretty clear, it’s what the name of the object will be
Effect: This will be what effect the item will have
Modifier: This will how effective the item is
Count: This is how many of this item you are carrying
So to use this type you must now declare it. We’re going to declare it as an array as we will have multiple items…my system requires your array to be “ItemCount” + 1. So I’ve put 20, leaving spaces for 19 items.
Dim Item(20) as inventory
An array can hold integers, strings, booleans and so on, but it can also hold your user-defined types.
Lets create a function for making our default inventory (what the character starts off with)
The array, as you will see, is basically for each slot. So slot 1 is an item called ‘Stamina’ and slot 2 is ‘Focus’.
You can also create a function to go into your loop. This will check what items are in each slot and update with information such as ‘modifier’ and ‘effect’.
If you want to Display your inventory, you could probably display it as a list and this simple function will do exactly that. This function can be called inside of a loop. (Click to Enlarge)
The result should look a little like this:
What you can do next is create a code where you player can pick up an item.
So, here’s a brand new function for you! (Helps keep things tidy)
We’ll call it ‘LoadItem’, so put it where all of your loading commands.
I’m using a ‘FreeObject()’ function to select an object number for me. If you’re not using one just replace ‘Pick = FreeObject()’ with ‘Pick = [Your Object Number]’. The only arguments for ‘LOADitem()’ are the coordinates to place the position of your object. I’ve created a cube, but in a game you’ll want to load your own object, so replace any commands necessary for your game.
There’s only 1 more function you need to worry about for this tutorial and that’s this:
What this function does is it’ll add the item from slot ‘obj’ and delete the object you’ve just picked up from the scene. This function is called inside of the following code.
This can be put inside of the loop (before UpdateINV).
- We need to check if the object exists (you are deleting it later after all)
- Then, we check if the PLAYER(cha(keats).object) is colliding with the ITEM(pick).
- obj$ is the name of the item we’re picking up. Change this and you change the item.
- We display a message telling the place to pick up the object. Obj$ appears because it’s the name of the object
- Check if the player hits enter
- Check EVERY item slot to see if there’s an item already there (if count = 0 then there’s no item there). If there is a ‘0’, then pick this slot and tell the program to skip to last slot
- Check if the player already has this item. If they do, then use this slot and then skip to the last
- The last slot is ’20’, by telling the program that you’re in slot ’19’ it will read slot ’20’ as the last. Slot 20 is not used, but it’s there to prevent errors (because if the total array is 19, next ‘x’ will be 20 and therefore create an error)
- The Item in the chosen slot will be ‘Obj$’ (or Stamina Plus in this example)
- Call the function for adding the new object.
A Couple of screenshots to show what is happening:
If I were to change obj$ from ‘Stamina Plus’ to ‘Stamina’, you can see the code selects a different slot:
What if you want to use an item? You could try something simple like this:
Basically, by hitting ‘1’ on the keyboard I use the item in Slot 1. If the ‘effect’ is that it restores health, then I add the ‘modifier’ to the character’s HP. If the ‘effect’ is that it restores MP then I add the ‘modifier’ to the character’s MP. Also, remove 1 item from your inventory. All this can be done just so long as the item count is higher than ‘0’.
The ‘wait’ is to stop the input being too sensitive, though there are better ways of doing it, but this is just to keep the tutorial simple.