0

想象一下,您正在尝试创建一个数据结构,该结构将包含所有可能装备在 RPG 中的装备的所有数据。

在这种情况下,一个齿轮可以由四个值表示:一个定长数组 length C、一个变长数组 length 0 < n < 5、一个字符串和一个实数。我是 gml 的新手,但我倾向于为此使用 2D 数组:

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

然后,所有的齿轮部件都由各种独特的 2d 数组表示,例如这个(数百个,带有硬编码值)。我想做的是将所有这些数组存储在像 ds_map 这样的数据结构中(本身存储在游戏开始时创建的持久控制器对象中),其中从地图的特定键获得的值是该片的二维数组用那把钥匙的齿轮。就像是:

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.

我的问题是我将如何填充该 ds_map,或者是否可以创建这样的数据结构?我需要澄清一下,因为从我(初学者)对 gml 以及数组如何在其中工作的理解来看,以下代码是有问题的:

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

这还存在其他问题,第一个2d_array是局部变量,那么当脚本完成运行时,数据结构中的引用是否还有任何意义?我认为不会。但即使将其更改为实例变量,仍然存在覆盖问题,并且显式创建数百个实例变量来保存所有二维数组的解决方案似乎很荒谬,就像使用gearObject 并创建数百个持久的不可见实例一样。我欢迎对上述代码提出任何建议,或如何最好地存储齿轮数据的替代解决方案。

在相关说明中,我在手册中看到了有关在其他 ds_lists 中存储 ds_lists 或映射的说明,但它说这仅适用于 JSON。我可以将数据结构存储在其他数据结构中吗?

4

1 回答 1

0

我想到了一种可能的解决方案,即通过使用一个或两个来创建一种伪地图,ds_gridsenum指定我自己的自定义 id 用于查找值。定义起来比较麻烦,但是无论哪种方式都会有很多硬编码,所以我现在就试试这个。仍然欢迎任何其他意见。

于 2017-03-28T02:11:33.757 回答