• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

GML Flexible Inventory System

Hans66

Member
So the code looks all good. I suspect the reason you're having issue is something causing the mouse pressed event to not actually trigger. I have a few ideas but I can't say for sure so you'll need to investigate this. Here are some suggestions:
- The slot objects are not in the correct place, if you have a moving view. (Unlikely since your drawing works).
- The slot objects need a mask assigned to detect if they are being clicked on. Make sure of that, and make the mask the same size as the item sprite itself.
- If you'd rather not assign a mask that way, you can also check for the mouse press by using a global mouse left pressed event instead, and checking if the mouse_x and mouse_y is overlapping with the desired area of the slot.
- Also try adding a show_debug_message to the left press event to make sure it isn't running, to verify my theory and ensure it isn't another issue.
Thank you so much everything was correct code wise i needed to add the sprite mask to the slot objects tried today have been swarmed with engineering school lately. Thank you again! now onto creating the menu.
 

tCubix

Member
Hey I am new to Gamemaker and I put the inventory in an extra room but I also want to have a hotbar if the inventory is closed. I dont know how I should do this
 
Hey I am new to Gamemaker and I put the inventory in an extra room but I also want to have a hotbar if the inventory is closed. I dont know how I should do this
You should have the inventory as a global ds_grid, then you can reference it from anywhere in the game.
Just create some slots for the hotbar where you need them, in addition to the slots in your inventory room.
 

doomtucan

Member
okay so my question is how can i take an implement this into the style of game i am making? im making an Action Role Playing Dungeon Crawler that has massive amounts of items and values!
 
okay so my question is how can i take an implement this into the style of game i am making? im making an Action Role Playing Dungeon Crawler that has massive amounts of items and values!
This tutorial should work fine for that type of game. Nothing special you need to do to get it to work in any particular genre. The quantity of items just extends your enumerator, data structures, etcetera.
 
How would you go about making slots that only allow an Item into them based on their typing?
In the code where you would put items into a slot, do an additional check based on the type. There's a lot of ways you can do this, and it depends on what specific limits you want to have.
- You may have different item_gain functions that you use depending on the type, that start the for loop for finding slots at different locations.
- You could also possibly assign types, or a list of types to individual slots, either on the object or in a new data structure and check for that.

I hope that helps. It's hard to give a specific answer.
 

McFishy

Member
Thanks for the tutorial! I found a way to better use this with cameras/view-ports. The device_mouse_x_to_gui(device); function can be used to get the mouse x/y relative the the gui layer and you could check for the position of the mouse when you click to see if its over a inventory slot.
 

joeblo24

Member
I'm not sure if it was posted yet but I was able to finally find a way to get a stackable'ish inventory.

/// @description Creates the inventory slots.

var slot = 0;
var slot_counter = 0
var new_row_x = 0;
var new_row_y = 0;


while (slot < ds_grid_width(global.inventory))
{

slot_counter +=1
if slot_counter>5 then
{
new_row_x += 320;
new_row_y += 64;
slot_counter = 1;
}
var inst = instance_create_depth(x+8+(64*slot)-(new_row_x), y+8+(new_row_y),0, obj_slot);

inst.var_slot = slot;
slot ++;

}

I don't think it's perfect but it does what I was looking for and wanted to share.
Thanks Cloaked Games for the engine!
 
Yes. This is explained in the tutorial. This is done using enums and takes the form of item.apple or item.sword etcetera.
yeah, but I'm talking about the same item. I would like to have the same item.sword in my inventory, but have different stats. If I do apply an edit to item.sword, it'll apply to all item.swords since the iid is just item.sword.
I'm trying to make a item scrolling system in the game i'm working on which is why I needed unique id's
 
Last edited:
@killerwolf Ah, I understand your question now.

You are correct, this inventory tutorial is not designed for that type of item system. You will need to make some major modifications yourself to do that. Here are a few ideas, though I cannot give you a full explanation:
- Each item can be it's own data structure instead. This would be a grid or a list, and each position would correspond to a different piece of the item data.
- OR you could use the same system, and to save memory have a small data structure which includes the item ID referencing the item index, and a list of item data modifications that apply to a specific item. (This is how Minecraft works, using NBT data).
- You will need to update the item moving code to check for items that both have matching IDs and matching modification data.
- This will be much easier to do in GML 2.3, as you can use structs to store modification data. (This solution is what I will be doing in my current project).
 
It really depends on your exact implementation. ds_list or ds_grid can both be used, or even a combination of them. Try drawing a picture of the data you want to store, to see what might work the best.
 
1591918187476.png
@Cloaked Games So i reworked part of the code, but i can't understand why global.item_list is being modified when i'm changing the map values in global.inventory2. When i change "str" to 4, the items in item_list also change from 0 to 4.
 
Last edited:
@killerwolf Sorry for the long delay in response. I hope you've already figured it out...

At any rate, I don't think I can really answer your question. Try to keep track of where all your data is stored. If you are using the same reference to the grid, it's going to modify the same grid.
Remember, the way this inventory system works is with a single item index. If you change a value in the inventory it'll be great, but if you change a value in the item index, it will change for every item. There are some tips for other approaches that let you change values with more granularity if you look through the thread a bit. But I can't give specific answers for doing that.
 
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object obj_slot:

Variable obj_slot.var_slot(100004, -2147483648) not set before reading it.
at gml_Object_obj_slot_Draw_64 (line 4) - var iid = global.inventory[# var_slot, 0];
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_slot_Draw_64 (line 4)


can you help?
 
@samiakincilar This error message just means that you did not define the var_slot value correctly when you created the slots.
Take another look at the code under Part 2: The Inventory > Displaying the Inventory.
A lot of the code in this tutorial won't work until it's completely finished, so if you're only partially finished, keep going to the end.
 
@samiakincilar This error message just means that you did not define the var_slot value correctly when you created the slots.
Take another look at the code under Part 2: The Inventory > Displaying the Inventory.
A lot of the code in this tutorial won't work until it's completely finished, so if you're only partially finished, keep going to the end.
1597921878400.png hey thank you for your reply but i didnt understand what did we do in here and cant solve the problem.
 

Jorg Gjer

Member
Hi.
I started coding my last game using this inventory tutorial.
With some adjustments, it's very flexible. The player and all objects, like f.eks a crafting table, that has a inventory use this.

To see it in use, try my game or see some pictures of it in this post.

Thanks to Cloaked Games for this tutorial.

Greeting Kaizen
 
Top