Hope someone can help me out of my misery here, still new to all this.
I have two scenes, an enemy ship and bullets my player fires.
My enemy ship is setup as follows: - Enemy (KinematicBody) - EnemyMesh (MeshInstance) - EnemyArea (Area) - EnemyCollider (CollisionShape) The GDscript that handles the logic for my enemy is situated on the root node (Enemy) The body_enter signal of the Area is linked up to a method in this GDscript
My bullet is setup as follows: - Bullet (RigidBody) - BulletShape (MeshInstance) - BulletCollider (CollisionShape) Again my GDscript that handles the logic for my bullet is on the root node (Bullet)
When my player shoots I create a new instance of the bullet scene and add it to a node in my main scene:
var newbullet = bullet.instance()
newbullet.set_IsPlayerBullet(true)
var position = self.get_global_transform()
newbullet.set_global_transform(position)
get_node("/root/Main_Scene/Bullets").add_child(newbullet)
And in the _ready of my bullet.gd I give it some velocity (this is a 2.5D shooter):
var curr_rot = self.get_rotation()
var Velocity = Vector3(bulletspeed * cos(curr_rot.z), bulletspeed * sin(curr_rot.z), 0.0)
self.set_linear_velocity(Velocity)
Then in my enemy.gd script called by my body_enter signal:
func _on_Area_body_enter( body ):
print(self.get_parent().get_name(),"/",self.get_name()," hit by ", body.get_parent().get_name(),"/",body.get_name())
if body.has_method("get_damage"):
var damage = body.get_damage()
health = health - damage
if (health < 0):
self.queue_free()
if body.has_method("hit_something"):
body.hit_something()
And finally the hit_something function in bullet.gd:
func hit_something():
self.queue_free()
Ok, so here is problem number 1. The moment queue_free is called for my bullet in hit_something GODOT crashes. I've tried some other stuff like first removing the bullet from Main_Scene/Bullets first (which also crashes) and things like that. Stepping through the code I sometimes get an error in the debug log "Index p_mode out of size(3)". For now I'm just hiding the bullet as a workaround but I'd like to know what I'm doing wrong.
Problem number 2 is much less drastic. My enemy is colliding with itself. Not constantly, looks like it just does it when its instantiated and it'll be easy to skip over but still :)
One last tidbit which may be related to the crashing issue. The log also shows a couple of lines like:
Node not found: EnemyArea
Node not found: ...
Node not found: ...
Node not found: EnemyArea
Node not found: ...
Node not found: ...
These are shown when an enemy ship spawns. They are linked to a Path and PathFollow node where the PathFollow and its linked Enemy ship are duplicated so multiple enemy ships follow the same path...
Thanks!