JamKit helper methods - GalanCM/Godot-Jamkit GitHub Wiki

The JamKit object consists of helper functions that are useful for quick game jam tasks.

Installation

TODO

The Helpers

Unique Nodes

Unique Nodes are a parallel to Node Groups. Both are intended for accessing objects globally, outside of the parent-child relationship. They differ in the that Node Groups are a collection of one or more nodes, Unique Nodes must be, well, unique. Attempting to give two nodes the same name will raise an error.

Three methods are used to work with unique nodes:

  • set_unique_node(id, node, replace = false)
  • get_unique_node(id)
  • remove_unique_node(id, node)

Setting a unique node

To set a unique node use:

JamKit.set_unique_node("name", node)

On occasion, you may find it necessary to replace a unique node. Say, for example, that you need your player to possess various nodes. Calling set_unique_node() as above will result in an error. To prevent accidentally changing a unique node, you need to explicitly allow node replacement by setting the optional argument replace to true:

JamKit.set_unique_node("player", newPlayer, true)

Getting a unique node

Once set, getting a unique node is simple:

var unique_node = JamKit.get_unique_node("name")

If a unique node is unset, or the node has been freed, get_unique_node() will return null. To ensure that your game will continue to function if the requested node doesn't exist, it is recommended that you use the following pattern:

var unique_node = JamKit.get_unique_node("name")

if unique_node != null:
  # work with unique_node
else:
  # ignore, print error, and/or change behaviour as needed

Removing a unique node

If you want to remove a unique node, you can use the remove_unique_node() function:

JamKit.remove_unique_node("name", self) # will raise error if `self` is not currently the named unique node

JamKit will not remove references to a unique node when a node is freed. To do so, you can add the following method to a unique node:

...
func queue_free():
  JamKit.remove_unique_node("name", self) # will raise error if `self` is not currently the named unique node
  .queue_free()
...