0

Imagine you are trying to create a data structure that will hold all of the data on all of the gear that can possibly be equipped in an RPG.

In this case one piece of gear can be represented by four values: a fixed-length array of length C, a variable-length array of length 0 < n < 5, a string, and a real number. I am new to gml, but my inclination is to use a 2D array for this:

2d_array[0, 0] = fixed_length[0]
...
2d_array[0, C-1] = fixed_length[C-1]
2d_array[1, 0] = variable_length[0]
...
2d_array[1, n-1] = variable_length[n-1]
2d_array[2, 0] = string
2d_array[3, 0] = real_number

All of the pieces of gear, then, are represented by various unique 2d arrays such as this (hundreds of them, with hard-coded values). What I would like to do is store all of these arrays in a data structure like ds_map (itself stored in a persistent controller object created at game start), where the value obtained from a specific key of the map is the 2d array for the piece of gear with that key. Something like:

gear_piece = ds_map_find_value(map, "key");  //do things with gear_piece as a 2d array
gear_name = gear_piece[2, 0]; //e.g.

My question is how would I go about populating that ds_map, or is it possible to create such a data structure? I need clarification, because from my (beginner) understanding of gml and how arrays work in it, the following code is problematic:

var 2d_array;
2d_array[3, 0] = 3;
2d_array[2, 0] = "Book of Life";

//now the variable-length array; for this piece of gear it is length 2
2d_array[1, 1] = 26;
2d_array[1, 0] = 10;

//now the fixed-length array
//SIZE is a macro for the length of the fixed-length array

i = SIZE-1;
repeat(SIZE) {
    2d_array[0, i] = 0;
    i -= 1;
} //just initializes to zeros

//hard-coded arbitrary gear values
2d_array[0, 0] = 40;
2d_array[0, 2] = 60;

//now add a reference to 2d_array to the ds_map with a unique key for that piece of gear
m_ds_gear[? "book-of-life"] = 2d_array;

//do the same process for the next piece of gear
2d_array[3, 0] = 1;
2d_array[2, 0] = "Book of Death";
...
2d_array[0, 1] = 20;
m_ds_gear[? "book-of-death"] = 2d_array;

//but now, since m_ds_gear["book-of-life"] just contains the id referencing 2d_array,
//I haven't really stored multiple arrays, right?
//I've just overwritten the old array values and stored a reference to it twice

There are also other problems with this, the first being that 2d_array is a local variable, and so will the reference in the data structure even mean anything when the script finishes running? I would think not. But even changing that to an instance variable, still has the overwriting problem, and the solution of explicitly creating hundreds of instance variables to hold all the 2d arrays seems ridiculous, as does using a gear Object and creating hundreds of persistent invisible instances. I welcome any suggestions on the above code, or alternative solutions of how to best store the gear data.

On a related note, I see notes in the manual about storing ds_lists or maps within other ds_lists, but it says this is only for use with JSON. Can I store data structures within other data structures?

1 Answers1

0

I've thought of one possible solution, which is to create a kind of pseudo-map by using one or two ds_grids and an enum that specifies my own custom ids to use in looking up values. It is messier to define, but there will be a lot of hard-coding either way, so I'll try this for now. Still welcome any other opinions.