Hello

I have a very simple Player scene that tries to instance a bullet node in-code and make it move horizontally. Here are the Player functions:

func _ready():
	shoot_bullet()
func shoot_bullet():
	var bullet = Bullet.instance()
	bullet.position = position
	bullet.set_direction(1)
	add_child(bullet)

And here is the bullet code.

func _process(delta):
	position.x += SPEED * delta * direction

While the bullet does instantiate (printing its position shows the correct coordinates), it somehow does not appear in the test window. I've set visible, show() etc. correctly so I'm unable to figure out why - can someone help?

  • DaveTheCoder replied to this.
  • I just found the bug - I had positioned the bullet towards the bottom right in it's own scene, so when instanced by the Player, it was being pushed even further out of the screen. I reset the Bullet local positions back to 0 and it's now visible. Rookie mistake, thanks everyone!

    I'm not good in script yet, but you declared no const (SPEED, delta, VECTOR), can be one problem.

    Not exactly a answer, but in the Asset Library Projects give a look in the 2D Bullet Shower, I think could help you with the script.

    zenbakery func _process(delta):
    position.x += SPEED * delta * direction

    One possibility is that it's moving too fast and immediately moves out of the view. What's the value of SPEED?

    Or maybe the bullet is moving too slowly and hidden inside of Player.

    I just found the bug - I had positioned the bullet towards the bottom right in it's own scene, so when instanced by the Player, it was being pushed even further out of the screen. I reset the Bullet local positions back to 0 and it's now visible. Rookie mistake, thanks everyone!