Can't Spawn Bullet At Gun

Godot Version

4.3

Question

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 speed: float = 50
@export var direction: Vector2

func _ready():
	velocity = direction.normalized() * speed

func _process(_delta):
	move_and_slide(velocity)

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):
	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!")

The error is exactly what was shown to you. move_and_slide() does not need any arguement.

3 Likes

Thanks, I fixed that, but I still cannot shoot my bullet. I have mapped my mouse button control, but no errors are popping up anymore.

Try adding some debugging print statements in your code to see where it propagates and what’s the state of the game before and after you try to shoot.

2 Likes

Can you show us your node structure ?

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.

  • Check if the bullet spawned in the wrong place.
  • Check if the bullet is visible, such as a visible node.
  • Check if the bullet node exists.

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!")

Did you assign the bullet_scene in the editor? Can you show a screenshot of the Gun node?

1 Like


Your bullet_scene is empty, you need to fill it in.

1 Like

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'.

Which line exactly?

Line 25 →

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.

1 Like

Wait, I found the issue I picked the wrong packed_scene. Thanks, for your help!

3 Likes

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.

1 Like