Locke
Member
Hey Community,
I have a simple inventory using ds_lists. I've implemented picking items up, dropping items and displaying items in an inventory. The only thing left to do is be able to stack the items but I can't quite figure it out. Was hoping someone could help.
I have a sprite that has 7 images of different items you can pick up. Then I have a player object with the following code:
create event:
//List
global.inventory = ds_list_create();
selected_item = 0;
type = -1;
show_inventory = true;
enum e_items
{
pea,
beatroot,
cabbage,
onion,
carrot,
potato,
bean,
tomato
}
Step event:
var item = instance_place(x, y, obj_crops);
var count = 1;
if (item != noone and item.can_be_picked_up == true)
{
show_debug_message("item picked up");
ds_list_add(global.inventory, item.image_index, count);
with (item)
{
instance_destroy();
}
}
Draw GUI Event:
if (show_inventory == false)
{
var margin = 10;
var list_size = ds_list_size(global.inventory);
for (var entry = 0; entry < list_size; entry++)
{
var arr = global.inventory[| entry];
var item = arr[0];
var count = arr[1];
var x_offset = sprite_get_xoffset(spr_crops);
var y_offset = sprite_get_yoffset(spr_crops);
var spr_width = sprite_get_width(spr_crops) + margin;
var draw_x = margin + (x_offset + (entry * spr_width));
var draw_y = (margin * 2) + y_offset;
//Draw the item sprite with the correct image
if (item != undefined)
{
draw_sprite(spr_crops, item, draw_x, draw_y);
draw_text(draw_x+4, draw_y+4, count);
}
//Show which item is selected
if (selected_item == entry)
{
draw_sprite(spr_inventory, 0, draw_x, draw_y);
}
}
}
When I run the game and try to pick up an item it says ... error in draw event of obj_player... trying to index a variable that is not an array... at line (11) var item = arr[0];
Would love if someone could show me what I'm missing. Thanks!
I have a simple inventory using ds_lists. I've implemented picking items up, dropping items and displaying items in an inventory. The only thing left to do is be able to stack the items but I can't quite figure it out. Was hoping someone could help.
I have a sprite that has 7 images of different items you can pick up. Then I have a player object with the following code:
create event:
//List
global.inventory = ds_list_create();
selected_item = 0;
type = -1;
show_inventory = true;
enum e_items
{
pea,
beatroot,
cabbage,
onion,
carrot,
potato,
bean,
tomato
}
Step event:
var item = instance_place(x, y, obj_crops);
var count = 1;
if (item != noone and item.can_be_picked_up == true)
{
show_debug_message("item picked up");
ds_list_add(global.inventory, item.image_index, count);
with (item)
{
instance_destroy();
}
}
Draw GUI Event:
if (show_inventory == false)
{
var margin = 10;
var list_size = ds_list_size(global.inventory);
for (var entry = 0; entry < list_size; entry++)
{
var arr = global.inventory[| entry];
var item = arr[0];
var count = arr[1];
var x_offset = sprite_get_xoffset(spr_crops);
var y_offset = sprite_get_yoffset(spr_crops);
var spr_width = sprite_get_width(spr_crops) + margin;
var draw_x = margin + (x_offset + (entry * spr_width));
var draw_y = (margin * 2) + y_offset;
//Draw the item sprite with the correct image
if (item != undefined)
{
draw_sprite(spr_crops, item, draw_x, draw_y);
draw_text(draw_x+4, draw_y+4, count);
}
//Show which item is selected
if (selected_item == entry)
{
draw_sprite(spr_inventory, 0, draw_x, draw_y);
}
}
}
When I run the game and try to pick up an item it says ... error in draw event of obj_player... trying to index a variable that is not an array... at line (11) var item = arr[0];
Would love if someone could show me what I'm missing. Thanks!