0 votes

I have a weapon scene which launches a projectile scene, but retains a reference to that instanced node, so the second time the player hits "fire", the launched projectile explodes into smaller projectiles.

When I scale up the number of NPCs shooting these projectiles, I pretty consistently (but not always) get a strange issue where that variable in the "weapon" that points to the last_shot projectile is pointing at a different projectile node of a different scene type that shares the same base class of Projectile.

Here is the "weapon" scene code, with the variable in question being "lastshot", and the exception being an error because the "lastshot" node does not have the "explode" method (because it has somehow changed to point at a different projectile.

extends Weapon

var energy_use = 35
var fire_delay = 0
var fire_delay_time = 0.75
var projectile_type = preload("res://Weapons/Projectiles/BlasterBeam.tscn")
var last_shot
onready var shoot_effect = $ShootAudio
onready var explode_effect = $ExplodeAudio

func _fire(delta,energy_available):
    var energy = 0

    if last_shot != null and is_instance_valid(last_shot) and last_shot.traveled > 30:
        last_shot = null
        if fire_delay <= 0 and energy_available >= energy_use:
            var projectile = projectile_type.instance()
            last_shot = projectile
            fire_delay = fire_delay_time
            energy = energy_use
            fire_delay -= delta
    return energy

In terms of inheritance, i do have an inherited "spawn" method from Weapon that different weapons use to place / position / add_child the new projectile. The projectiles also inherit from a common base that extends Area.

I don't refer to or access the "last_shot" variable anywhere outside of this script or scene.

Any help or ideas on how to debug would be greatly appreciated! If more code is needed to fully understand, please also indicate! Thanks!

asked May 15 in Engine by Nikolaj Baer (15 points)

1 Answer

+1 vote
Best answer

Sounds like you could be experiencing this?


answered May 15 by jgodfrey (5,490 points)
selected May 15 by Nikolaj Baer

oh awesome, thank you, this looks like exactly that.

For reference, the approach i used to resolve it was to add after spawn:

last_shot.connect("tree_exited", self, "set",["last_shot",null])
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.