Manipulating variables in other scripts within same node tree?

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


i’m currently learning the ins and outs of godot (especially the script part) and i’ve come across a problem i don’t understand on a conceptual level and i hope to find the underlying problem and solution here.

i’m practising around with a checkers game and would like to manipulate variables from one script to another like this:
the tree looks like this with the Game Node being the root

  1. Game Node
  2. Board Sprite
  3. Control Node
  4. Button Node

The Game Node handles the setup process and is in control of the board and its functionality. Inside of it, there is a 2D array called board_2d that is 8x8.
Since i want to populate the board with the red and blue stones of the players, i want to update the board_2d array from another script with a function called set_stone_on_board(x, y, stone):
Because my button is so far down in the tree, i have to make the call like this (which is almost definitely not the way to go but i couldnt find anything better and using the full res:// patch didnt work for me):

get_node("..").get_node("..").get_node("..").set_stone_on_board(1, 1, stone)

unfortunately my board_2d array is empty upon calling and i dont quite understand why that is since i didnt change the scene and the game node is a parent node of the button

Could anyone bring light to my darkened mind?

Also any form of “it might be better to use/do this that way” is appreciated :slight_smile:

Thanks in advance

Choose your hack :b

  • You can access a node with absolute path: get_node('/root/scene_node/Game Node');
  • You can emit a signals;
  • You can use globals/singleton with a reference to the array;
  • You can pass the Game Node as reference;

Emerson MX | 2018-02-15 16:39