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.
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
The pause menu, is an abomination of control nodes, making the path to reach the button in question
PauseMenu → MarginContainer → VBoxContainer → Change Skin
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