Understanding how Utils works.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Plaz_GD

Hello everyone.

A week later of learning Godot. One of my friends gives me a utils script (utils.gd) for me to try to learn and understand. And so far, I haven’t fully 100% understand it. Here’s the code:

extends Node
var mouse_pos setget, _get_mouse_pos
var main_node setget, _get_main_node
var view_size setget, _get_view_size
func _ready():

func create_timer(wait_time):
   var timer = Timer.new()
   timer.connect("timeout", timer, "queue_free")
   return timer

func choose(choice):
   var rand_index = randi() % choice.size()
   return choice[rand_index]

func search_node(node):
   return self.main_node.find_node(node)

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)

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)

func _get_mouse_pos():
   return get_viewport().get_mouse_position()

func _get_main_node():
   var root = get_tree().get_root()
   return root.get_child( root.get_child_count() -1 )

func _get_view_size():
   return get_tree().get_root().get_visible_rect().size

I’m curious to know the usefulness of this utils. If someone could write a specific explanation in detail, that would be appreciated.

~ Regards

Also, I would like to thank Eldam for the answers in my previous topic question, regarding the mute button 10 days ago. Sorry, I forgot to be thankful for the help. ( Mute Button - How to pause the music in game background? - Archive - Godot Forum )

One of my friends gives me a utils script […] I haven’t fully 100% understand it

Just curious: Why don’t you simply ask your friend?

njamster | 2020-05-22 09:57

He often replied: “Try to learn it for yourself for this matter”. Maybe because I ask him too much, considering his always busy.

Plaz_GD | 2020-05-22 10:19

:bust_in_silhouette: Reply From: supper_raptor

1.) func create_timer(wait_time):

It creates and returns a timer and will destroy itself after wait_time

2.) func choose(choice):

This function returns a random element from an array.

  var foo = [1,2,4,5,6]
  var x = choose(foo) #x may be any element from foo

3.) func search_node(node):

It will search node and will return the node you searced for

4.) func attach(src_node, src_signal, trg_node, trg_func):

This function is used to connect signals between 2 nodes.
You can pass both reference or node name as src_node or trg_node.


attach($Timer,"Timeout",self,"on_timeout")  #on_timeout is a function

5.) func remote_call(src_node, method, arg0 = null, arg1 = null):

This is used to call method in src_node

#you have a node timer in ur scene and start it with wait_time 100

Function 3 is bit slow, dont use it all the time

supper_raptor | 2020-05-22 09:47

Why Function 3 “func search_node(node):” is considered “a bit slow” according to you?
If so, then is there a better function that is “much faster”?

Plaz_GD | 2020-05-22 10:22

its slow because search_node() does a recursive search and its better to use get_node if you know the node path of the node

supper_raptor | 2020-05-22 11:10

Interesting, thanks for sharing your answers regarding this.

Plaz_GD | 2020-05-22 12:31