0 votes

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.

in Engine by (41 points)

1 Answer

0 votes
Best answer

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)
by (3,225 points)
selected by

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

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]

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?

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:

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

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

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

You can either use addtogroup 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

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.