Why the won't my "bullet" shoot?

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

Basically, my “bullet” is just not spawning at my marker no matter what i do. its really annoying and i don’t see anything wrong with my code. i’ve tried almost anything. if you can help, i cant thank you enough. here is the code i have right now.

var BULLET = preload("res://Stock.tscn")

func shoot():
	if Input.is_action_just_pressed("Click1"):
		var bullet =  BULLET.instance
		get_node("Node2D/Marker2D").add_child(bullet)
		bullet.global_position = $Node2D/Marker2D.global_position

What if instead of

bullet.global_position = $Node2D/Marker2D.global_position

you just did

    bullet.position = Vector2(0,0)

Since the bullet is added as a child of Node2D/Marker, then the global position of the bullet will be right on top of the marker, and changing its position might make it spawn in non-desired locations

godot_dev_ | 2023-05-05 19:41

doesn’t seem to work. thank you though.

SevenNotes | 2023-05-05 19:47

Maybe you are missing brackets on

var bullet =  BULLET.instance

what if you do

var bullet =  BULLET.instance()

godot_dev_ | 2023-05-05 19:52

Nah man, still doesn’t work. I have no idea whats happening. I’m very sorry.

SevenNotes | 2023-05-05 19:53

You could also try adding a Sprite child node to the Node2D/Marker2D and setting a texture to that sprite. The goal here is to confirm you can see the sprite when you run the game and instance your scene. If you can’t see your sprite, the bullets may in fact be correctly spawning but your camera is misaligned

godot_dev_ | 2023-05-05 19:55

I have already done that, the marker is where its supposed to be alright.

SevenNotes | 2023-05-05 19:56

Could the bullet be invisible for some reason or be transparent?
Also, confirm that you bullet creation code is running by adding a print statement or break point to make sure the user input is correctly spawning a bullet (under the if statement in your code above)

godot_dev_ | 2023-05-05 20:03

its not invisible because its in my test scene next to me, i cant spawn it though. also the animation and print doesn’t happen. weird huh?

SevenNotes | 2023-05-05 20:06

If the below code doesn’t print “Hello World!”, then you have an issue with the configuration of the input mapping of your project. Whatever button/key you are trying to use to spawn a bullet hasn’t been correctly mapped to “Click1”.

var BULLET = preload("res://Stock.tscn")

func _process(delta):
    if Input.is_action_just_pressed("Click1"):
        print("Hello World!")
        var bullet =  BULLET.instance
        get_node("Node2D/Marker2D").add_child(bullet)
        bullet.global_position = $Node2D/Marker2D.global_position

godot_dev_ | 2023-05-05 20:12

yea, i tried tinkering with that. i even tried the built in ones. also sorry for not getting back to you for a while, i was eatin lunch.

SevenNotes | 2023-05-05 20:42

If the print statement isn’t printing, make sure to properly configure your input mapping (see this tutorial), and make sure your script with the code I posted above has set_process(true) in the _ready() function to make sure the _process function is being called.

godot_dev_ | 2023-05-05 20:50

hey man, before i say this thanks for all your help. but literally nothing is working for me right now.

SevenNotes | 2023-05-05 21:12

I can’t help you further. I believe the issue is poorly configured input mapping (assuming the print doesn’t occur), so it’s up to you to familiarize yourself with getting input from the user and processing it to fix your bug

godot_dev_ | 2023-05-05 21:37