Skip to content

Commit 7952ac9

Browse files
committed
add array_find example
1 parent a060f1c commit 7952ac9

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
randomise();
22
test_array = [1, -3, "foo", { n: "bar" }, [0, 2.333]];
3-
choice = choose_from_array(test_array);
3+
choice = choose_from_array(test_array);
4+
found = array_find(test_array, "foo");

current-scripts/Demos/useful-scripts/objects/obj_data_structures_demo/Draw_0.gml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ draw_text(room_width/2, 16, "Data Structures Demo");
55

66
draw_set_halign(fa_left);
77
draw_text(16, 64, "Choose From Array");
8+
draw_text(16, 160, "Array Find (deprecated, use array_find_index instead)");
89

910
draw_set_font(fnt_demo);
1011

1112
// Choose From Array
1213
draw_text(16, 96, "Original Array: " + string(test_array));
13-
draw_text(16, 128, "Random Choice: " + string(choice));
14+
draw_text(16, 128, "Random Choice: " + string(choice));
15+
16+
// Array Find
17+
draw_text(16, 192, "Index of Foo: " + string(found));

current-scripts/Demos/useful-scripts/objects/obj_data_structures_demo/obj_data_structures_demo.yy

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

current-scripts/Demos/useful-scripts/scripts/data_structures/data_structures.gml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ function array_is_subset(_a, _b) {
4646
/// @param {Array} array
4747
/// @param {any} value
4848
function array_find(_array, _value) {
49-
array_foreach(_array, function(_element, _i) {
50-
if (_element == _value) return _i;
51-
});
52-
49+
var _len = array_length(_array);
50+
for (var i=0; i<_len; i++) {
51+
if (_array[i] == _value) return i;
52+
}
53+
5354
return -1;
5455
}
5556

0 commit comments

Comments
 (0)