inventory issue/dragging icons [not solved]

G

gibberingmouther

Guest
i'm trying to make it so i can drag object icons from the inventory to a hotkey bar.

i'm not concerned about the hotkey bar here. i'm just trying to get the dragging to work.

relevant draw event code for the inventory
Code:
//draw list content
//mouseWithin is supposed to be var before but i changed it
for(var A=0;A<itemsShow;A++){
     //this is going to show the inventory list
    var itemH=listH/itemsShow;
  
    mouseWithin=point_in_rectangle(mouse_x,mouse_y,listX,listY+A*itemH,buttonX[0]-1,listY+(A+1)*itemH-2);

    draw_set_valign(fa_center);
    //draw list item red if it's hovered
    draw_set_colour(cl[mouseWithin]);    
    draw_text(listX+itemsOffset, listY + A*(listH/itemsShow)+itemH/2, global.item[global.inv[A+itemCurrent, 0], global.DESC] + " x " + string(global.inv[A+itemCurrent, 1]));
}
step code for dragging
Code:
if mouseWithin {
    if mouse_check_button_pressed(mb_left){
        dragging = true;
        instance_create(mouse_x, mouse_y, obj_genicon);
        xoffset = obj_genicon.x - mouse_x;
        yoffset = obj_genicon.y - mouse_y;
    }
}

//listX,listY+A*itemH,buttonX[0]-1,listY+(A+1)*itemH-2

if dragging == true{
    obj_genicon.x = mouse_x+xoffset;
    obj_genicon.y = mouse_y+yoffset;
}
if mouse_check_button_released(mb_left){
    if dragging == true{
        dragging = false;
        with (obj_genicon)
        {
        instance_destroy();
        }
    }
}
basically when i click on an inventory slot it's supposed to spawn an instance of obj_genicon but instead it does nothing, no error message, just nothing.
 

rIKmAN

Member
Put some 'show_message()' or 'show_debug_message()' calls in each block to see what is and isn't getting called, it will help you narrow it down.
ie
Code:
if mouseWithin {
   show_message("mouseWithin true")
    if mouse_check_button_pressed(mb_left){
    show_message("mouseWithin > mb_left pressed")
        dragging = true;
        instance_create(mouse_x, mouse_y, obj_genicon);
        xoffset = obj_genicon.x - mouse_x;
        yoffset = obj_genicon.y - mouse_y;
    }
}
Also try 'draw_text'ing at the mouse x/y you are using, I suspect you have an error in your coords, which is why nothing happens.

If it's a GUI, are you using the GUI Draw event?
 
G

gibberingmouther

Guest
okay, mouseWithin is never true because the debug message never shows... anybody else got any ideas?

i want to point out that mouseWithin works fine in the draw event. it's just the code in the step event that doesn't work.
 

rIKmAN

Member
Code:
//mouseWithin is supposed to be var before but i changed it
What does this comment mean?

Is mouseWithin a var in either the Draw Event or the Step Event?

If you 'draw_rectangle' using the points you use in the 'point_in_rectangle' line does it draw a rectangle where you expect it be?
 
G

gibberingmouther

Guest
Code:
//mouseWithin is supposed to be var before but i changed it
What does this comment mean?

Is mouseWithin a var in either the Draw Event or the Step Event?

If you 'draw_rectangle' using the points you use in the 'point_in_rectangle' line does it draw a rectangle where you expect it be?
mouseWithin is not a var. in the scrolling list code i used it originally was but i changed it. the point_in_rectangle code works as it is supposed to. the text lights up red when i put my mouse cursor over it. there's more code but i don't think it's relevant. i initialized mouseWithin to -1 in the create event but i don't think that matters .... thank you for trying to help me!

EDIT: okay i figured out PART of the problem. i had "var mouseWithin" declared multiple times in the draw code. so instead i created a global variable with point_in_rectangle(blah blah blah) and now at least the show message code (thank you rIKmAN!) is executing but genicon isn't spawning so not working as planned. should point out that only the first show message code is executing so it isn't registering the mouse press. could be a problem with the use of a step event and a draw event but i don't know coding well enough to say.

also there's a weird delay between when i mouse over and when the show message shows. don't really get it.

EDIT2: i could put the code in the step event into the draw event instead so things might be more in sync but the manual says it's a bad idea to put non-draw code into a draw event.
 
Last edited by a moderator:

rIKmAN

Member
So what's the actual problem now?

Explain what's wrong and post the exact code copied from the script files so we can see exactly what is happening.
 
G

gibberingmouther

Guest
relevant draw event code block
Code:
for(var A=0;A<itemsShow;A++){
     //this is going to show the inventory list
    var itemH=listH/itemsShow;
   
    var mouseWithin=point_in_rectangle(mouse_x,mouse_y,listX,listY+A*itemH,buttonX[0]-1,listY+(A+1)*itemH-2);

    draw_set_valign(fa_center);
    //draw list item red if it's hovered
    draw_set_colour(cl[mouseWithin]);     
    draw_text(listX+itemsOffset, listY + A*(listH/itemsShow)+itemH/2, global.item[global.inv[A+itemCurrent, 0], global.DESC] + " x " + string(global.inv[A+itemCurrent, 1]));
    //draw_text(listX+itemsOffset,listY+A*(listH/itemsShow)+itemH/2,inventoryList[A+itemCurrent]);
    //global.dragger = point_in_rectangle(mouse_x,mouse_y,listX,listY+A*itemH,buttonX[0]-1,listY+(A+1)*itemH-2);
    global.dragX1 = listX;
    global.dragX2 = buttonX[0]-1;
    global.dragY1 = listY+A*itemH;
    global.dragY2 = listY+(A+1)*itemH-2;
    //if clicked, show object invrightclick
    if mouse_check_button_pressed(mb_right) && mouseWithin && global.item[global.inv[A+itemCurrent, 0], global.DESC] != "Nothing" && !instance_exists(obj_invrightclick){
        //global.item_clicked = global.item[A+itemCurrent, 0];
        //global.item_name = global.item[A+itemCurrent, 4];
        global.item_clicked = A+itemCurrent; //do this instead
        //show_message("You clicked "+global.item[global.inv[A+itemCurrent, 0], global.DESC]);
        instance_create(listX+itemsOffset+string_width(global.item[global.inv[A+itemCurrent, 0], global.DESC]+ " x " + string(global.inv[A+itemCurrent, 1])), listY + A*(listH/itemsShow)+itemH/2, obj_invrightclick);
    }
    if mouse_check_button_pressed(mb_left) && mouseWithin && global.store_open && global.item[A+itemCurrent, 0] != 0 {
        msg = show_question_async("Do you want to sell the " + global.item[global.store_1[A+itemCurrent, 0], global.DESC] + "?");
    }
   
}
my entire step event
Code:
//if global.dragger {
if mouse_x >= global.dragX1 && mouse_x <=  global.dragX2 && mouse_y >= global.dragY1 && mouse_y <= global.dragY2{
        //temporary show messages
    show_message("mouse within true");
    if mouse_check_button_pressed(mb_left){
        show_message("mouse within > mb_left pressed");
        dragging = true;
        instance_create(mouse_x, mouse_y, obj_genicon);
        xoffset = obj_genicon.x - mouse_x;
        yoffset = obj_genicon.y - mouse_y;
    }
}

//listX,listY+A*itemH,buttonX[0]-1,listY+(A+1)*itemH-2

if dragging == true{
    obj_genicon.x = mouse_x+xoffset;
    obj_genicon.y = mouse_y+yoffset;
}
if mouse_check_button_released(mb_left){
    if dragging == true{
        dragging = false;
        with (obj_genicon)
        {
        instance_destroy();
        }
    }
}
as i said it's apparently entering the desired field because i get a showmessage "mouse within true", but it shows these messages sporadically and not exactly every time i put my mouse in the field.

it does not show the second showmessage, no matter what, and it doesn't spawn the item obj_genicon. i'm unsure about the way i used obj_genicon.x/y but that is not the cause of the current problem. just thought i'd mention it because it could cause another problem after this one is resolved.
 
G

gibberingmouther

Guest
okay, it's been >48 hours so bumping this. don't have much to add. i've tinkered a little but no luck
 

rIKmAN

Member
First thing I would do is go through that code and space it out so it isn't a wall of text.

If the 'show_message("mouse within true")' only shows sporadically then you need to investigate why.

It should show whenever the mouse is within a certain area, so draw_text the values of those variables (mouse_x, mouse_y, dragX1, dragX2, , dragY1, dragY2) to see what they are and why they are only evaluating to true sporadically.

That's a great offer from @SyntheticStorm9, but being blunt - you use his system, plug it in and as he says it works perfect - what are you going to have learned?

edit:
What I'm trying to say is don't get too frustrated, bugs are part of programming and we've all been there and will be there again.
But by blindly using a piece of code that "just works" you don't learn anything, you don't understand how it works and so will 100% find yourself in this situation again in future.
 
Last edited:
G

gibberingmouther

Guest
think i may have found part of the problem. i added the following to the draw code:
Code:
draw_text(100, 150, string(global.dragX1));
draw_text(100, 175, string(global.dragX2));
draw_text(100, 200, string(global.dragY1));
draw_text(100, 225, string(global.dragY2));
the first two draw normally but are static, which i believe is correct. the next draw a bunch of numbers overlapping each other instead of drawing what is selected. i don't know what to make of it but this is probably part of the problem. i'm still not sure why it doesn't register the second show_message. any ideas? i'm desperate lol.
 
G

gibberingmouther

Guest
*bump.

the status is the same as in my last post. any ideas would be helpful!
 

rIKmAN

Member
You said the second 2 values are drawing erratic values - so I would say that is probably the problem.

Have you broken it down or tried to simplify/debug it at all since your last post?
 
G

gibberingmouther

Guest
I got it to work by putting the step event code into the draw event. What I want to know is, how much does this slow down performance? You're not supposed to put non-draw event code into the draw event. I could really use some feedback on this. Should I just keep it like this or still try to get the step event code to work? I'm thinking maybe this is not possible using the step event but I'm not sure.
 
Top