how to pass parameters from one object to another object

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

I have a scene location. I have two scene objects in this location. How can I pass a value from object 1 to object 2?
I read the manual about signals, but I didn’t understand the meaning.

:bust_in_silhouette: Reply From: Magso

The easiest way to pass values is by variables
Object1

var value
func _ready():
    object2.value = value

Object2

var value
func _ready():
    yield(get_tree(),"idle_frame") #wait one frame
    print(value)

Here’s by parameters
Object1

var value
func _ready():
    object2.custom_function(value)

Object2

func custom_function(value_parameter):
    print(value_parameter)

And by signals (signals shouldn’t be used like this)
Object1

var value
signal custom_signal(value_to_pass)
func _ready():
    emit_signal("custom_signal", value)

Object2

func _ready():
    object1.connect("custom_signal", self, "the_custom_signal", [value_to_pass])
func the_custom_signal(value_to_pass):
    print(value_to_pass)

but, how to check if there is an instance of an object in the scene?

ateff | 2020-12-12 07:16

If you mean referencing the nodes you either use get_node("path/to/node") , $path/to/node which is the same thing, get_node_or_null("path/to/node") doesn’t error if the node doesn’t exist and finally using groups get_tree().get_nodes_in_group("object group")[0]

Magso | 2020-12-12 10:38

No. I mean, there can be multiple instances of an object on the scene.
How to get a list, for example, of all instances of object “a” in the scene?

ateff | 2020-12-13 06:28

Add a group name to that scene in the node tab and in the scene where there are multiple instances check if get_tree().get_nodes_in_group("your group").size() > 0:

Magso | 2020-12-13 10:07

Ок. Но как это сделать? Что вообще такое “группа”?

ateff | 2020-12-13 15:09

A group is an efficient way to reference nodes in bulk, other game engines call it tagging so you can give objects a tag such as all enemies can be tagged/grouped “enemy”.

Magso | 2020-12-13 18:02

Ok, figured out,
Спасибо)

ateff | 2020-12-14 08:42

You can either use add_to_group in gdscript or add a group name to that scene/node in the node tab as I mentioned above.

Edit: didn’t see your edit

Magso | 2020-12-14 09:14