Skip to content

Commit aca6b9a

Browse files
committed
Add _error and _map_of
Also fix to _keys for empty maps
1 parent 8412873 commit aca6b9a

File tree

10 files changed

+135
-3
lines changed

10 files changed

+135
-3
lines changed

src/gdash.yyp

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,22 @@
1-
test_suite_pass();
1+
/// @description _map_of
2+
test_start("_map_of", "returns a map of the given parameters, paired as key/value");
3+
var res = _map_of(
4+
"hello", 1,
5+
"world", 2,
6+
"test", 3
7+
);
8+
9+
assert_is_true(ds_exists(res, ds_type_map));
10+
assert_equal(res[? "hello"], 1);
11+
assert_equal(res[? "world"], 2);
12+
assert_equal(res[? "test"], 3);
13+
14+
ds_map_destroy(res);
15+
16+
// Odd argument count - fails with empty map
17+
res = _map_of(1);
18+
assert_is_true(ds_exists(res, ds_type_map));
19+
assert_equal(_length(_keys(res)), 0);
20+
ds_map_destroy(res);
21+
22+
test_end();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test_suite_pass();

src/objects/obj_gdash_test_3/obj_gdash_test_3.yy

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/scripts/_error/_error.gml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// @func _error(message [, fatal])
2+
/// @desc When running with the debugger, displays an error window. Otherwise, logs an error using _log.
3+
/// @param {String} message The message to error with
4+
/// @param {Boolean} fatal [Optional] If true, will force a show_error() call with `abort` set to true
5+
/// @note Only mark an error as `fatal` if you are okay with it ending your game
6+
/*
7+
@example
8+
_error("This is an error that will let the game continue");
9+
_error("This is an error that will let the game continue", false);
10+
_error("This is an error that will kill the game", true);
11+
*/
12+
13+
var message = argument[0];
14+
var fatal = argument_count > 1 ? argument[1] : false;
15+
16+
if (fatal || debug_mode) {
17+
show_error(message, fatal);
18+
} else {
19+
_log("ERROR: " + message);
20+
}

src/scripts/_error/_error.yy

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/scripts/_keys/_keys.gml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ _keys(map);
1616
var map = argument0;
1717
var nextKey = ds_map_find_first(map);
1818
var nextInd = 0;
19-
var keys;
19+
var keys = [];
2020

2121
while (!is_undefined(nextKey)) {
2222
keys[nextInd++] = nextKey;

src/scripts/_map_of/_map_of.gml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// @func _map_of(values...)
2+
/// @desc Returns a DS_Map containing the provided values. Arguments are split into key/value pairs in the order they are provided.
3+
/// @note This script must take an even number of arguments. Keys can only be integers or strings.
4+
/// @param {*} values... As many starting values for the map as desired
5+
/// @returns {DS_Map} A new DS_Map containing the provided key/value pairs
6+
/*
7+
@example
8+
9+
var map = _map_of(
10+
"health", 100,
11+
"mana", 20,
12+
"level", 1
13+
);
14+
15+
map[? "health"] // = 100
16+
map[? "mana"] // = 20
17+
map[? "level"] // = 1
18+
19+
*/
20+
21+
if (argument_count % 2 != 0) {
22+
_error("_map_of must take an even number of arguments. This call will return an empty map.", false);
23+
return ds_map_create();
24+
}
25+
26+
// Ensure keys are integers or strings
27+
for (var i = 0; i < argument_count; i += 2) {
28+
var key = argument[i];
29+
var type = typeof(key);
30+
if (type != "number" && type != "string") {
31+
_error("_map_of must take keys that are strings or integers. This call will return an empty map.\nOffending key: " + string(key), false);
32+
return ds_map_create();
33+
}
34+
}
35+
36+
var map = ds_map_create();
37+
38+
for (var i = 0; i < argument_count; i++) {
39+
var key = argument[i++];
40+
var value = argument[i];
41+
map[? key] = value;
42+
}
43+
44+
return map;

src/scripts/_map_of/_map_of.yy

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/views/8ebd4763-7203-42aa-9554-cc55d03a802b.yy

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)