T
Tyler Ratliff
Guest
Hello! I have been working to integrate an inventory system into my adventure game and the only one that I was able to replicate with the desired effect is GM Wolfs tutorial on YouTube. Basically, if the left mouse button is pressed on a grid over the inventory it will lift the items up and stack them on a similar item or drop them into the nearest empty slot. When you right click on a usable object (the health potion in this situation) it consumes one. I've narrowed down that my problem is anytime a right click is made in the inventory where there is no item that the crash happens. I am also using GMS2. The crash I receive is:
Here is the language used in my inventory_obj:
Create:
Global Left Pressed:
Global Left Released:
and (where I believe the error is) Right Mouse Clicked:
Thank you again for the help! Bonus points if anyone can tell me how to alter the formulas to include the y-offset as well. Thanks again for the time to read this!
Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Mouse Event for Glob Right Pressed
for object obj_inventory:
Push :: Execution Error - Variable Index [0,-4] out of range [1,4] - -1.script(100036,-4)
at gml_Object_obj_inventory_Mouse_54 (line 16) - if script[slot[slot_clicked]] != noone
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_inventory_Mouse_54 (line 16)
Create:
Code:
///Define Items and Items Sprites
enum items ///Assigns IDs to items with enumerators
{
health_potion, ///ID 0
bones, ///ID 1
feathers, ///ID 2
raw_chicken ///iD 3
}
sprite[items.health_potion] = spr_health_potion; ///Creates and stores the items sprite
sprite[items.bones] = spr_bones;
sprite[items.feathers] = spr_feathers;
sprite[items.raw_chicken] = spr_raw_chicken;
script[items.health_potion] = scr_use_health_potion;
script[items.bones] = noone;
script[items.feathers] = noone;
script[items.raw_chicken] = noone;
///Initialize the Inventory
slot_number = 28;
inventory_width = 4;
for(var i = 0; i < slot_number; i++)
{
slot[i]=noone;
slot_n[i] = 0;
}
slot[10]=items.health_potion;
slot_n[10]=5;
slot[1]=items.bones;
slot_n[1]=1;
slot[15]=items.bones;
slot_n[15]=1
slot[8]=items.feathers;
slot_n[8]=34
slot[4]=items.raw_chicken;
slot_n[4]=1
mouse_slot = noone;
mouse_slot_n = 0;
x_off = 1245;
y_off = 392;
Code:
var gui_mouse_x = device_mouse_x_to_gui(0);
var gui_mouse_y = device_mouse_y_to_gui(0);
if (mouse_slot == noone) and (gui_mouse_x > x_off) and (gui_mouse_x < x_off + inventory_width * 64) and (gui_mouse_y > 0) and (gui_mouse_y < (slot_number / inventory_width) * 64)
{
var xx = (gui_mouse_x - x_off) div 64; //Number after div is width of the slot
var yy = (gui_mouse_y div 64);
var slot_clicked = xx + (yy * 4); ///4 may have to be changed to number of slot columns
mouse_slot = slot[slot_clicked];
mouse_slot_n = slot_n[slot_clicked];
slot[slot_clicked] = noone;
slot_n[slot_clicked] = 0;
}
Code:
var gui_mouse_x = device_mouse_x_to_gui(0);
var gui_mouse_y = device_mouse_y_to_gui(0);
var xx = (gui_mouse_x - x_off) div 64; //Number after div is width of the slot
var yy = (gui_mouse_y div 64);
var slot_clicked = xx + (yy * 4); ///4 may have to be changed to number of slot columns
if ((gui_mouse_x > x_off) and (gui_mouse_x < x_off + inventory_width * 64) and (gui_mouse_y > 0) and (gui_mouse_y < (slot_number / inventory_width) * 64))
{
if scr_add_item(slot_clicked, mouse_slot, mouse_slot_n)
{
mouse_slot = noone;
mouse_slot_n = 0;
}
}
Code:
var gui_mouse_x = device_mouse_x_to_gui(0);
var gui_mouse_y = device_mouse_y_to_gui(0);
if (mouse_slot == noone) and (gui_mouse_x > x_off) and (gui_mouse_x < x_off + inventory_width * 64) and (gui_mouse_y > 0) and (gui_mouse_y < (slot_number / inventory_width) * 64)
{
var xx = (gui_mouse_x - x_off) div 64; //Number after div is width of the slot
var yy = (gui_mouse_y div 64);
var slot_clicked = xx + (yy * 4); ///4 may have to be changed to number of slot columns
if script[slot[slot_clicked]] != noone
{
script_execute(script[slot[slot_clicked]], slot_clicked)
}
}
Last edited by a moderator: