Hello everybody!
UPDATE [2]:
- added inventory limit
- fixed alphabetic sorting
- little issues and better coding
Upon request, I have developed a simple item system that gives the items random values based on a base value. In addition, it is now possible to equip items to make the player stronger. For reasons I had to do this in Construct 3. Nevertheless, it should be possible for anyone to open the c3p file and view all changes. Since you can not upload any c3p files in the tutorials, here is the link:
c3p-file: 1drv.ms/u/s!Ap_-qxoGKbDcdp-nbqSBulYC1oE
Note: I tried to bookmark all changes. This version has not been thoroughly tested. Bugs can occur.
Original:
I've tried a more complex inventory that can be used well in larger RPGs and can be easily customized. I would like to share my results with you. (Please excuse my language errors. English is not my mother tongue)
On this page I will briefly explain the theory. The code with comments can be found in the .capx.
Items
Before we build an inventory, we need items. How you create them and give them properties is up to you. However, some properties (object variables) are important for the placement in the inventory. I distinguish between World Items and Inventory Items.
Essential Variables for World Items:
• Name - Ingame name for sorting by alphabet
• Type - Item type for classification into a category (e.g. weapons, armor ...)
• Main property - property to be sorted (e.g., damage, defense ...)
• Priority - If you want to separate items within a category (e.g. melee weapons from ranged weapons)
• Letter - current letter index to compare while alphabetical sorting
• Quantity - The number of items in a stack
For Inventory Items there is also:
• Slot ID - Indicates the slot on which the item was placed
I have grouped items of a category in a sprite with different animations that relate their values from a dictionary. After the world items were ready, I cloned them and adjusted the variables for the inventory items. This should not be the content of this tutorial. You can proceed differently in your own game.
Inventory
The inventory consists mainly of the slots. For this, we use a sprite and give it the variable "Slot_ID". Then we copy this slot as often as we want to have slots and arrange them in order. When this is done, we give each slot the corresponding ID, starting with Slot 1 to Slot X. Since I refer to the sorting and placing of items on the slot, all relevant values of the looted item are packed into variables of the slot.
The slots require the following variables:
• Slot_ID
• Name of the item
• Number of items / size of the stack
• Priority of the item
• Main property of the item
• Letter of the item
The variables are always transferred from the corresponding item to the slot.
A small difficulty is that a slot takes all categories. If you have 5 categories, there can be 5 different items on Slot 1. Therefore, it is important to always save the currently active category in a variable (for me a global variable) and to assign all items to a category. So you can only select and display the items that match to the active category.
Looting
Each time you move the cursor over an item, you must make a unique selection, otherwise you will pick up several overlapping items with one click. For this purpose, we use a variable which always stores the UID of the targeted item. If we click on some overlapping items, we only want to pick up that one whose UID corresponds to the variable.
The following steps are performed for sorting into the inventory. For this I use a "For each" loop, which begins with the first slot and then ascending by Slot ID.
1. Is this item already in inventory?
a. Yes -> stack
b. No -> to 2.
2. Has the looted item a higher priority than the item on the current slot?
a. Yes -> Push the item on the slot and all the following backwards and place the new item
b. Same priority -> go to 3.
3. Has the looted item a higher main property than the item on the current slot?
a. Yes -> Push the item on the slot and all the following backwards and place the new item
b. Same properties -> go to 4.
4. Is the name of the item looted before the name of the item on the slot?
a. Yes -> Push the item on the slot and all the following backwards and place the new item
b. No -> move a slot forward and move the items from this slot backwards to place the new item
Do you ask yourself where the query remains for a lower priority and lower properties? This is done automatically when the loop has arrived at the first free slot. The default properties of an empty slot are so low that the weakest item can be placed there. So an empty slot always has damage = 0. If you have cosmetic weapons with 0 damage in the game, you must give the free slot a negative value.
In my .capx example there are so far only weapons and armor, but also a placeholder for materials. Here you can try to add something yourself. You could sort materials by name only. For this purpose, you give all materials a uniform priority and main property, so that the alphabetical sorting is always used.
Dropping
When an item is dropped, the corresponding Inventory Item is deleted and a new World Item is created. There may be difficulties with C2 if you use an extra sprite for each item and group them into families. If you create a family item, a random object from this family is chosen (Create Object Action). Only with C3 there is "Create Object (by name)". Here you can give each item a variable with the name of the sprite object and then call it. Since I use animations, I can create an item and then select the appropriate animation.
When there is a free slot, all Inventory Items after this slot and the active category moves up by reducing their slot ID by 1.
Attention! It is important that all objects that are associated with the inventory have the 'Global' option selected in 'Object type properties', so that the inventory is maintained even during a layout change!
If you are experiencing any bugs or see any difficulties with this method, please let me know! Have fun trying!