Hi Everyone,
I'm writing a twin-stick shooter and encountering a weird error where my game freezes / locks up after a short period of time - was really hoping a second set of eyes could help me see what I'm overlooking.
It started happening after I introduced a new enemy node to the game. The game runs anywhere from 10 - 120 seconds (variable) and then freezes. No errors are in the console and the game window is unresponsive. I THINK I've isolated the problem, but I'm not sure why it's occurring. I have spawn points that spawn new enemies every few seconds in the arena, and I isolated the enemies 1 by 1 until I found who was locking my game. It's this guy (it's a KinematicBody2D with a Sprite of a meteor and a circular CollisionShape2D as children):
extends KinematicBody2D
var direction = Vector2(0.5,0.5)
var scale = Vector2(2.0, 2.0)
var speed = 200
var rotationRate = 4
const EPSILON = 0.00001
onready var explosionPlayer = get_tree().get_root().get_node("Level_1/ExplosionPlayer")
onready var fireball = preload("res://particles/Enemy_Explosion_1.tscn")
onready var sprite = get_node("Sprite")
func _ready():
set_fixed_process(true)
speed = speed + (randi() % 200)
#rotationRate = max(EPSILON,randf()) * 4
var x = randf()
var y = randf()
# Prevent 0,0 direction
if (x + y) == 0:
x = -1.0
y = -1.0
direction = Vector2( x, y ).normalized()
# if randf() > 0.5:
# rotationRate = -rotationRate
# direction = -direction
# var scalerand = 1+(2*randf())
# scale = Vector2( scalerand , scalerand )
scale(scale)
func _fixed_process(delta):
if is_colliding():
var obj = self.get_collider()
if obj.is_in_group("wall") or obj.is_in_group("meteor"):
direction = direction.reflect( get_collision_normal() )
sprite.rotate( rotationRate * delta )
move( direction * speed * delta )
func takeDmg(amount):
var f = fireball.instance()
f.set_pos( get_pos() )
get_tree().get_root().add_child( f )
explosionPlayer.play("boom3")
queue_free()
Here's the thing - If I comment out the if is_colliding statement, the program runs fine. I can run for more than 10 minutes without problems. So it's something in that collision block - I'm pretty sure. The meteors collide and bounce off of walls and each other while it's running so the groups are set correctly. The only thing I can think of is maybe get_collision_normal() might return something bad occasionally?
Thanks for any thoughts you may have - I'm running thin on ideas. (I may check for null collision_normals next just to tie that off - just thought of it :smile: )