Inventory Example - 3 Sprites and a Text Object - No Arrays

2

Index

Attached Files

The following files have been attached to this tutorial:

.capx

inventory-example.capx

Download now 186.02 KB

Stats

4,784 visits, 8,363 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.

Published on 19 Dec, 2015. Last updated 19 Feb, 2019

Woo Hoo, now we get to code the events. We will need the 4 global variables as shown on the event sheet pic. Go ahead and create them. We are also going to need a mouse object so go ahead and add this to your project.

So when we click on our item found object what are we wanting to do? First we want to check and see if it exist in our current inventory. If it does we will stack it on that item. If it doesn't currently exist we will place it in our inventory in the first available spot. If no spots are available then we do nothing. If programing this for a game we might want to check to see if the item is stackable or not. if its not we would bypass looking to see if it exist currently. You could simply add an instance variable to the item_found sprite. stackable (yes/no) and then check it as you picked up item. Hopefully I can get on another tutorial and show how to use variables like health_add, mana_add, useage , etc. there are tons of stuff.

ok back to the code. You'll notice on the event sheet we set up a loop for "x" from 1 to Inventory slots 1. Inventory slots is our Global variable representing the 10 slots we have. the plus 1 lets us loop one past our last slot so we can trigger inv_loop > inventory_slots and reset our variables. Also it was a good place to destroy the item_found if we wanted too.

To look and see if the item exist we set up the following conditions:

1. inv_item = where loop is

2. Check id to see if item in inventory slot is same as one being picked up

3. Make sure item has not been placed or stacked yet on this pickup

If our conditions are met we set the item count to the existing count plus the item being picked up's count. We then set the item stacked variable to one so we wont meet our conditions again for this pickup.

Ok so now lets assume we did not find our item in the current inventory.

We check following conditions

1. inv_item = where loop is

2. inv_count = 0 meaning the slot is empty

3. Make sure item has not been placed or stacked yet on this pickup

if our conditions are met we execute the following actions.

1. Set the animation frame of inv_item to match the item we are picking up

2. set the inv_item's count to match the item we are picking up

3. set the inv_item's item_id to match the item we are picking up

4. Set item_placed to one so we wont meet our conditions anymore during this pickup.

At the end of our loop we reset our variables to get ready for the next pickup.

In the image above the right click event is where we would normally have such things as

drop item, consume, equip, etc. Depending on the information we get from the item we picked up. For this example we just reset the inv_item and eliminated the item from our inventory.

The every tick event at the bottom of the event sheet is where we display the amounts or item_count for each inv_item in our inventory.

I hope some of you got something out of this tutorial. Please let me know if anyone would like me to go into more detail. Also if anyone has suggestions please comment. We are all seekers of the truth and better ways of coding.

Also thanks so much to All the C2 staff for such a great tool.

a

.CAPX

inventory-example.capx

Download now 186.02 KB
  • 2 Comments

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