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

Problem converting 1D array to 2D array

D

Diveyoc

Guest
I'm trying to change what I know from a 1D array to a 2D array. It seems that this 1 command line is hanging me up.
button[c,r].slot = [c,r]; - (it won't accept this command)
button[c,r].slot = c; - (This was a test. When I pick up an item it fills the entire column)
button[c,r].slot = r; - (This was a test. When I pick up an item it fills the entire row)
_____________________________________________________________________________
//==== Create Event From Tutorial - 1D Array
for (i = 0; c < global.max_items; i += 1)
{
global.inventory[ i ] = -1;
button[ i ] = instance_create(0, 0, obj_invisible_button);
button[ i ].slot = i;
}
}
_____________________________________________________________________________

//==== My Code For 2D Array
for (c = 0; c < global.max_columns; c += 1)
{
for (r = 0; r < global.max_rows; r += 1)
{
global.inventory[c,r] = -1;
button[c,r] = instance_create(0, 0, obj_invisible_button);
button[c,r].slot = [c,r];
}
}
 

jo-thijs

Member
TheouAegis' suggestion should suffice and is in a lot of cases a very good way of doing this.
It is not the most general solution though (if you're using indices beyong [255, 255]), so I'll just give a more general alternative as well:
Change:
Code:
button[c,r].slot = [c,r];
to:
Code:
var pair;
pair[1] = r;
pair[0] = c;
button[c,r].slot = pair;
And you can later read r and c back by using:
Code:
var pair = /* some code pointing to the instance */.slot;
c = pair[0];
r = pair[1];
 
D

Diveyoc

Guest
Ok, this pushes me down the line a little further and I now get an error in the obj_invisible_button draw event.
I assume this needs to be changed like you mentioned in section 3 of that code, I just don't exactly how.
Is putting global.inventory[slot] in brackets the same as typing global.inventory.slot ??

//==== obj_new_inventory - Here's how I added it
Code:
for (c = 0; c < global.max_columns; c += 1)
    {
    for (r = 0; r < global.max_rows; r += 1)
    {
    global.inventory[c,r] = -1;
    global.button[c,r] = instance_create(0, 0, obj_invisible_button);
    var pair;
    pair[1] = r;
    pair[0] = c;
    global.button[c,r].slot = pair; 
    }
    }
//==== obj_invisible_button - (error with this command)
Code:
var move_item = global.inventory[slot];
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object obj_invisible_button:

DoConv :1: illegal array use
at gml_Object_obj_invisible_button_DrawEvent_1 (line 2) - var move_item = global.inventory[slot];
############################################################################################
 

Binsk

Member
You are essentially creating a small array (called "pair") and storing it inside a 2D array. To read an array you need to provide an index (or indices) in the array to read from.

When you attempt to read the value from global.inventory you are not providing the two required indices but rather an array as the index. This will not work. You must extract the values from the array pair and manually plug them into the large array.

All this to say, change that last bit of code to something like this:
Code:
var __r = slot[1],
    __c = slot[0];
var move_item = global.inventory[__r, __c];
 
D

Diveyoc

Guest
Ok, I'm assuming my obj_new_inventory create event is ok now. I'm not getting any errors when I compile, but when I right click to add my item, nothing draws in the inventory.
Do I need to change up more code in these 2 events or the script?

obj_invisible_button - Draw Event
Code:
//var pair;
//pair[0] = c;   
//pair[1] = r;

var __c = slot[0];
var __r = slot[1],
var item = global.inventory[__c, __r];
var click = mouse_check_button_pressed(mb_left);

if (abs(mouse_x-x) < 21) && (abs(mouse_y-y) < 21)
    {
    draw_set_color(c_white);
    draw_rectangle(x-21,y-21,x+21,y+21,0);
    if (click)
        {
        if (item != -1)
            {
            scr_item_drop_slot(slot);
            }
            if (global.mouse_item != -1)
            {
            scr_item_pickup_slot(global.mouse_item,slot)
            }
            global.mouse_item = item;
        }
    }
if (item != -1)
    {
    draw_sprite(spr_items, item, x, y)
    }
obj_red_ball - Right Mouse Press event
Code:
scr_item_pickup(1);
audio_play_sound(snd_whoosh,0,0);
instance_destroy();
scr_item_pickup
Code:
for (c = 0; c < global.max_columns; c += 1)
    {
    for (r = 0; r < global.max_rows; r += 1)
        {
        if (global.inventory[c,r] == -1)  //  IF SLOT C,R IS EMPTY
            {
            global.inventory[c,r] = argument0;
            return(1);
            }
        }
    }
return(0);
 

jo-thijs

Member
I can't find anything wrong with those codes.
My guess would be that this here is causing issues:
Code:
    global.button[c,r] = instance_create(0, 0, obj_invisible_button);
You're creating every button on the same place.
The first slot might be working correctly, but other slots might be drawing over it.

Can you verify this by replacing that line with:
Code:
    global.button[c,r] = instance_create(c * 42 + 21, r * 42 + 21, obj_invisible_button);
If that's not the issue, can you verify if any sprite is drawn at all by replacing:
Code:
if (item != -1)
    {
    draw_sprite(spr_items, item, x, y)
    }
with:
Code:
if (item != -1)
    {
    show_message("Test");
    draw_sprite(spr_items, item, x, y)
    }
Does a message saying "Test" pop up when you add an item to the inventory?
 
Top