Jumat, 13 November 2015

Action adventure tutorial part 9: more on the inventory system

Hi guys, it's been quite some time after my last blog post (which was in September), I apologize for not writing anything in October. The truth is, as long as this Patreon isn't enough to pay my bills I'll have to do outside contract job as well. I don't really ask for a lot, about $400-500 a month from Patreon would be great, but I'm still far from that goal so... yeah, I need to get outside job.

So, last time we're making an inventory system and we get to a good start, but there are at least two things missing: the functionality to choose an item, and the functionality to use that item. Beside those two important parts, we still can't return to the main game from the inventory screen. Let's fix the easy part first, return to the main game. We simply need to add the action Go to Layout 1 (or whatever you name the first layout) to the event of keyboard's key press, the result would be like this

 photo image 1_4.png
Back to layout 1

And with that out of the way, let's add the functionality to choose an item. We first need a sprite that acts as a "selector", so we know which slot is currently being selected. So let's create it, add a new sprite object and name it "selector" and change the size to 64 by 64, because we want the selector to be as big as the item slot. And then you can use the Construct 2's image editor or your any other image editor you prefer, but make the selector to look something like this

 photo image 2_4.png
Selector

The gray part is from Construct 2's editor, the selector is actually mostly transparent. Next, add an instance variable to the selector called itemIndex with the type number and we'll keep the default value to 0. The way it works is like this: we're going to check the selector's itemIndex at all times, and we're going  to put the selector's position to the position of the item slot with the same index, this way if we want to move the selector, we'll only need to add or subtract the itemIndex instance variable.

The code for the first part of our logic is like this

 photo image 3_5.png
Putting the selector

for the second part we obviously need a keyboard event, and we'll add or subtract the itemIndex instance variable by one each time the right arrow key or the left arrow key is pressed. And we also need to put a limit on the code, so when the value is above 11 or below 0 the selector will still correctly selecting an item in the inventory. We could easily use the set value action to set the instance variable to 11 or to 0, but we're going to do something more, we're going to add or subtract the variable by 12 so that when the selector goes up or down, it will go back to the other side of the selection.

 photo image 4_6.png
Moving the selector