• 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!

GameMaker Inventory Items are not displaying in Draw GUI!

W

wesk1288

Guest
Hey guys,

I have been following this tutorial to set up my inventory system. However, instead of drawing through the draw event as he does, I am now trying to move all of that to the draw GUI event. Problem is, I can't get my items to display in my inventory when I pick them up. Everything else about my inventory draws fine on screen except my items and their description text. Any ideas?

Obj_Inventory / Create Event:
Code:
globalvar playerInventory, playerInventoryWidth;

playerInventoryWidth = 5;
playerInventory = ds_grid_create(playerInventoryWidth, 1);

showInventory = false;

textBorder = 12;
myItems = playerInventory;
myColor = c_black;
isEmpty = false;
emptyMessage = "You don't have any items";

globalvar itemSelected, scrolledAmount, inventoryEndAt;
itemSelected = 0;
scrolledAmount = 0;
inventoryEndAt = min(ds_grid_height(myItems), floor((sprite_height - (textBorder * 3)) / 24));
inventoryOnScreen = 0;
if(ds_grid_get(myItems, 0 , 0) == 0){
    inventoryEndAt = 0;
    isEmpty = true;
}

alarm[0] = 1;
Obj_Inventory / Key Press - I:
Code:
if(showInventory == false){
    audio_play_sound(snd_UI_click, 10, false);
    showInventory = true;
} else {
    audio_play_sound(snd_UI_click, 10, false);
    showInventory = false;
}
Obj_Inventory / Draw GUI:
Code:
if(showInventory == true){
draw_self();
draw_set_font(fnt_header);
draw_set_colour(myColor);
draw_text(bbox_left + textBorder, bbox_top + textBorder - 10, "Image");
draw_text(bbox_left + 65, bbox_top + textBorder - 10, "Name");
draw_text(bbox_left + 245, bbox_top + textBorder - 10, "Amount");
draw_set_font(-1);

itemLeftStart = bbox_left + 75;
itemTopStart = bbox_top + 24;

for(i = 0; i < inventoryEndAt; ++i) {
    for(j = 0; j < playerInventoryWidth; ++j){
        inventoryOnScreen = i;
        if(j == 0)
            draw_text(itemLeftStart - 8, itemTopStart + (i * 32) + 3, ds_grid_get(myItems, 0, i + scrolledAmount));
        if(j == 1)
            draw_text(itemLeftStart + 175, itemTopStart + (i * 32) + 3, ds_grid_get(myItems, 1, i + scrolledAmount));
        if(j == 3)
            draw_sprite(ds_grid_get(myItems, j, i + scrolledAmount), 0, bbox_left + 25, itemTopStart + (i * 32) + 10);
    }
}

draw_rectangle(bbox_left + textBorder, itemTopStart - 5 + ((itemSelected - scrolledAmount) * 32), bbox_right - textBorder, itemTopStart - 10 + ((itemSelected - scrolledAmount) * 32) + 32, true);

draw_sprite(spr_InventoryDescriptionWall, 0, x + 150, y + 150);

    if(isEmpty){
        draw_text_ext(bbox_right - 110, camera_get_view_width(view_camera[0]) - sprite_get_yoffset(spr_InventoryDescriptionWall) - 10, emptyMessage, 32, sprite_get_width(spr_InventoryDescriptionWall) - textBorder);
    } else {
        draw_text_ext(bbox_right + 15, camera_get_view_y(view_camera[0]) + 425 - sprite_get_yoffset(spr_InventoryDescriptionWall) - 100, ds_grid_get(myItems, 2, itemSelected), 16, sprite_get_width(spr_InventoryDescriptionWall) - textBorder - 4);
    }
}

draw_set_font(-1);
draw_set_color(c_white);
Thanks in advance for any help!
 
W

wesk1288

Guest
Sorry, I probably should have added my additem script as well to give you more info...

addItem Script
Code:
/// @function addItem
///    @description Add an item to a grid
/// @argument0 DSGrid Grid_To_Add
/// @argument1 String Item_Name
/// @argument2 Int Item_Amount
/// @argument3 String Item_Description
/// @argument4 Sprite Item_Sprite
/// @argument5 Script Item_Script

gridToAddTo = argument0;
newItemName = argument1;
newItemAmount = argument2;
newItemDescription = argument3;
newItemSprite = argument4;
newItemScript = argument5;

//Case 1 - Item is already in the inventory
for(i = 0; i < ds_grid_height(gridToAddTo); ++i){
    if(ds_grid_get(gridToAddTo, 0, i) == newItemName){
        ds_grid_set(gridToAddTo, 1, i, ds_grid_get(gridToAddTo, 1, i) + newItemAmount);
        return true;
    }
}

//Case 2 - Item is not in the inventory
if(ds_grid_get(gridToAddTo, 0, 0) != 0)
    ds_grid_resize(gridToAddTo, playerInventoryWidth, ds_grid_height(gridToAddTo) + 1)
   
newItemSpot = ds_grid_height(gridToAddTo) - 1;
ds_grid_set(gridToAddTo, 0, newItemSpot, newItemName);
ds_grid_set(gridToAddTo, 1, newItemSpot, newItemAmount);
ds_grid_set(gridToAddTo, 2, newItemSpot, newItemDescription);
ds_grid_set(gridToAddTo, 3, newItemSpot, newItemSprite);
ds_grid_set(gridToAddTo, 4, newItemSpot, newItemScript);

return true;
I am calling this script whenever the player collides with an item. That is handled here in obj_pickup / Collision with Player Event:


Code:
additem(playerInventory, myItemName, myItemAmount, myItemDescription, myItemSprite, myItemScript);
audio_play_sound(snd_popitem, 10, false);
instance_destroy();
 
The Draw GUI Event draws to a separate surface that has coordinates of 0,0 in the upper left corner, and the view width and height in the bottom right.

So unless your camera/view never moves from its initial position, if you are drawing something in the Draw GUI event using room based coordinates (x, y, bbox_ etc...), you need to subtract the position of the view in the room from those coordinates using camera_get_view_x() and camera_get_view_y() in order for them to show up on the GUI surface.

Code:
draw_text(x - camera_get_view_x(view_camera[0]), y - camera_get_view_y(view_camera[0]), "my_text');
Edit: Also, you are calling draw_set_colour(myColor) -> and myColor seems to be set to c_black.

This might be causing the issue as well.
 
W

wesk1288

Guest
Thanks for the help! I updated my Draw GUI with the camera_get_view functions as you said but the items and their text/descriptions still don't appear. I even changed the color back to the default c_white but no difference. It's as if they're not being drawn at all when I pick them up so it's most likely something else in my code that's throwing it off. However, this was all working just fine when this lived in the draw event. I've tried changing the coordinates of where to draw these text/sprite all over the room but haven't seen anything appear. I even changed the order of it in my Draw GUI event thinking something has been drawn on top of it but nope.

I'm gonna scan the tutorial again and see if there's something I'm missing when adding these items to my ds_grid.
 
Top