I can’t seem to figure out how to spawn the bullet and make it move. I get the error Too many arguments for 'move_and_slide()' call. Expected at most 0 but received 1. bullet.gd
extends CharacterBody2D
@export var player : Node2D
@export var distance_from_player : float = 100
@export var bullet_scene : PackedScene
func _ready():
position = player.position + Vector2(distance_from_player, 0)
func _process(_delta):
var direction = (get_global_mouse_position() - player.position).normalized()
position = player.position + direction * distance_from_player
look_at(get_global_mouse_position())
if Input.is_action_just_pressed("mouse_left"):
print("Mouse Click Detected!")
shoot_bullet(direction)
func shoot_bullet(direction: Vector2):
if bullet_scene:
print("Spawning Bullet!")
var bullet = bullet_scene.instantiate()
bullet.position = position
bullet.direction = direction
get_parent().add_child(bullet)
else:
print("Error: Bullet scene is not assigned!")
Check if the position formula is correct. the bullet may be correctly added to the node tree but you are just not able to see it. Try replacing the initial position with a dummy one within your view area. e.g. Vector2(300, 200) just to check if this is or not the problem.
I end up getting this, I think it’s something to do with how the bullet_scene is fetched.
Executing shoot_bullet()
<null>
Error: Bullet scene is not assigned!
gun.gd
extends CharacterBody2D
@export var player : Node2D
@export var distance_from_player : float = 100
@export var bullet_scene : PackedScene
func _ready():
position = player.position + Vector2(distance_from_player, 0)
func _process(_delta):
var direction = (get_global_mouse_position() - player.position).normalized()
position = player.position + direction * distance_from_player
look_at(get_global_mouse_position())
if Input.is_action_just_pressed("mouse_left"):
print("Mouse Click Detected!")
shoot_bullet(direction)
func shoot_bullet(direction: Vector2):
print("Executing shoot_bullet()")
print(bullet_scene)
if bullet_scene:
print("Spawning Bullet!")
var bullet = bullet_scene.instantiate()
bullet.position = Vector2(200, 200)
bullet.direction = direction
get_parent().add_child(bullet)
else:
print("Error: Bullet scene is not assigned!")
Thanks, but now I get the error Invalid assignment of property or key 'position' with value of type 'Vector2' on a base object of type 'null instance'.
extends CharacterBody2D
@export var player : Node2D
@export var distance_from_player : float = 100
@export var bullet_scene : PackedScene
func _ready():
position = player.position + Vector2(distance_from_player, 0)
func _process(_delta):
var direction = (get_global_mouse_position() - player.position).normalized()
position = player.position + direction * distance_from_player
look_at(get_global_mouse_position())
if Input.is_action_just_pressed("mouse_left"):
print("Mouse Click Detected!")
shoot_bullet(direction)
func shoot_bullet(direction: Vector2):
print("Executing shoot_bullet()")
print(bullet_scene)
if bullet_scene:
print("Spawning Bullet!")
var bullet = bullet_scene.instantiate()
bullet.position = Vector2(200, 200) # this one
bullet.direction = direction
get_parent().add_child(bullet)
else:
print("Error: Bullet scene is not assigned!")
I don’t see any reason why it would throw an error on this line. Try putting a breakpoint there and investigate what’s the state of the variables at that time.
Nice one. Always be cautious with assigning, since the code will not be able to manage an instance if it does not have a scene or class to instantiate.
You may already know what this means, but I will explain it anyways, just in case.
Invalid assignment of property or key 'position' with value of type 'Vector2' on a base object of type 'null instance'
Next time you get a msg like this, go word by word:
Invalid assignment of property the code is trying to assign a property (e.g. position, speed, etc…) but it is not able to do so.
'position' with value of type 'Vector2' the name of the property is position and it has a type of Vector2 which means a vector (as in mathematics) of two dimension (x,y)
on a base object of type 'null instance' the declared object which the code is trying to change the property does not have an instance. This is because the scene was not properly set-up and so the engine was not able to instantiate anything.