• 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 Java vector array equivalent for GML?

M

Major Cooke

Guest
As someone who is partly coming from Java, it was possible to store vectors in an array.

I've tried looking at ds_list, but it's confusing as to how to access an X and Y slot... So for right now I'm forced to use this awful thing:

Code:
enum Inv
{
    Item = 0,
    Amount = 1,
    X = 2,
    Y = 3,
    Slots = 37
};

Slot = [Inv.Slots, 4]; //Hold an item, an amount, x and y coordinates.

// Allocate the amount first.
Slot[Inv.Slots, Inv.Item] = noone;
Slot[Inv.Slots, Inv.Amount] = 0;

for (var i = 0; i < Inv.Slots; i++)
{
    Slot[i, Inv.Item] = noone;
    Slot[i, Inv.Amount] = 0;
}

// Weapons
Slot[0, Inv.X] = 672;    Slot[0, Inv.Y] = 43;
Slot[1, Inv.X] = 672;    Slot[1, Inv.Y] = 95;
Slot[2, Inv.X] = 672;    Slot[2, Inv.Y] = 147;

// Spells
Slot[3, Inv.X] = 870;    Slot[3, Inv.Y] = 43;
Slot[4, Inv.X] = 870;    Slot[4, Inv.Y] = 95;
Slot[5, Inv.X] = 870;    Slot[5, Inv.Y] = 147;

// Row 1
Slot[6, Inv.X] = 642;    Slot[6, Inv.Y] = 243;
Slot[7, Inv.X] = 694;    Slot[7, Inv.Y] = 243;
Slot[8, Inv.X] = 746;    Slot[8, Inv.Y] = 243;
Slot[9, Inv.X] = 798;    Slot[9, Inv.Y] = 243;
Slot[10, Inv.X] = 850;    Slot[10, Inv.Y] = 243;
Slot[11, Inv.X] = 902;    Slot[11, Inv.Y] = 243;

// Row 2
Slot[12, Inv.X] = Slot[6, Inv.X];    Slot[12, Inv.Y] = 295;
Slot[13, Inv.X] = Slot[7, Inv.X];    Slot[13, Inv.Y] = Slot[12, Inv.Y];
Slot[14, Inv.X] = Slot[8, Inv.X];    Slot[14, Inv.Y] = Slot[12, Inv.Y];
Slot[15, Inv.X] = Slot[9, Inv.X];    Slot[15, Inv.Y] = Slot[12, Inv.Y];
Slot[16, Inv.X] = Slot[10, Inv.X];    Slot[16, Inv.Y] = Slot[12, Inv.Y];
Slot[17, Inv.X] = Slot[11, Inv.X];    Slot[17, Inv.Y] = Slot[12, Inv.Y];

// Row 3
Slot[18, Inv.X] = Slot[6, Inv.X];    Slot[18, Inv.Y] = 347;
Slot[19, Inv.X] = Slot[7, Inv.X];    Slot[19, Inv.Y] = Slot[18, Inv.Y];
Slot[20, Inv.X] = Slot[8, Inv.X];    Slot[20, Inv.Y] = Slot[18, Inv.Y];
Slot[21, Inv.X] = Slot[9, Inv.X];    Slot[21, Inv.Y] = Slot[18, Inv.Y];
Slot[22, Inv.X] = Slot[10, Inv.X];    Slot[22, Inv.Y] = Slot[18, Inv.Y];
Slot[23, Inv.X] = Slot[11, Inv.X];    Slot[23, Inv.Y] = Slot[18, Inv.Y];

// Row 4
Slot[24, Inv.X] = Slot[6, Inv.X];    Slot[24, Inv.Y] = 399;
Slot[25, Inv.X] = Slot[7, Inv.X];    Slot[25, Inv.Y] = Slot[24, Inv.Y];
Slot[26, Inv.X] = Slot[8, Inv.X];    Slot[26, Inv.Y] = Slot[24, Inv.Y];
Slot[27, Inv.X] = Slot[9, Inv.X];    Slot[27, Inv.Y] = Slot[24, Inv.Y];
Slot[28, Inv.X] = Slot[10, Inv.X];    Slot[28, Inv.Y] = Slot[24, Inv.Y];
Slot[29, Inv.X] = Slot[11, Inv.X];    Slot[29, Inv.Y] = Slot[24, Inv.Y];

// Row 5
Slot[30, Inv.X] = Slot[6, Inv.X];    Slot[30, Inv.Y] = 451;
Slot[31, Inv.X] = Slot[7, Inv.X];    Slot[31, Inv.Y] = Slot[30, Inv.Y];
Slot[32, Inv.X] = Slot[8, Inv.X];    Slot[32, Inv.Y] = Slot[30, Inv.Y];
Slot[33, Inv.X] = Slot[9, Inv.X];    Slot[33, Inv.Y] = Slot[30, Inv.Y];
Slot[34, Inv.X] = Slot[10, Inv.X];    Slot[34, Inv.Y] = Slot[30, Inv.Y];
Slot[35, Inv.X] = Slot[11, Inv.X];    Slot[35, Inv.Y] = Slot[30, Inv.Y];
Basically this is just setting up the rows and columns of an inventory system for drawing the icons within.

If anyone has any ideas/examples on how to properly put those things into a list (or better) and on how to access them, that'd be greatly appreciated in lending a hand.
 

Simon Gust

Member
I think it would be less of a pain if you made the first index the X and the second index the Y. The value should be indexing a ds_list inside memory.
In the list you would hold all the required data (item, amount...).
Code:
// create list
slot[0, 0] = ds_list_create(); // slot[0, 0] holds the index of the list (no data)

// write to list
ds_list_add(slot[x, y], item, amount);
// or
ds_list_replace(slot[x, y], item, amount);

// read from a list
var item = ds_list_find_value(slot[x, y], Inv.Item);
var amount = ds_list_find_value(slot[x, y], Inv.Amount);
 
M

Major Cooke

Guest
How am I supposed to access particular slots with that, though? That's the biggest thing. I have 36 in total, each with their own coordinates. I don't want to have to memorize what the coordinates are in particular.
 

Simon Gust

Member
How am I supposed to access particular slots with that, though? That's the biggest thing. I have 36 in total, each with their own coordinates. I don't want to have to memorize what the coordinates are in particular.
I see, you have an inventory that isn't just a grid. It features slots for weapons, speels and other kind of gear as well.
Maybe a 1D array with lists is already enough. Then you can save the positions inside the lists.
Basically like you did it originally plus having the array point towards the created list in memory.
create / write slot
Code:
slot[0] = ds_list_create();
ds_list_add(slot[0], item, amount, x, y);
read slot
Code:
var item = ds_list_find_value(slot[0], Inv.Item);
var amount = ds_list_find_value(slot[0], Inv.Amount);
var xx = ds_list_find_value(slot[0], Inv.X);
var yy = ds_list_find_value(slot[0], Inv.Y);
 
M

Major Cooke

Guest
I believe that a ds_grid may be what you want.
I thought the same at first but no. It really isn't.

I see, you have an inventory that isn't just a grid. It features slots for weapons, speels and other kind of gear as well.
Maybe a 1D array with lists is already enough. Then you can save the positions inside the lists.
Basically like you did it originally plus having the array point towards the created list in memory.
I'll give this a try.

Although I wonder if I should be doing it the other way around -- instead of creating a ton of lists, why not have one massive list instead? Though I have yet to figure that out yet...
 

Simon Gust

Member
I thought the same at first but no. It really isn't.


I'll give this a try.

Although I wonder if I should be doing it the other way around -- instead of creating a ton of lists, why not have one massive list instead? Though I have yet to figure that out yet...
I think the memory you'd save is not worth the pain. That is my opinion. Unless you want to do bitmasking which is the way I do it.
 

Simon Gust

Member
I'd like an example of how you bitmask it.
I am going to assume you know binary math and data types.
GM values are all 64 bit values.
I will seperate those into
8 for items (0-255)
8 for amounts (0-255)
16 for X (0 - 65535)
16 for Y (0 - 65535)
making 48 bits in total.

I write scripts to make it readable
Code:
/// setAll(item, amount, X, Y)
var item = argument0;
var amount = argument1;
var X = argument2;
var Y = argument3;

var key = item | amount << 8 | X << 16 | Y << 32;
return (key);
Code:
/// getItem(key)
var key = argument0;
return (key & 255);
Code:
/// getAmount(key)
var key = argument0;
return ((key & 65280) >> 8);
Code:
/// getX(key)
var key = argument0;
return ((key & 4294901760) >> 16);
Code:
/// getY(key)
var key = argument0;
return ((key & 281470681743360) >> 32);
It's best to open the windows calculator and go the programmer and copy the numbers to see the bitmasks.
 
E

Edmanbosch

Guest
I thought the same at first but no. It really isn't.
Looking a bit more at what you are asking for, you may actually just want to do an array of ds_lists instead or an array of arrays(if you have GMS2) or maybe a ds_list of ds_lists or a ds_list of arrays.
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Question: why not have SlotX[row, column] and SlotY[row, column] instead of what you have now? That'd be both more readable and more straightforward to work with.
 
T

The Sentient

Guest
How I would go about it.

  • Create on object with the variables you want to access
  • Create instances of the object
  • Dectivate the instance
  • Put the instance on a ds_list like you were attempting the the OP

Deactivating the instance stops all of the events other than the create event. Effectively making the object a 'struct'. The variables are still accessible and can be modified as you see fit :)
 

Simon Gust

Member
Question: why not have SlotX[row, column] and SlotY[row, column] instead of what you have now? That'd be both more readable and more straightforward to work with.
I suggested that but it is kinda nice to have specialised slots seperated from the main inventory.
This is what it should look like I think. The most elegant solution would be seperating the inventories as the main one
inventorySlot[row, col] = ds_list_create()
weaponSlot[row] = ds_list_create()
spellSlot[row] = ds_list_create()
upload_2017-10-13_8-38-12.png
 
Top