Problem creating shots

LaMoscaSASLaMoscaSAS Posts: 1Member

Hello, I'm new to godot and programming. Problem is, I can not get my ship to fire. I have created a sprite-type node named "sprnaveshoots" that initialized in graph 2d with the parameter "visible" disabled. Then, in the parent script, create an empty array. When left-clicking the mouse, I have told godot that, this value is equal to the sprite "sprnaveshoots" with the difference that it will have the "visible" property activated. Once the sprite is stored inside the array, it will proceed to move the sprite, but when I try to get the position of the sprite and modify it, godot retorn me following error message: "Invalid get index '[Sprite: 563]' (Based on 'Array'). ".

These is my code:
extends Node2D

    onready var nave = get_node("spr_nave")
    onready var nave_prt = get_node("prt_nave")
    onready var nave_shoot = get_node("spr_nave_shoot")
    onready var nave_shoots = []

    func _ready():

    func _process(delta):

    func move_shoot(shoot,delta):
        var sh = shoot
        var sh_p = shoot.get_pos()

    func mover_disparos_nave(delta):
        for i in nave_shoots:

    func disparar(delta):
        #var pos = nave_shoot.get_pos()
        #pos.y -=200*delta
        var disparo = nave_shoot
        var dis_pos = disparo.get_pos()
        dis_pos = nave.get_pos()

    func action_nave(delta):



  • RossRoss Posts: 199Member
    edited November 2016

    The error is because you're using the for loop wrong. When you loop over an array, i is the array element, not the index. So in this case i will be the node. But on line 20 you're trying to use it as the index.

  • RossRoss Posts: 199Member

    Personally I find it way easier to make the bullet it's own scene. Then when you want to fire, just create a new instance of the bullet scene, put it at the muzzle position of your gun, give it a velocity, and let it handle it's own behavior.

    Actually, it's almost required to do it that way if your bullets have any effects or behavior of their own. Animations, particles, physics simulation, etc.

  • woopdeedoowoopdeedoo Posts: 39Member
    edited January 1

    I agree with Ross. It's easier and advantageous to just make the bullet an entity with "life of its own" and just set it free when needed. It's been a while since this post was made, but just in case it may still help, take a look at this video:

    (The video is in portuguese, but since some words in your code are in portuguese, I suppose you speak it)

Sign In or Register to comment.