0 votes

Hello everyone.
I have followed a tutorial of Godot.
Top down Tank Battle Series:

However, it seems that the version i have and the version described in said tutorial is not the same.
I think i'm working in 3.1 (is there any manipulation to check that?) while the tutorial is in 3.0.

My probleme are the followings, both in the 10th part of the tutorial :

-First, when i modified the code to include a tracking bullet, i got the following prolem:
Invalid get index "position" (on base: previously freed instance'). It happen when my tank is destroyed.

extends Area2D

export (int) var speed
export (int) var damage
export (float) var lifetime

var velocity = Vector2()

export (float) var steer_force = 0
var acceleration = Vector2()
var target = null

func start(_position, _direction, _target=null):
    position = _position
    rotation = _direction.angle()
    velocity = _direction * speed
    target = _target

func seek():
    var desired = (target.position - position).normalized() * speed
    var steer = (desired - velocity).normalized() * steer_force
    return steer

func _process(delta):
    if target!=null:
        acceleration += seek()
        velocity += acceleration * delta
        velocity = velocity.clamped(speed)
        rotation = velocity.angle()
        lifetime = 0
    position += velocity * delta

func explode():
    velocity = Vector2()

func _on_Explosion_animation_finished():

func _on_Bullet_body_entered(body):
    if body.has_method('take_damage'):

func _on_Lifetime_timeout():

So, from what i understood, in the function seek() (Line19), the part target.position try to access the position of the target. The problem is when the target is destroyed (freed).
Following the tutorial i added a condition to prevent this case. if target, (Line 25), that i modified to if tager != null. The problem persist as if the condition was not revelant.

What i though is that in the function start, we do a copy of the target (Line 17), that we then use. But in this case, the bullet should simply track the position of the tank at the moment it was fired.
So target = _target gives apparently a reference to my bullet.

In this case, why my condition if target != null does not prevent bugs?

-Second, I tried to realise the trail described in the tutorial. But i don't manage to do the same way it was done in the tutorial. Even after i downloaded the project from the tutorial, it didn't work the way intended. I tried to use a changing texture, but the texture change at the same moment for every particle. Is there a way to add a delay starting from the creation of the said particle?
I also tried to make a flow, but the problem is that it's goes straight out of the missile, even when it turn, giving an artificial feeling.

Thank in advance.

in Engine by (16 points)
edited by

Thank you, volzhs, for putting the code in red. Can you tell me how you do that?
Thank in advance.

Please log in or register to answer this question.

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 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 webmaster@godotengine.org with your username.