Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | randor73 |
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