ds_map_set

此函数设置给定 DS 映射内的键值。

您提供 DS Map 值 (由函数 ds_map_create 返回),然后给出您要设置的键以及要设置的值。如果给定的键不存在,那么将为您创建它并设置为该值。

由于该函数不返回任何内容,因此您应该使用函数 ds_map_replace 来检查键值是否已被替换或是否已创建新键。

提示 键不限于字符串,可以是任何类型,包括 结构体

注意 此函数与使用 DS 地图访问器 设置 / 创建地图键 / 值对相同。

 

语法:

ds_map_set(id, key, value)

参数类型描述
idDS Map要使用的映射的ID。
keyAny要设置的关键。
valueAny要将密钥设置为的值。

 

返回:

N/A

 

例子:

struct_key = {a: 10, b: "Some text"};

map_key_value_pairs = ds_map_create();
ds_map_set(map_key_value_pairs, "score", "the value belonging to the string key");
ds_map_set(map_key_value_pairs, 8, "The value belonging to the real number key");
ds_map_set(map_key_value_pairs, struct_key, "The value for the struct key");

show_debug_message(map_key_value_pairs[? "score"]);
show_debug_message(map_key_value_pairs[? 8]);
show_debug_message(map_key_value_pairs[? struct_key]);

上面的代码首先创建一个基本结构体 struct_key。然后,它使用 ds_map_create 创建一个新的 DS 地图,并将其存储在变量 map_key_value_pairs 中。

接下来,使用 ds_map_set 设置 DS 映射中的三个不同键,每个键具有不同的类型:第一个键是字符串,第二个键是数字,最后一个键是结构体。

最后,使用 DS 地图访问器 ? 查找这三个值,并使用 show_debug_message 显示这三个值。