0 votes

I've made a golem spawn with this script:

extends Area2D
var spawn = false
export (int) var xLoc = -20
export (int) var yLoc = 0
export (int) var direction = -1
export (int) var time = 3
var enemy = preload("res://WarriorGolem.tscn")

func _physics_process(_delta):
    var bodies = get_overlapping_bodies()
    for body in bodies:
        if body.name == "Player" && spawn == false: #error here!
            spawn = true
            var e = enemy.instance()
            get_parent().add_child(e)
            e.position = Vector2(xLoc, yLoc)
            e.direction = direction
            yield(get_tree().create_timer(time), "timeout")
            spawn = false

            if direction == 1:
                $Sprite.flip_h = true

But it gives me that error: invalid get index 'name' (on base: 'previously freed instance')

in Engine by (55 points)

1 Answer

0 votes
Best answer

At some point during the for loop, one or more of the bodies are being freed so when it comes back to body.name == "Player", body.name can't be found. If you're trying to detect a single overlap by the player you're better off using the body_entered signal. The code can stay the same just without the need of a for loop.

by (3,257 points)
selected by

What should I replace with body_entered?

extends Area2D
var spawn = false
export (int) var xLoc = -20
export (int) var yLoc = 0
export (int) var direction = -1
export (int) var time = 3
var enemy = preload("res://WarriorGolem.tscn")



func ready():
    connect("body_enter", self, "on_GolemSpawner_body_entered")



func _on_GolemSpawner_body_entered(body):
    if body.name == "Player" && spawn == false:
            print("sciao")
            spawn = true
            var e = enemy.instance()
            get_parent().add_child(e)
            e.position = Vector2(xLoc, yLoc)
            e.direction = direction
            yield(get_tree().create_timer(time), "timeout")
            spawn = false

            if direction == 1:
                $Sprite.flip_h = true

Now it works correcty! is the func ready that i've made correct?
The only problem is a bit of lag when a golem spawns, but it's better than a crash I guess

Does it lag every time or only the first time it spawns? I've also had a slight lag issue on the first spawn, I don't know if it's instance(), add_child() or if it's down to the parent node type of the instanced scene which for me was a CPUParticles node.

It is add child,when a golem spawns there s a little freeze(Not every time)

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.