0 votes

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():
   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(choice):
   randomize()
   var rand_index = randi() % choice.size()
   return choice[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 _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

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. ( https://godotengine.org/qa/70186/mute-button-how-to-pause-the-music-in-game-background )

in Engine by (22 points)

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?

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

1 Answer

0 votes
Best answer

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.
eg

  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 srcnode or trgnode.

eg

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

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

This is used to call method in src_node

eg
#you have a node timer in ur scene and start it with waittime 100
remote
call("Timer","start",100)

by (731 points)
selected by

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

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"?

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

Interesting, thanks for sharing your answers regarding this.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.