Is there a way of calling functions through dirrent scenes?

Godot Version

Question

Does anyone know if there is a way to call a function event tho the function itself is on a diffrent scene? Or should I create a global variable and then make a function always look out for it changing value and then call the function? Sorry if it got a bit confusing.

use EventBus signaling
https://gdscript.com/solutions/signals-godot/#:~:text=the%20script%20file.-,Global%20Event%20Bus,-In%20GoDot%20we

or if you have the instantiated+add_child scene’s reference, you can just .method_you_want_to_call() from other scene

or really you can pass around the Callable as a Variable and .call() Callable — Godot Engine (stable) documentation in English

or register/made a group for that node, and call the group node method with get_tree().call_group() Groups — Godot Engine (stable) documentation in English

Hey! I have read the documentation but because I am new to programming I didn’t understand it to a 100%. Could you do a more easy explanation?

Just started out with Godot a few days ago.

hi, aight we back to question:

so what you are here trying to do is to want to call a function from, let’s say, A script from B script.
B script needs to know A first in order to call the function it has. the most simple explanation
how B can know A?
you can pass A as a Variable to B script, then B Script just call the method

or

if you know A Address node, then you can meet A from the Address Path

image
example this
Player Node can know GUI node simply by getting the GUI node path right
which is
…/…/…/GUILayer/GUI

Alright. So how do I do if I want to create a variable for script A?

But that is because they are on the same scene?

yes, i instantiated and add_child the scenes first for this main Core scene

get the node that is holding/attaching the A Script, then just

a_node.the_variable=10

but the_variable should already exists first in the A Script, else it will tell there’s no the_variable property/value in A Script

it’s back again how B Script can get A Script’s Node:
the ways i mentioned were

  • by routing the address path in a Scene tree from B to A
  • pass the A node self reference to B script variable so he know

Wait, wait. What do you mean with, a_node.the_variable=10?

since you dont really use a standalone script unless it’s intentionally a RefCounted type called and created like an Object with reference, script normally attached to a node. by that, you can just call the property/method as long as you have the Node reference

let’s say A Script is attached to Player Node
B script is attached to Enemy Node
If B want to alter something in A Script, can simply route the address of Player in a SceneTree and get Player node, then just player_node.do_something()

I still dont get it. Might just be me that has to learn more lol.

well, this is the most direct way to call function from other scene
usually when things has parent node etc, you will most likely use signal to tell the main script to do something to other script node

I can send a picture of how my scripts are set up

1 Like

So this is Script B:


Where I call the function

Script A, where the function is:


label has already been made as a variable

And then the trees are in diffrent scenes

Could you post the scene tree? And you can paste scripts after three ticks like so

```
func my_script():
    pass
```

1 Like

Whole script B:

i assume this Add_Points Script A is in the score_board scene
how do you spawn this score_board scene and Script B scene in your SceneTree?
also what type is score_board scene node? is it even a script that’s attached to a node?