Skip to content

Instantly share code, notes, and snippets.

@BrastenXBL
Created July 5, 2024 00:50
Show Gist options
  • Save BrastenXBL/d266625cd9832972656f8de8fc0c83ee to your computer and use it in GitHub Desktop.
Save BrastenXBL/d266625cd9832972656f8de8fc0c83ee to your computer and use it in GitHub Desktop.
An Autoload hack for Godot Android editor to pre-test for NULL crashes.
class_name GodotAndroidEditorRuntimeErrorDebugger
extends Node
## Singleton(Autoload) containing global helper methods for the Android Editor.
##
## This class is intended to be used as a Singleton(Autoload), the [code]gdaererrd[/code] plug-in will
## automatically add [code]aerr[/code] as an Autoload. The Singleton name and method names are
## intended for easy typing on QWERTY digital keyboards found on phones and tablets.[br]
## [br]
## Use [code]aerr.t(arg)[/code] to test if a single variable is_instance_valid().[br]
## Failure will cause the program to print the error to console Output.
## Then safely quit back to the Editor.
signal crashed
func _ready():
crashed.connect(_crash_clean)
## Android Error Test[br]
##
## Use [code]if aerr.t(arg):[/code] to test a variable
## before it is used later in the function.
## e.g. [codeblock]
## if aerr.t(player_node):
## player_node.position.x += 1
## pass
## [/codeblock]
## [codeblock]
## if aerr.t(player_node): player_node.position.x += 1
## [/codeblock]
## e.g. [codeblock]
## aerr.t(player_node)
## aerr.t(player_node.position)
## player_node.position.x += 1
## [/codeblock]
func t(arg) -> bool:
if is_instance_valid(arg):
return true
else:
_print_crash()
crashed.emit()
return false
## Alternate method name for [code]t()[/code]
func test(arg) -> bool:
return t(arg)
func _print_crash():
printerr("CRASH: VAR is not valid. Is it NULL?")
print_rich("[color=yellow]If it was a Node, check your NodePath $\" \"get_node()[/color].")
print_stack()
func _crash_clean():
Engine.get_main_loop().set_pause(true)
await Engine.get_main_loop().create_timer(0.01).timeout
Engine.get_main_loop().set_auto_accept_quit(false)
Engine.get_main_loop().call_deferred("quit")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment