On this page are only some features listed, for a complete overview please see the API documentation.
All functions are handling Array
s and other types as immutable (unless explicitly specified).
1var inputArray = [6, 9]
2var outputArray = G(inputArray).map(GG.id).val # new array equal to [6, 9]
Most functions/methods should support following types when expecting a function (referred to as function-like/FunctionLike
):
FuncRef
- obtained by calling funcref
1# following expressions are same
2G([1, 2]).map(GG.inc).val # [2, 3]
3G([1, 2]).map(funcref(GG, "inc_")).val # [2, 3]
String
- compiled to an anonymous function1G([1, 2]).map("x => x + 1").val # [2, 3]
2#
3# the in-line function is similar to:
4func f(x): return x + 1
5G([1, 2]).map(funcref(self, f)).val
6#
7# "->" marks non-returning function (return type is void)
8G(["Firefly", "Daedalus"]).for_each("x -> print(x)") # prints Firefly and Daedalus (two calls)
Array
- a tuple where first item is function-like, second item is value to apply (from right)1# y parameter is partially applied (is passed as 10 to the anonymous function)
2G([1, 2]).to(["x, y => x.size() * y", 10]) # returns 20
with_ctx
/with_ctx2
- older form, mimics FuncRef
A pipe accepts an input value and a pipeline (Array
of function-like). An input value is passed to the first function, result of the first function is passed to the second function and so on, until the last function which result is returned from the pipe
function as a final result.
1# Take 3 first words and capitalize them
2
3var text_input = "Morbi id mauris pep erisus. Aenean."
4
5var three_words_capitalized = GG.pipe_(text_input, [\
6 GG.words_raw, # ["Morbi", "id", "mauris", "pep", "erisus.", "Aenean."]
7 [GG.take, 3], # ["Morbi", "id", "mauris"]
8 [GG.map, GG.capitalize], # ["Morbi", "Id", "Mauris"]
9 GG.unwords # "Morbi Id Mauris"
10])
11assert(three_words_capitalized == "Morbi Id Mauris")
Similar to a pipe
function, but instead of immediately doing computation, flow
returns a "function" (callable object instance with a similar interface as FuncRef
) which behaves as it would be composed of all functions in a passed pipeline. flow
is commonly known as a reverse composition operator (e.g. >>>
in Haskell).
call_spread
: invokes a function with given arguments in a form of an array - it "spreads" the arguments (not easily doable in Godot 3.1)flip
: accepts function of two arguments, returns a new function with flipped order of argumentskey_from_val
: finding a key from valuekeys
: unified way of getting array of keys for Dictionary
and custom classesap_if_defined
: call method when method exists and pass result, otherwise null
get_fields
/set_fields
: batch access to fieldsmerge
: merging two dictionariespick
: construct a new dictionary from a given list of fields of a given object/dictionaryassign_fields
, omit
Array utilities known from other dynamic and/or functional languages. Golden Gagdet also contains an array wrapper with fluent API - GGArray
- to streamline working with arrays. some examples:
map
, filter
, foldl
(aka reduce
)head
, tail
, last
, init
, take
, take_right
, drop
, drop_right
, take_while
, drop_while
, drop_while_right
append
, prepend
, concat
, concat_left
sort
, zip
, reverse
(aka inverse
)flatten
: flattens one level of array of arrays, e.g. [[1, 2], [3]]
-> [1, 2, 3]
without
: remove all occurrences of given value from an arraysample
/sample_or_null
: get random item from an arraysum
/product
all
/any
: does predicate (given function returns true
) for all/any items of an array?find
/find_or_null
/find_index
/find_index_or_null
: finds a first item/index for which predicate holdsgroup_with
transpose
nub
and unique
zip_with_index
and map_with_index
intersect
, union
, difference
elem
, not_elem
: similar to has
methodvalid_index
GGArray
1var monsters = [
2 Monster.new(0, "Orc"), # hp, name
3 Monster.new(5, "Demon"),
4 Monster.new(12, "Amus"),
5 Monster.new(0, "Borg"),
6]
7
8# Names of Weak and Alive monsters example
9
10# imperative solution
11var weak_alive_monsters_imperative = []
12for monster in monsters:
13 if monster.is_alive && monster.hp < 10:
14 weak_alive_monsters_imperative.push_back(monster.name)
15
16# functional approach, uses lambdas (anonymous functions)
17var weak_alive_monsters =
18 G(monsters).filter("x => x.is_alive && x.hp < 10").map("x => x.name").val
19
20assert(weak_alive_monsters_imperative == ["Demon"])
21assert(weak_alive_monsters == ["Demon"])
1GG.new_array_("x", [2, 3]) # [["x", "x", "x"], ["x", "x", "x"]]
2GG.generate_array_("x => x[0] * 10 + x[1]", [2, 3]) # [ [0, 1, 2], [10, 11, 12] ]
String helpers like words
, unwords
, lines
, unlines
, join
.
floats_are_equal
: floats comparison (range, not a strict equality check)clampi
: clamp
for int
s1GG.format_float_2_(1.23456) # "1.23"
2GG.format_vec2_2_(Vector2(1.2345, 0)) # "1.23, 0.00"
3GG.format_vec3_2_(Vector3(1.2345, 0, 7)) # "1.23, 0.00, 7.00"
fmt_input_event
: formatting of InputEvent
with localization supportrand_dir2
rand_dir3
rand_sign
rand_bool
rand_vec2
rand_float_r
: a given radius (and optionally a center) generates a random float
randc
: random opaque colorrandca
: random color with random alphadelete_children
: delete all children of given Node
get_node_or_crash
: safer option to get_node
(which returns current Node
on empty NodePath
, thus frequently leading to bugs and strange behaviour)set_node_parent
: like add_child
with re-parenting supportget_nth_parent
draw_cross
, draw_ellipse
, draw_arrow
: mainly for use in tool scriptsAllows drawing of 2D debug shapes which will remain drawn for specific amount of time.
1# draw a red dot on global position 300 100 which stays rendered for two seconds
2GG.debug_draw_2d.point(Vector2(300, 100), Color.red)
Shapes: point
, line
, rect
, rect_wire
, string
, ellipse
, ellipse_wire
, arrow
.
See GGTestsDebugDraw/GGTestsDebugDraw.gd
for more examples.
Various utility functions, few examples:
take_screenshot
: take a screenshot and save it. convenient defaults - as path is used <user_data_of_your_project>/screenshots
and name is current date, e.g. 2019-12-19--13-20-35.png
create_timer_and_start
: creates and starts a Timer
node, connects your timeout handler (method of object). Also supports repeating mode. Useful when yield(get_tree().create_timer(1), "timeout")
is leading to Resumed after yield, but class instance is gone
errors.vec2
/vec3
: construct Vector2
/Vector3
, if only one argument is given then use it for all axesload_relative
: load a resource relative to a path (in filesystem) of a given node (typically a script)anim_get_progress
: get progress of current animation in AnimatedSprite
input_event_to_dict
and dict_to_input_event
: serialization and deserialization of InputEvent
s, useful for key binding support