How do i send values between very far nodes?

Godot Version

4

Question

UI General Programming
I’m new to game making and i’m having a problem with passing values between far away nodes.

I wanted to make a button in the pause menu that swapped my skins

The skin of the banana is inside the sprite of the banana itself, it’s a very long texture that changes frames and makes different skins through a stopped animation. Inside the main “Banana” characterBody2D of this scene, there is a variable called skin that I need to modify.

image

In the main scene, the banana is connected to the player node (which contains other things that move along with the player) and to the main game scene, which is connected to the pause menu

image

The pause menu, is an abomination of control nodes, making the path to reach the button in question

PauseMenu → MarginContainer → VBoxContainer → Change Skin

image

I wanted a button to make the skin of my banana change every time I clicked it, so I made this code.

//INSIDE THE PAUSE MENU SCRIPT
func _on_change_skin_button_up() -> void:
  var skinFileLoc := "user://savedata.save"
  var banana = get_parent().get_parent().get_parent().get_node("Player").get_node("Banana")
  var skinFile = FileAccess.open(skinFileLoc, FileAccess.WRITE)
  skinFile.store_32(banana.skin + 1)
  banana.skinRefresh()
  skinFile.close()
//INSIDE THE BANANA SCRIPT
var skin: int = 0 #0 = default skin (use frames)
var skinFileLoc := "user://savedata.save"

func _ready() -> void:	
	height = 0
	music.stream = load("res://ost/menuMusic.wav")
	music.play()
	skinRefresh()

func skinRefresh() -> void:
	var skinFile = FileAccess.open(skinFileLoc, FileAccess.READ)
	skin = int(skinFile.get_as_text())
	$Sprite.frame = skin
	skinFile.close();

What i wanted to make the code do was:
1. Read the current equipped skin
2. Update the skin by going +1 in the list of skins
3. Make the skin refresh

I’m having difficulties with step 1, the rest of the program seems to work for now
The problem here is that whenever I try clicking the button, it tells me cannot call method ‘get_node’ on a null value. I just need to move this integer value inside the banana, navigating through 3 scenes in total (PauseMenu → Main scene → Banana).

Is that even possible or am I doing something wrong?

Thanks in advance

the PauseMenu’Parent is Game, your right code is:
get_parent().get_node(“Player/Banana”)

1 Like

I would recommend implementing the EventBus pattern for this kind of problems. It allows you to easily send messages between nodes regardless of their position in the tree. It is fairly easy, you can check how to do it here (The Events bus singleton · GDQuest)

With the event bus, you could do something like this:

  1. Player presses the button
  2. The button sends a signal through the EventBus
  3. The Banana scene (or any other interested scene) receives the signal and does whatever is needed to be done.
2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.