I got used to programming languages providing powerful functions, thus I added them to GML so I can easily write cool code for my games. Possibly you'd like to use it too :)
Let's have a look!
“Talk is cheap. Show me the code.”
1. Strings processing
// String formatting, up to 7 arguments:
string_place("Hello, {}!", "Nathan") == "Hello, Nathan!"
string_place("Hello, {1} {0}!", "Tom", "little") == "Hello, little Tom!!"
// Named arguments:
string_args("Hello, {age}-years old {name}!", {name: "Adam", age: 152, skill: "strong"})
== "Hello, 152-years old Adam!"
// Print to console the same way
print("Player pos: {}:{}", x, y);
// Split & join
string_split("This is so cool!", " ") == ["This", "is", "so", "cool!"]
string_join(["This", "is", "so", "cool!"], " ") == "This is so cool!"
// Slice strings in Python fashion:
string_slice("abcdef", 1, -2) == "bcde"
// Simple ones:
string_leading_zeros(16, 4) == "0016"
string_seconds(153) == "02:33"
string_starts_with("Hello", "Hell") == true
string_ends_with("Boring", "ring") == true
string_datetime(date_current_datetime()) == "2021-09-14 02:30"
2. Data structures
// Filter an array using given function
array_filter([1,2,3,4,5], function(x){return x%2==1}) == [1,3,5]
// Find max value
array_find_max([3,14,15,92,65,35]).item == 92
// But you can also specify a getter!
array_find_max([61,80,33,98], function(x){return x%10})
== {index: 3, item: 98, value: 8}
// Appends value to the end of given array
array_append(array, value)
// Extend an array with values of another array
array_extend(array, another_array)
// Slice arrays in Python fashion:
array_slice([1,2,3,4,5], 1, -2) == [2,3,4]
// Sum an array
array_sum([1,2,3]) == 6
// Returns index of value in the array, or -1 if it doesn't exist
array_find([3,14,15,92], 15) == 2
array_find([3,14,15,92], 16) == -1
// Check whether an array contains given value
array_contains([3,14,15,92], 261) == false
// Remove a value from given array
// without save elements order
array_remove_unord([1,2,3,4,5], 3) == [1,2,5,4]
// Like choose, but for arrays
array_random_choice(array)
// Sets intersection
array_intersection([1,2,3,4,5], [4,5,6,7]) == [4,5]
// Sets disjunction
array_intersection([1,2,3,4,5], [4,5,6,7]) == [1,2,3]
// Handy for stats calculations
struct_diff({A: 3, B: 8}, {B: 3, C: 2}) == {A: 3, B: 5, C: -2}
3. Some math
// This function is amazing for health bars
merge_colors(0.5, c_black, c_red, c_yellow, c_green)
// Handy for scattering
distr(radius) == choose(-1,1) * random(radius)
is_between(3.141592, 3, 4) == true
4. Functional programming!
Thanks to my Python & especially Lisp experience :)
// Call multiple function at once
button.on_click = func_combine(game_save, game_exit);
// func_execute(func, args, since=0, to=-1)
// Since GMS 2.3.3 we have script_execute_ext,
// but it doesn't work with methods,
// and this baby does!
// Example:
var s = {a: 3, func: function(b){return a*b}};
func_execute(s.func, [4]) == 12
// partial(func, firsts=[], lasts=[])
// Smth like Python.functools.partial
// - func: target function
// - firsts: array of arguments to prepend
// - lasts: array of aruments to append
// Examples:
var is_ing = partial(string_ends_with, [], ["ing"]);
is_ing("building") == true
is_ing("password") == false
var is_hell = partial(string_starts_with, ["hell"], []);
is_hell("hello") == true
is_hell("hi") == false
// wrapped(wrapper, target)
// Some form of decorator pattern implementation
// Examples:
wrapped(A, B)(X) == A(B(X)
var is_hell = partial(string_starts_with, ["hell"], []);
is_hell("Hello") == false // :(
var is_hell = wrapped(partial(string_starts_with, ["hell"], []), string_lower);
is_hell("hello") == talse // ^_^
Conclustion
The list above is not full, the asset contains even more functions :)
The asset is free to use, but I'll be happy to see a credit. Feel free to contact me on Facebook or Vk.
End User Licence Agreement (EULA).
Initial upload