how to get_tree from other scene?

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

i want to get node from Main.scene to script in block.scene

get_tree().get_parent().get_rect().size.x

the block.scene not in the main.scene beacuse block.scene will automatically show up

how to get_tree sprite in other scene ?
here is the screenshot

enter image description here

:bust_in_silhouette: Reply From: AlexTheRegent

This is bad concept design, because your components will be tightly coupled. Instead, you can pass reference to Main node to your block.gd when you instance this node, for example:

# block.gd
var main 

# main.gd
var block = Block.instance()
block.main = self

so instead of get_tree().get_parent() you will simply write main.

Or you can expose it as member of Singleton class (https://docs.godotengine.org/en/stable/getting_started/step_by_step/singletons_autoload.html)

# provider.gd
var main

# main.gd
func _init():
  Provider.main = self

then get_tree().get_parent() can be written as Provider.main.

i see… thank you for the answer… i’ll try it then

okkyhw | 2021-07-23 10:13