• Godot Help
  • How to handle cleaning array references to freed objects?

I have predator animals that keep track of prey animals. At first I kept them in an array but when one of the prey dies or is eaten I get an invalid element in the predator's prey array, which hints me this is not the way.

What is the best practices way of storing collections of other nodes to avoid having to clean up?

  1. children nodes

Prey nodes can't be children of predators since there are many types of predators which share prey.

  1. groups

That could work if I assign a group for each species, but that's a lot of groups. Is that the appropiate way?
pick_random from group each time the predator is instructed to hunt.

  1. signal on death/freed

Since I have to register each new prey to each potential predator at birth, I could also connect the free signal and delete from the array reactivelly.

  1. blackboard

have a blackboard object each animal registers to. I think this wouldn't be that different to the groups option.

any more ideas? how would you do it?

  • xyz replied to this.
  • I would use groups. They are just arrays behind the scene, so it's similar to a blackboard, except you don't have to manage it yourself.

    I would use groups. They are just arrays behind the scene, so it's similar to a blackboard, except you don't have to manage it yourself.

    quimnuss Definitely groups. Better let native code keep the books instead of script arrays.