Access a function from another scene

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

I have a keys scene from which I created inherited scenes and I have an instance of each one in a keyboard scene.
I have a script attached in keys scene with a connection to on_button_pressed signal, which works for every key(textureButton) I press.
I also have a board scene and a script attached to it. Both keyboard and board scenes are instanced in Main scene but not the keys scene.
Now, I want to access a function of board scene’s script from the keys scene’ s script in on_button_pressed function but I don’t know how to do it because only the inherited key scenes are instanced. I tried to preload the keys scene in board scene’ s script, and connect a signal but it doesn’t call the function and doesn’t throw any error.
board scene:

onready var key_scene = preload("res://keys.tscn")

func _ready():
    var keys = key_scene.instance()
	keys.connect("place_char", self, "detect_block")

keys scene:

signal place_char

func _on_keys_pressed:
:bust_in_silhouette: Reply From: ramazan

onready var key_scene = preload(“res://keys.tscn”)
var keys = null
func _ready():
keys = key_scene.instance()

func _on_keys_pressed:

Can you please add some comments about the code you posted?
In which script I need to put it?
The script that I have the fanction which I want to run is the board script.
The on_keys_pressed() function is in keys script.
I suppose the code goes to board script, but then, first var keys need to declared outside the on_ready and what is the onkeyspressed function? Do you mean the function which is in keys script? Then I get error that keys isn’t declare in the current script. If no, then this is another onkeyspressed function which is in board script? If yes how this function triggers? I tried it and it doesn’t work.

dancaer69 | 2022-01-16 09:38

onready var key_scene = preload(“res://keys.tscn”)
var keys = null
func _ready():
keys = key_scene.instance()

#Note = on key pressed(): your pressed sinyal
func -on-keys-pressed():
keys.func name that should work. Write here the name of the func that should work in
this scene.
don’t forget to add the “()” of the end
I hope I explained

If not, let me explain in more detail.

ramazan | 2022-01-16 09:59

I’m not sure you understood what I want to accomplisse, but this doesn’t work as I wrote.
What I want is:
keys script:

func _on_keys_pressed() -> void:
	var temp = self.get_child(0).text
	Utils.current_char = temp
    detect_block() -> *this is the function from board script I   want to add*

From my understanding you proposed this:
board script:

var keys
onready var key_scene = preload("res://keys.tscn")

func _ready() -> void:
    keys = key_scene.instance()

func _on_keys_pressed():

The above doesn’t work. The actions which are on keys script for pressed button work but the “keys.detect_block()” function from board doesn’t trigger and doesn’t give any error. Just ignored.

dancaer69 | 2022-01-16 10:33

Add a Button to the scene here and
var keys
onready var key_scene = preload(“res://keys.tscn”)

func _ready() → void:
keys = key_scene.instance()

func _on_keys_pressed():

func _on_Button_pressed():

ramazan | 2022-01-16 10:38

Why to do that. I already had the 26 buttons I want to use which are inherited scenes of the keys.scene. The script of this scene(keys script) contains the onpressed function and detects which of the 26 buttons is pressed and run the code for this button.

dancaer69 | 2022-01-16 10:51

You want to run the Function in another scene, right? To the scene you sampled
keys = key_scene.instance()

If not, sorry I misunderstood the question.

ramazan | 2022-01-16 10:59

The function is in board scene’ s script.
The onpressed function is in keys scene’ s script.
Yes, I want to run the function of board scene script in another scene(keys scene).
I have a main scene in which there are instances of board and all keys inherited scenes but not the original keys scene itself. So I don’t know how to get access to the original keys scene and to use a signal and preload this scene doesn’t work as I wrote on my first post.

dancaer69 | 2022-01-16 11:10