This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Hello!

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()
    get_parent().add_child(b)
    b.position=robotarm.position+Vector2(200,200)

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?

Godot version 3.3 Stable Official Win64
in Engine by (52 points)

1 Answer

0 votes

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.

by (2,159 points)

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... :-/

Ok, so these two lines:

`get_parent().add_child(b)
 b.position=robotarm.position+Vector2(200,200)`

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.