Skip to content

Instantly share code, notes, and snippets.

@EdwardAngeles
Last active December 14, 2020 22:22
Show Gist options
  • Save EdwardAngeles/8d19427b0ccc69a0a2bef6f8ee556302 to your computer and use it in GitHub Desktop.
Save EdwardAngeles/8d19427b0ccc69a0a2bef6f8ee556302 to your computer and use it in GitHub Desktop.
A set of utility function for Godot Engine.
extends Node
var main_node setget , _get_main_node
var view_size setget , _get_view_size
var mouse_pos setget , _get_mouse_pos
func _ready():
pass
func create_timer(wait_time):
var timer = Timer.new()
timer.set_wait_time(wait_time)
timer.set_one_shot(true)
timer.connect("timeout", timer, "queue_free")
add_child(timer)
timer.start()
return timer
pass
func choose(choises):
randomize()
var rand_index = randi() % choises.size()
return choises[rand_index]
pass
func search_node(node):
return self.main_node.find_node(node)
pass
func attach(src_node, src_signal, trg_node, trg_func):
if typeof(src_node) == TYPE_STRING:
src_node = search_node(src_node)
if typeof(trg_node) == TYPE_STRING:
trg_node = search_node(trg_node)
if src_node != null and trg_node != null:
src_node.connect(src_signal, trg_node, trg_func)
pass
func remote_call(src_node, method, arg0 = null, arg1 = null):
src_node = search_node(src_node)
if src_node and src_node.has_method(method):
if arg0 and arg1:
return src_node.call(method, arg0, arg1)
if arg0:
return src_node.call(method, arg0)
return src_node.call(method)
pass
func mk_vector(length, angle_radians):
var vector = Vector2()
vector.x = cos(angle_radians) * length
vector.y = sin(angle_radians) * length
return vector
pass
func _get_mouse_pos():
return get_viewport().get_mouse_position()
pass
func _get_main_node():
var root = get_tree().get_root()
return root.get_child( root.get_child_count()-1 )
pass
func _get_view_size():
return get_tree().get_root().get_visible_rect().size
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment