GML Flexible Inventory System

H

Hans66

Guest
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.
 
T

tCubix

Guest
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.
 
M

McFishy

Guest
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.
 
S

samiakincilar

Guest
___________________________________________
############################################################################################
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.
 
S

samiakincilar

Guest
@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
 
M

Mikey3131

Guest
Hi @Cloaked Games !

I just ran across this tutorial, and WOW, I'm in love. You do a really job of breaking this down really simply to make it easy to understand and implement our own solutions based off of this. Thank you!

With all of the different items being defined as enums during the game start, can you think of a way to add enums that aren't hard-coded?

I'm thinking of something like a text file that on game launch, you'd read and parse into enums. (This could get you in trouble if folks removed items that they already had stored of course.)

It'd open up some interesting possibilities for modding.

EDIT:
I kept digging around, and it looks like using a JSON parser might be the way to go:

Then we can store the input as an ds_map, and "kinda" emulate the enum method you've lined out for the ds_grids. I'm thinking this would take more memory, but allow for some extra flexibility. Think I'm barking up the right tree?
 
Last edited by a moderator:
@blooflazh I did this by storing each item as a map inside an a grid ( the inventory ). This is basically the foundation for how I implemented it. The maps contain information such as "itemid" and "type" as well as "Str", "Dex", "Int" etc.. and I'm able to edit the map values without changing all the items since they're not stored as enums.
 

bazza0853

Member
Hi, I had completed my inventory using friendly cosmo's tutorials on youtube, but then I saw this.
If you had seen her inventory system by any chance, what would be the difference between your's and her tutorial?
 
Hi, I had completed my inventory using friendly cosmo's tutorials on youtube, but then I saw this.
If you had seen her inventory system by any chance, what would be the difference between your's and her tutorial?
Sorry, I've no idea! Friendly Cosmonaut makes great tutorials so I'm sure you're great with what you've got regardless. If you want to learn a bit extra feel free to read this one too just for the sake of understanding GML better.
 

bazza0853

Member
Sorry, I've no idea! Friendly Cosmonaut makes great tutorials so I'm sure you're great with what you've got regardless. If you want to learn a bit extra feel free to read this one too just for the sake of understanding GML better.
Thanks. I think this will also be a great help on my understanding. I'll definitely have a go at this tutorial as well!
 
hey guys i'm trying to create a game, very good asset, however... i'm trying to make the slots follow the Obj_player but nothing works i've been trying to fix this for almost a whole day when i follow the player it all comes together and everything gets buggy can you help me ?

PLEASE HELP ME!
 
hey guys i'm trying to create a game, very good asset, however... i'm trying to make the slots follow the Obj_player but nothing works i've been trying to fix this for almost a whole day when i follow the player it all comes together and everything gets buggy can you help me ?

PLEASE HELP ME!
Hey! I'm sorry you're running into trouble.

Without more details, it's hard to help much. Were you able to get the inventory to work without following the player's position? If so, the next step is to ensure the position of the slots is set every frame to be relative to the camera's position. What sort of trouble are you running into?
 

Ninkfyzk

Member
I apologize if this question is silly, but why does this keep happening?



___________________________________________
############################################################################################
ERROR in
action number 1
of Draw Event
for object oSlot:

Variable oSlot.var_slot(100056, -2147483648) not set before reading it.
at gml_Object_oSlot_Draw_64 (line 3) - var iid = global.inventory[# var_slot, 0];
############################################################################################
gml_Object_oSlot_Draw_64 (line 3)

but the code seems to be exactly the same as the one in the guide, I even copy-pasted it just to see if i mistyped something, but it kept happening
1668138411061.png
that's the code in the game controller object
the code in the slot object is as follows
1668138523475.png
(the sprite is just a placeholder)
what have I done wrong? was I supposed to put a variable I missed somewhere else? where? I've also placed the 10 instances of the slot object, either way, cool guide.
 
I've also placed the 10 instances of the slot object
That's your error. :)

The bit where you assign the slots in the create event also creates the slot objects at the same time.

This means you have twice as many, and the ones you placed yourself don't have slots assigned either, causing the crash.
 

Ninkfyzk

Member
That's your error. :)

The bit where you assign the slots in the create event also creates the slot objects at the same time.

This means you have twice as many, and the ones you placed yourself don't have slots assigned either, causing the crash.
oh, damn, well that was silly of me, thank you for answering, it works now!
 
Top