[go: up one dir, main page]

Skip to content

The most complete list class for GameMaker Studio 2.3+. GC-friendly, [  ] accessor, 50+ functions (add, remove, insert, contains, find, sort, shuffle, reverse, etc.) and reference as array.

License

Notifications You must be signed in to change notification settings

KeeVeeGames/ArrayList.gml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ArrayList.gml

Donate License

It is a custom List class / struct build on top of a standard GameMaker array. Works only with the 2.3+ version which introduced structs and constructors.

The advantages over ds_list or other struct realizations are: garbage collected, fast sort function, [] accessor and referencing as an array.

ArrayList is GC-friendly as it's basically an array inside of a struct so no need to manually destroy it as you do with the ds_list (although you can use delete on a ArrayList to dispose it explicitly).

There are some methods, including analogues of corresponding ds_list functions such as: add, remove, insert, sort, shuffle, reverse, copy and other.

Array reference is available from the struct in the array property and then can be passed anywhere or used with an accessor. Array reference is staying the same for the all lifecycle of struct so any changes made with that array will also reflects on the ArrayList and other way, if you pass that array somewhere (inside another data structure, draw it on GUI, etc.), all changes made with the ArrayList struct will also change the array. Basically you have all the array scripts ever written for GM available for this struct also.

So it is both an array and a list:

list = new ArrayList();

list.add("howdy", "ho", "world", "!");
list.remove(1);

list.array[0] = "hello";
array_resize(list.array, 2);

Weak parts are: slower than pure array or ds_list, no outside of bounds checking and possibility of losing the reference if reassign it explicitly via list.array = [], which you shouldn't do.

JSON Serialization:

You can easily encode/decode ArrayList to Json and vice versa with this packages:

Installation:

Get the latest asset package from the releases page. Import it into IDE.
Alternatively copy the code from corresponding scripts into your project.

Dependencies:

ArrayList is using quicksort package so you need to import this also if you do install as asset package and not from sources.

Syntax:

Example:

var list1 = new ArrayList([2, 4, 43, 50, 79, 36, 93, 54, 99, 26, 65, 69, 81, 63, 72, 3]);

list1.add(10, 9);

list1.remove(list1.size() - 2);

list1.insert(2, 5);

list1.array[2] = 6;
list1.array[list1.find_index(81)] = 18;

var list2 = new ArrayList();

list2.copy(list1);

list1.sort(true);
list2.shuffle();

show_debug_message(list1.array);
show_debug_message(list2.array);

Authors:

Nikita Musatov - MusNik / KeeVee Games

Contributors:

License: MIT