Hello hello!! I have ran into a recent issue with my game that pertains to the handling of area3Ds; I have code within my PLAYER controller that detects whenever it enters an area3D, however, I have hit an error when the code attempts to check what kind of GROUP the area3D is in.
Some ENEMIES spit out an area3D as a projectile, this projectile is in a certain GROUP specific to enemy projectiles. When the projectile intersects with the PLAYER it does damage, and then runs the âqueue_free()â command. However, after a few seconds, the game crashes and presents the âCannot call method âis_in_groupâ on a previously freed instance.â error.
This is because it is also trying to check if the projectile is part of another group, one for another entity, but cannot since it is queue_free()'d.
Is there a way to make it STOP checking which group the area is in if it is queue_free()'d?
However Iâm not entirely sure what youâre doing. Using is_instance_valid as a guard may stop the game crashing, but is potentially just working around some deeper design problem.
The overall design for the enemy projectiles might be flawed. Every projectile is a raycast that moves an Area3D in a set direction, and when it collides with something, whether it be the player or a wall/floor, it is supposed to do damage and delete itself.
I have not been able to find a better method for projectiles, so I have settled with this method for now.
If your projectiles are all the same, instead of a node group, how about adding a class_name Projectile to the projectiles? Then, you can do if object is Projectile instead of checking is_in_group. I donât know if it fits in your problem, but itâs worth a try!
A fix for your current setup, like what @gentlemanhal said, is to use is_instance_valid to check for the instance of the projectile. Also, disconnect the projectile before running queue_free.
Hmmmmm, that sounds interesting! Apologies for asking, but how would I go about achieving something like that? I am a beginner with GDscript so I have no idea where to start.
Thank you! This is very useful! If you also donât mind me asking, how might I check if the projectile is in a specific class_name? I tried âif area is ENEMY_PROJECTILEâ but that doesnât seem right.
This one did the trick! I attached a script to the area of the projectile that puts it in the ENEMY_PROJECTILE class and that fixed the issue entirely! Thank you tons!