Hi!
I am trying to instantiate a Bullet Node in another Scene. I want this because I want to separate the Bullet Scene from any other Scene and only instantiate it in a Scene when a certain Signal occurs. I tried to follow this example:
https://docs.godotengine.org/en/stable/tutorials/misc/instancing_with_signals.html
Unfortunately, it is not possible to use the Node tab to connect Signals because the Scene which emits the Signal does not see the other Scene where it should connect to.
I read that I can do it programmatically, something like this: bullet.connect("shoot", self, "onPlayer_shoot")
But this does not work for me somehow.
This is my Bullet Script from one Scene:
extends Node2D
signal shoot(bullet, direction, location)
onready var bullet = load("res://Scenes/Bullet.tscn")
func _input(event):
if event is InputEventMouseButton:
if event.button_index == BUTTON_RIGHT and event.pressed:
emit_signal("shoot", bullet, rotation, position)
func _process(delta):
look_at(get_global_mouse_position())
I want to connect it to a Player Node in another Scene, I tried something like this
in the Player.gd script which doesn't work
onready var bullet = preload("res://Scripts/Battle/Bullet.gd")
func _ready():
bullet.connect("shoot", self, "_on_Player_shoot")
func _on_Player_shoot(bullet, direction, location):
var b = bullet.instance()
print("shoot!")
add_child(b)
b.rotation = direction
b.position = position
b.velocity = b.velocity.rotated(direction)
What do I need to do here to make this work? Is there a better way than doing it programmatically like this?