0 votes

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?

asked Jun 1 in Engine by BennieBe (24 points)

1 Answer

0 votes

Connect the signal after you instance the bullet

edit: also I think you want to instance bullet.tscn instead of .gd

answered Jun 1 by RazorSh4rk (256 points)

I am not sure how this works? Would you mind providing an example?
I tried out different ways, I am not able to get it to work ...

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.