#How to better structure node targeting

1 messages · Page 1 of 1 (latest)

verbal matrix
#

I recently had to manually redo a bunch of my scripts because I changed how a scene was structured.

Many of my scripts refer to other nodes so I use a variable like this example
@onready var player = $"/root/game/combat/player_controller/player"
@onready var combat_manager= $"/root/game/combat/combat_manager"

Then later I might call player.position or something similar. Is there a way to stucture my code so I don't rely on this as much? It was really tedious to change a million different scripts.

I know signals can help, but only if the scene is instantiated in the editor, because otherwise you need to conntect the signal through targeting the node anyway.

keen owl
#

There are a couple things you can do, sometimes Scene Unique Nodes work
https://docs.godotengine.org/en/stable/tutorials/scripting/scene_unique_nodes.html
but for me I usually have an Autoload
https://docs.godotengine.org/en/stable/tutorials/scripting/singletons_autoload.html
I have that autoload store important objects. For example lets say my Autoload is called GameState. In GameState's script I have
var player : Node2D
then in the player's script I have

func _ready():
  GameState.player = self  
#

then every other object can access the player by saying GameState.player

verbal matrix
#

thats a great idea! thanks!

keen owl
#

you are welcome!