• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Drawing a sprite on top of another sprite

MyGMSUser

Member
I'm having trouble with an inventory system, where when I hover over an item, it should draw a sprite "spr_description" on top of the other sprites, but with my code it keeps drawing it below them. I ask if anyone would be able to help me with this problem.
Create event:
GML:
if (variable_global_exists("AllItems") == false)
{
    global.AllItems = ds_grid_create(0, Item.Height);
}

myItems = global.AllItems;

isshowingmenu = false;
showingDescription = false;
menuWidth = 9;
menuHeight = 9;
itemSeparation = 63;
itemScale = 1;
sprt = undefined;
currentItem = undefined;
Step event:
GML:
if keyboard_check_pressed(ord("Q"))
{
    if active = false
    {
        active = true;
    }
}

if keyboard_check_pressed(vk_escape)
{
    if active = true
    {
        active = false;
    }
}

if (sprt != undefined)
{
    showingDescription = true;
}
else
{
    showingDescription = false;
}
Draw event:
GML:
if active
{
    draw_set_color(c_black);
    draw_set_alpha(0.75);
    draw_rectangle(0,0,room_width,room_height,false);

    draw_set_alpha(1);

    draw_sprite(spr_inv,0,CameraMiddleX(),CameraMiddleY());

        for (var i = 0; i < ds_grid_width(myItems); ++i)
        {
        var itemX = CameraX() + 230 + (i*itemSeparation);
        var itemY = CameraY() + 163;
        var sprite = myItems[# i,Item.Sprite];
    
        if (i >= menuWidth and i < menuWidth*2){itemX = CameraX()+230+(i-menuWidth)*itemSeparation;}
        if (i >= menuWidth*2 and i < menuWidth*3){itemX = CameraX()+230+(i-menuWidth*2)*itemSeparation;}
        if (i >= menuWidth*3 and i < menuWidth*4){itemX = CameraX()+230+(i-menuWidth*3)*itemSeparation;}
        if (i >= menuWidth*4 and i < menuWidth*5){itemX = CameraX()+230+(i-menuWidth*4)*itemSeparation;}
        if (i >= menuWidth*5 and i < menuWidth*6){itemX = CameraX()+230+(i-menuWidth*5)*itemSeparation;}
        if (i >= menuWidth*6 and i < menuWidth*7){itemX = CameraX()+230+(i-menuWidth*6)*itemSeparation;}
        if (i >= menuWidth*7 and i < menuWidth*8){itemX = CameraX()+230+(i-menuWidth*7)*itemSeparation;}
        if (i >= menuWidth*8 and i < menuWidth*9){itemX = CameraX()+230+(i-menuWidth*8)*itemSeparation;}
    
        if (i >= menuHeight and i < menuHeight*2){itemY = CameraY() + 228;}
        if (i >= menuHeight*2 and i < menuHeight*3){itemY = CameraY() + 291;}
        if (i >= menuHeight*3 and i < menuHeight*4){itemY = CameraY() + 354;}
        if (i >= menuHeight*4 and i < menuHeight*5){itemY = CameraY() + 417;}
        if (i >= menuHeight*5 and i < menuHeight*6){itemY = CameraY() + 480;}
        if (i >= menuHeight*6 and i < menuHeight*7){itemY = CameraY() + 543;}
        if (i >= menuHeight*7 and i < menuHeight*8){itemY = CameraY() + 606;}
        if (i >= menuHeight*8 and i < menuHeight*9){itemY = CameraY() + 669;}
    
        draw_sprite_ext(sprite,0,itemX,itemY,itemScale,itemScale,0,c_white,1);
    
        draw_set_color(c_white);
        draw_set_alpha(1);
        draw_set_font(fnt_smaller);
        draw_text(itemX + 19, itemY + 17, myItems[# i, Item.Amount]);
    
            if point_in_rectangle(mouse_x,mouse_y,itemX-29,itemY-29,itemX+29,itemY+29)
            {
                draw_set_alpha(0.25);
                draw_set_color(c_blue);
                draw_rectangle(itemX-29,itemY-29,itemX+29,itemY+29,false);
                draw_set_alpha(1);
                draw_set_color(c_black);
                if (instance_exists(myItems[# i, Item.Object]) == false)
                {
                    currentItem = instance_create_layer(-32,-32,"InvObjects",myItems[# i,Item.Object]);
                    currentItem.type = myItems[# i,Item.Type];
                    currentItem.name = myItems[# i,Item.Name];
                    currentItem.isinmenu = true;
            
                    if (showingDescription)
                    {
                    currentItem.isshowinginfo = true;
                    }               
                }
            
            sprt = draw_sprite_ext(spr_description,0,mouse_x+20,mouse_y+20,1.4,1,0,c_white,1);
    
            }
            else
            {
                sprt = undefined;
            }

        }

        if (instance_number(obj_itemparent) > 1)
        {
            instance_destroy(obj_itemparent);
        }
        
        if !point_in_rectangle(mouse_x,mouse_y,itemX-29,itemY-29,itemX+29,itemY+29)
        {
            instance_destroy(obj_itemparent);
        }
}
Create event:

GML:
enum Item
{
    Name,
    Sprite,
    Amount,
    Type,
    Object,
    Txtcolor,
    Height
}

enum Type
{
    Weapon,
    Armor,
    Consumable,
    Equipment
}

enum Ailment
{
    Poison,
    Burning,
    Slow
}

enum SortType
{
    Name,
    Amount,
    Type,
    Heigth
}

if (variable_global_exists("AllItems") == false)
{
    global.AllItems = ds_grid_create(0, Item.Height);
}
AddItemToMasterList(["Sword",inv_sword,1,Type.Weapon,obj_sword]);
AddItemToMasterList(["Bow",inv_bow,1,Type.Weapon,obj_bow]);
AddItemToMasterList(["Magic Staff",inv_magic_staff,1,Type.Weapon,obj_magic_staff]);
AddItemToMasterList(["Magic Staff",inv_magic_staff,1,Type.Weapon,obj_magic_staff]);
Script "scr_inventory":
GML:
function AddItemToMasterList(attributes){

    if (variable_global_exists("AllItems") == false)
    {
        global.AllItems = ds_grid_create(0, Item.Height);
    }

    if (ds_exists(global.AllItems,ds_type_grid) == false)
    {
        global.AllItems = ds_grid_create(0,Item.Height);
    }

    ds_grid_resize(global.AllItems,ds_grid_width(global.AllItems)+1,ds_grid_height((global.AllItems)));
    for (var i = 0; i < array_length(attributes); ++i)
    {
        global.AllItems[# ds_grid_width(global.AllItems)-1,i] = attributes[i];
    }
}

All the item sprites named "sprite" and the description "spr_description" are drawn in the correct positions, but in the wrong order. If anyone could come up with how to draw "spr_description" on top of all the "sprite", I would be very thankful.
 
Last edited:

TheouAegis

Member
You need to save the value of i inside a variable, then draw your description Sprite outside of your for Loop. The problem is you are it's still inside the for Loop when you draw all the other Sprites, so you're just drawing on top of the description.
 
Top