Is there a way of calling functions through dirrent scenes?

The Scoreboard tree scene:
image
Code: extends Control
@onready var label = $CanvasLayer/Label

func Add_Points():
global.Score += 1
label.text = "Score: " + str(global.Score)

Coin tree scene:
image
Script: extends Area2D

@onready var game_managar = %GameManagar
@onready var animation_player = $AnimationPlayer
@onready var score_board = $“.”

func _on_body_entered(body):
game_managar.add_point()
score_board.Add_Points()
animation_player.play(“Pickup”)


i see you link game manager node here, i guess your coin is instantiated and add_child from editor in a main scene?
your main script should know where the score_board scene node is, and when coin is collected, you just send signal telling a coin is collected to Main Script, so the Main Script can tell score_board to update the label points

What do you mean with instantiated?

Game manager is just a node in the main scene with a label that displays the coin number

but I also wanna show the coin number on a UI that is made in another scene

Scoreboard is probably best used through an Autoload, otherwise if your coins are placed manually they would each need to be connected or given a reference to the scoreboard. If they are made dynamically/spawned in that is less of an issue.

Oh an the unique identifier only works for nodes within the scene of said script, so %GameManagar will not give you a game manager since it does not exist in coin. Probably autoload that as well

do you have a main script here? it doesnt look like the game manager is the main script now, the Main Script should hold all the essential node in a variable or two, then use the variable to tell the node to do something when receiving signals to do something

the variable score is a global one

No I don’t have one

It’s just a node that holds a label that display how many coins are being collected

so your main scene is now the game manager? and you created score_board inside game_manager?

hold up I can show the main scene

here
image

yes, the main scene is slacking right now, it’s responsible to transfer information to one or another, not just a main scene for a name

so global is an autoloaded script? Try using Scoreboard instead, it can have the same variables, score is more relevant to the scoreboard anyways.

scoreboard is the UI that should display how many coins have beem collected. How can I autoload that and have variables in it?

extends Area2D

@onready var game_managar = %GameManagar
@onready var animation_player = $AnimationPlayer
@onready var score_board = $"../../ScoreBoard"

func _on_body_entered(body):
game_managar.add_point()
score_board.Add_Points()
animation_player.play(“Pickup”)

then just this will do

Autoloaded .tscn files will also allow access to it’s scripted variables and functions. Assuming the script is on the root node, which it is for your scoreboard.

Worked great! Thanks. When I created the variable I just dropped it into the script using ctrl cause I thought it would do it auto

1 Like

Thanks for the help!

When naming variabels do you go through the diffrent trees?