GML [SOLVED] Cant narrow down crash in code (GMS2)

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:

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)
Here is the language used in my inventory_obj:
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;
Global Left Pressed:
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;
}
Global Left Released:
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;
  }
}
and (where I believe the error is) Right Mouse Clicked:
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)
    }
}
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!
 
Last edited by a moderator:
T

Tyler Ratliff

Guest
I was able to get the right click fixed with a response from GM Wolf, this was done by changing the right click event to:
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 (slot[slot_clicked] != noone) {       ///This IF statement makes sure the slot is not empty
    if script[slot[slot_clicked]] != noone
    {
        script_execute(script[slot[slot_clicked]], slot_clicked)
    }
}
}
This solved the problem by adding
Code:
if (slot[slot_clicked] != noone)
as an IF statement to check if the slot is empty first. Yay!

Still having trouble with the y_offset portion though. The problem is definitely in this line:
Code:
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)
Its in the last two and statements if I am correct. I am not sure how to integrate the y_offset to determine the correct y position of the mouse if the inventory is moved down.
 
Top