Game doesn't show bullet sprite

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Tomi


I have a big problem: when I reset shooting from a Node to another Node, since then Godot doesn’t show bullet’s sprite. Totally invisible but still exists, because I can shoot down enemies.
The sprite properties: Scale X and Y: 1 and 1, and Visible=True.
Here is the code when I create it:

func _process(delta):  
 if can_shoot==true:
    var bullet=preload("res://robotbullet.tscn")
    var b=bullet.instance()

The b.position may be any coordinate, but the sprite stay invisible.
Has anyone ever met problem like this? Maybe I forgot something in that code?

:bust_in_silhouette: Reply From: DaddyMonster

This shouldn’t be in _process() - that runs every frame so you’re potentially making millions of bullets and offsetting them from robotarm by a fixed distance.

Unless, that is, ‘can_shoot’ turns false when the bullet spawns but there’s no can_shoot = false within this branch. Anyway, that’s not a great way of doing it tbh. It’s an unnecessary evaluation every frame at the very least. And you’re even preloading the scene repeatedly, just make that a member variable (at the top) and do it once.

Change func _process to func spawn_bullet(): and only call it once when you need a bullet. Then add a _process in the bullet to handle the movement.

Of course, it could be the material or the parent but I’d tidy up the code first. Hope that helps.

Thanks, DaddyMonster! I have replaced the code into a separated function.
But the bullets still comes from wrong places: outside of the scene into the scene… :-/

Tomi | 2021-06-17 08:33

Ok, so these two lines:


Ok, is the parent you’re adding it to the World scene? If the parent is the robot or anything else the bullet’s position will be relative to the parent (this is because the basis matrices of children are multiplied with the parent’s) Anyway, it should be a child to the scene is the point.

My guess is that you’re childing it to something else and then offsetting that by the robot arm position and by then a further 200x200… Print b.position when you spawn the bullet and also check in Remote that it is a child of the World. If it’s spawning in the right place check the script that moves it. Then check the basis matrix is identity, etc, etc.

DaddyMonster | 2021-06-17 13:44