NOTE: This project was made before GMS released an array functionality update which effectively rendered the majority of this project redundant. However, this library does add some extra functions like array_some()
JS Arrays is a small library that aims to extend native array functions to provide identical functionality to JS arrays.
View the Github Repo here
Some useful examples
array_map()
// Creates a new array which contains just the names of items
var items = [{
name: "Sword",
type: TYPE.melee
}, {
name: "Bow",
type: TYPE.ranged
}]
var item_names = array_map(items, function(item) {return item.name})
// returns a new array ["Sword", "Bow"]
array_filter()
// Creates a new array with the filtered items
inventory = [{id: 4, amt: 32}, {id: 5, amt: 12}]
potion_items = array_filter(inventory, function(slot) {
// item_get() is a global function used to retrieve items details from a slot
return (item_get(slot).type == TYPE.potion)
})
// returns a new array with items related to a potion
array_some()
// If at least 1 element satisfies the predicate, return true. Otherwise false.
has_ring = array_some(equipped, function(slot) {
return (item_get(slot).type == TYPE.ring)
})
// returns true if they have a ring equipped. False otherwise.
To understand more about each function's details in this library, please refer to various online sources about JavaScript arrays here1 and here2.
Limitations
Game maker doesn't support closures. So doing something like
var some_var = TYPE.potion;
filtered_items = array_filter(inventory, function(slot) {
return (item_get(slot).type == some_var) // error: "some_var" not set before reading it
})
Will not work. Read this for more details on what a closure is, and how it works.
End User Licence Agreement (EULA).