code that can affect other objects

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

Hello, so I’m new to Godot and I wanted to know if there is a way to use one code to modify multiple objects, for example, I’ve seen someone use node2d to write a teleportation code and use it for other objects but I can’t find the video.

I thought about creating a function, but you can’t include other functions inside it, so if I wanted to use area2ds or signals, that also wouldn’t work. Any idea how to help me?

:bust_in_silhouette: Reply From: USBashka

Yes, It’s really easy: Say you have a script attached to the Node1 and Node2 as it’s child. To access Node2 you need to use $ or get_node(path) and relative path to the node. Like this:

onready var node2 = get_node("Node2")

Onready vars putted in the beginning of script in variables section.

After that you use node2 var to access the node:

func _process(delta):
     if node2.position.x > 200:
         node2.position.x = 0
     elif node2.posotion.x < 0:
         node2.position.x = 200

For examle.

thank you so much! I didn’t realize it was that easy

C12rismaticBunny | 2022-07-28 13:15