• 3D
  • Can't figure out two issues with colliders

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!

Oh goodie... it looked good in preview but all my indentation has disappeared :( Can I edit posts?

EnemyMesh and EnemyArea are child nodes of Enemy EnemyCollider is a child node of EnemyArea

BulletShape and BulletCollider are both children of Bullet

Hmm. Try using call_deferred("queue_free") instead of just queue_free(). I think I've had the same issue and I believe it has to do with the order in which things are called. You may be destroying objects when they are still trying to reference other objects.

You can edit posts. There's a little gear icon in the top right of the post.

FYI, you don't ever have to use "self". I don't know if you're doing that on purpose, some people think it makes the code more readable, but the functions all use self by default.

Also maybe you don't know about +=. It's pretty convenient sometimes. Instead of writing health = health + x, you can just do health += x and save some typing. There's also -=, *=, and /=.

Oh, forgot about the second issue. I think the easiest way to fix that is using separate collision channels. You're just using that area to detect bullets, right? So just put bullets their channel, enemies in theirs, and set the area to ignore the enemy channel.

However, to me it seems redundant that your enemy is a kinematic body but you have an area to detect collisions. I would only use one or the other. If you want some physics simulation to happen (things bouncing off enemies, wall collisions, etc.), use the kinematic body; if you just need to detect when the enemy hits a bullet or the player, just use the area.

Hey Ross,

I've just tried call_deferred, wasn't aware of that one yet :) Unfortunately it still crashes.. It really seems the bullet is still references somewhere that isn't being released properly, no idea what.

Self is a force of habit. I'm aware I can omit it but I'm one who indeed quotes the readability argument, though it is nothing more then subjective :)

I'm also aware of += but thanks for pointing it out. I've programmed C++ for well over 20 years and too this day I still intermix a += and a = a + in my code with no explanation as to why I use one or the other seemingly at random :) I guess I should really be more consistent :)

I was not aware of the different collision channels, that will prove to be very handy. I do want to detect when an enemy ship collides with my player so that will prove useful. All in all I've only been playing with GODOT for a few days now so I'll be figuring out which nodes are best to use for certain situations for awhile :)