Skip to main content

Miscellaneous (misc)

This module contains many methods that don't fit into any other module and generally make your life as a developer easier.

Functions

void quit_safely(int exitcode = 0)

Awaiting required

Using the await keyword is required for this function.

Deprecated

This method has been deprecated and should no longer be used.

Moved to Core.quit_safely.

float byte2mib(int bytes, bool flatten = true)

Converts a number of bytes into mebibytes.

If flatten is set to true, the decimal part will be discarded.

float mib2byte(float mib, bool flatten = true)

Converts a number of mebibytes to bytes.

If flatten is set to true, the decimal part will be discarded.

float mib2gib(float mib, bool flatten = true)

Converts a number of mebibytes to gibibytes.

If flatten is set to true, the decimal part will be discarded.

float gib2mib(float gib, bool flatten = true)

Converts a number of gibibytes to mebibytes.

If flatten is set to true, the decimal part will be discarded.

String format_stringarray(Array[String] array, String item_before = "", String item_after = "", String separator_list = ", ", String separator_final = " & ")

Converts a string array into a normal, nicely formatted string.

With item_before and item_after you can customize the lists appearance. Here's an example on how to format every item bold:

extends Node

var core: Core = get_node("/root/CORE")
var misc: CoreBaseModule = core.misc
var logger: CoreLoggerInstance = core.logger.get_instance("some/script.gd")

func _ready() -> void:
var array: Array[String] = ["Apples", "Bananas", "Oranges"]

logger.info(misc.format_stringarray(array))
logger.info(misc.format_stringarray(array, "[b]", "[/b]"))

Array[String] array_to_stringarray(Array array)

Converts an array into a string array.

If an item is found that is not of type String, an empty array is returned.

Array stringarray_to_array(Array[String] array)

Converts a string array into an array.

Vector2 get_center(Vector2 parent_size, Vector2 child_size)

Calculates the center of the child in the area of the parent.

Example:

extends Control

var core: Core = get_node("/root/CORE")
var misc: CoreBaseModule = core.misc

func _ready() -> void:
position = misc.center_object(get_parent().size, size)

String stringify_variables(String template, Dictionary variables, bool no_quotes = false, bool force_no_type = false)

Makes variables as look correct inside strings.
Short examples:
true -> 'true'
Vector2(69.064, PI) -> 'x=69.064 y=3.14159265358979'
"This is a test string" -> '"This is a test string"'
Full example:

Code:
logger.diag(stringify_variables("Triggered %trigger% (pos=%position%, successful=%success%)", { "trigger": "shoot", "position": Vector2(5156.149, 581.69), "success": true }))

Output:
[16:44:35] [DIAG Test.gd] Triggered '"shoot"' (pos='x=5156.149 y=581.69', successful='true')