0 votes

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 onbuttonpressed 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 onbuttonpressed 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:
    emit_signal("place_char")
Godot version 3.4.2
in Engine by (221 points)
retagged by

1 Answer

0 votes

onready var keyscene = preload("res://keys.tscn")
var keys = null
func _ready():
keys = key
scene.instance()

func onkeyspressed:
keys.function
nametorun()

by (650 points)
edited by

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 onkeyspressed() 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.

onready var keyscene = 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.

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
    print(temp)
    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():
    keys.detect_block()

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.

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 onkeyspressed():
keys.detect
block()

Try
func onButtonpressed():
_on
keys_pressed()
pass

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.

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

If not, sorry I misunderstood the question.

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.