0 votes

Hello,

I am using an online tutorial to learn and have gotten to a spot that is baffling me. I have followed along and tried to make sure i am EXACT with the tutorial but for some reason the bullets are not appearing.
Below are my GD scripts, then the Eror log:

Bullet.GD

extends Area2D

export (int) var speed = 10

var direction := Vector2.ZERO

func _physics_process(delta: float) -> void:
    if direction != Vector2.ZERO:
        var velocity = direction * speed

        global_position += velocity


func set_direction(direction: Vector2):
    self.direction = direction

BulletManager.GD

extends Node2D


func handle_bullet_spawned(bullet, position, direction):
    add_child(bullet)
    bullet.global_position = position
    bullet.set_direction(direction)

Main.GD

extends Node2D

onready var bullet_manager = $BulletManager
onready var player = $BulletManager

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
    player.connect("player_fired_bullet", bullet_manager, "handle_bullet_spawned")

Player.GD

extends KinematicBody2D

signal player_fired_bullet(bullet, position, direction)



export (PackedScene) var Bullet
export (int) var speed = 100


onready var end_of_gun = $EndOfGun


func _ready() -> void:
    pass # Replace with function body.


func _process(delta: float) -> void:
    var movement_direction := Vector2.ZERO

    if Input.is_action_pressed("up"):
        movement_direction.y = -1
    if Input.is_action_pressed("down"):
        movement_direction.y = 1
    if Input.is_action_pressed("left"):
        movement_direction.x = -1
    if Input.is_action_pressed("right"):
        movement_direction.x = 1

    movement_direction = movement_direction.normalized()
    move_and_slide(movement_direction * speed)

    look_at(get_global_mouse_position())


func _unhandled_input(event: InputEvent) -> void:
    if event.is_action_released("shoot"):
        shoot()


func shoot():
    var bullet_instance = Bullet.instance()
    var target = get_global_mouse_position()
    var direction_to_mouse = end_of_gun.global_position.direction_to(target).normalized()
    print("sending")
    emit_signal("player_fired_bullet", bullet_instance, end_of_gun.global_position, direction_to_mouse)

Error Log:

E 0:00:01.225   connect: In Object of type 'Node2D': Attempt to connect nonexistent signal 'player_fired_bullet' to method 'Node2D.handle_bullet_spawned'.
  <C++ Error>   Condition "!signal_is_valid" is true. Returned: ERR_INVALID_PARAMETER
  <C++ Source>  core/object.cpp:1452 @ connect()
  <Stack Trace> Main.gd:11 @ _ready()

Any Ideas??

Thank you for your help

in Engine by (15 points)

1 Answer

0 votes
Best answer

Your error is here:

player.connect("player_fired_bullet", bullet_manager, "handle_bullet_spawned")

But you've written

onready var player = $BulletManager

And the "BulletManager" does not have that signal.

by (20,899 points)
selected by

Thank you,
However; I am still a bit confused?

am I missing a method to give the BulletManager the signal? This whole signal thing is new to me, so i am struggling a bit to wrap my head around it.

Your code as it's written is trying to connect a signal on the bullet manager to the bullet manager, because both bullet_manager and player are pointing at `BulletManager'.

Shouldn't player be looking at `$Player' ?

onready var player = $Player

"Player" has the signal. You want to connect the player's signal to the bullet manager, I assume. Now this:

player.connect("player_fired_bullet", bullet_manager, "handle_bullet_spawned")

makes sense, because it's saying "Connect the player's 'playerfiredbullet' signal to the bullet_manager".

OMG! Thank you!

Now that I see that it makes more sense.. I guess im not sure why he didnt show that fix in the tutorial..

Very much appreciated!

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.