Inventory with no arrays for a platformer/sidescroller game.

3
  • 15 favourites

Attached Files

The following files have been attached to this tutorial:

.capx

platformer-inventory-no-arrays.capx

Download now 187.28 KB

Stats

2,890 visits, 4,093 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

If you're a stubborn idiot like me arrays are impossible... So, I knew it was possible to make an inventory with no arrays even though it might be a bit more confusing. But hey? It works!

So basically we create this with the help of sprites; "inventory", "selection", and our items that we manually move to the inventory when picked up, and return back to the player position when we drop them. This tutorial is built on the basic platformer example that Construct 2 provides with the software, so it is a good idea to open it up if you want to copy this from scratch.

We start with making our inventory have specific points where we can easily put our items later. Click on the "set origin or image points" button, you can set your origin however you like, but create new image points for each item slot and position them on the parts where you want your items to go. In this case I made new image points in the middle of every square. Name them 1,2,3,4 for the future usage.

Make sure your selection sprite has their origin sprite right in the middle of the sprite.

We also need a Keyboard object so make sure to add one by right clicking the Layout and inserting a new object, which in this case will be the Keyboard object.

Create multiple sprites for the items, just tiny colored boxes is fine for tutorial purposes.

Now create two families which we need. "ground" where you add all your solid objects and "item" where you will add all our items. Give both of the families Physics behavior, make sure to make the ground behavior "Immovable"

We also need some layers, I have 3 on this tutorial, but we basically only need 2 for this.

2 = hud

1 = game

0 = background

And now for the inventory sprite, set number variables slot1-4 so we can check if the slot is empty (0) or filled (1)

And, also and a "position" number variable for the selection sprite. so we can check what position the selector has. 0 being not active, and 1-4 being the inventory slots.

So, first we make picking up items. When the player is overlapping any sprite from the item family, presses E and cursor position is 0.

Make a sub-event which determines what slot we will be using.

Starting with slot1, if slot 0 is empty, we move the item to layer 2, which is our hud layer.

Set position to (inventory.imagepoinx(1), inventory.imagepointy(1) )

This is where we are setting our item on the custom image points earlier.

Set slot1 to 1, means the slot is now filled so we can only have one item per slot.

Set physics disabled, means that our item now on our hud won't just fall off! :D

Now our other condition. Make sure to have System Else in front or otherwise all the code will run at once and will not work.

With this we check if slot 1 is filled, but slot 2 is not, so we do the same thing for slot 2 and now it will be filled.

And the same thing happens until slot 4.

Now if we try and pick up and item when our slots are filled, we just don't pick it up anymore! Amazing!

Now we are going to make dropping items.

We have to check the position of selection sprite by seeing if it is overlapping an item (which is in our inventory) and if we press Q, we move our item to layer 1 (our game, away from our hud), set the items position to drop at our player. Set physics enabled now since its in the game layer--- and random 360 because I just like how it looks!

Then add a sub-event to make sure we set our slot empty after dropping it, if selection position is = we make our slot1 0 so we can pick up things in this slot in the future.

Now, were going to put the code for the selector.

For starters, we make our position reset to 0 (disabled) when we move, this is to make sure we don't randomly use items in our inventory while we pick up new items.

if position is 0 we put our cursor x1,y1, basically we just move it out of the way to the very border of the game where it will not come in touch with any items, we also set it invisible so we can't see it.

When position is not 0, we set it visible, so when we use it we can see it in our inventory!

Now, if position is 1 we set our selection position on our inventory image point 1, just like we did earlier with the item picking up code.

Then add buttons for actually moving the selection, -1 position on A, +1 position on D.

And we have to make sure we don't accidentally end up with our selection going out of our range so we can loop our movements for it to appear on the right when we move left on the first inventory slot and other way around.

Lastly, we add the ability to use our items.

If E is pressed while our selector overlaps with item, make something happen.

It is pretty straightforward at this point. Only thing to make sure is that if we use or destroy and item we have to remember to make the slot empty again, for future items.

This can be done by making a sub-event for the using part where we check by the position of the selector which slot to free up!

Thank you for reading my tutorial, if it helped, please tell me. If there is something wrong with the code, please tell me too!

.CAPX

platformer-inventory-no-arrays.capx

Download now 187.28 KB
  • 3 Comments

  • Order by
Want to leave a comment? Login or Register an account!