vec_to_angle and c_scan makes engine freeze

Posted By: Denn15

vec_to_angle and c_scan makes engine freeze - 02/03/15 21:40

i have some enemies in a level and for some reason the engine freezes when they are hit by the player characters scancone and stops responding.

the scancone is used for attacks, so when the player attacks, the player entity uses c_scan around it so the enemy can detect if it has been hit. if an enemy is hit they lose health.

sometimes it takes like 10 hits by c_scan, other times it takes like 2 so i tried to find out what caused it and it turns out that if i remove the vec_to_angle from the following code from the enemy entities' action, then the engine wont freeze:

Code:
action enemy()
{
	wait(-1);
	enemycount = enemycount + 1;
	VECTOR diffen;
	ANGLE angleen;
	c_setminmax(me);
	my.health = 2 + random(4);
	vec_set(my.scale_x, vector(1 + random(1), 1 + random(1), 1 + random(1)));
	//attacking();	
	my.group = 3;
	my.pan = random(360);
	my.emask = ENABLE_SCAN;
	my.event = hit_me;
	set(my, SHADOW);
	while(my.health > 0)
	{
		c_ignore(2, 0);
		c_move(me, vector(my.speed * time_step, 0, 0), nullvector, GLIDE);
		if (my.z < - 1024)
		{
			my.health = 0;
		}
		if (my.speed < 0)
		{
			my.speed = my.speed + 5 * time_step;
		}
		if (vec_dist(my.x, player.x) >= 500) if (my.speed > 0)
		{
			my.speed = my.speed - 5 * time_step;
		}
		if(vec_dist(my.x, player.x) < 500)
		{
			c_trace(vector(my.x, my.y, my.z + 20), player.x, IGNORE_ME);
			if(you)
			{
				vec_diff(diffen, player.x, my.x);
	vec_to_angle------>	vec_to_angle(angleen, diffen);            <-----vec_to_angle
				c_ignore(2, 0);
				c_rotate(me, vector(angleen.pan - my.pan, 0, 0), GLIDE);
				if(my.speed < 20)
				{
					my.speed = my.speed + 5 * time_step;
				}
			}
		}
		wait(1);
	}
	combo = combo + 0.1;
	effect(bloodex, 100, my.x, vector(0, 0, 40));
	enemycount = enemycount - 1;
	ent_remove(me);
}



enemies event function:
Code:
function hit_me()
{
	my.health = my.health - 1;
	my.speed = - 20 * player.strength;
	effect(blood, 30, my.x, vector(0, 0, 40));
}

Posted By: sivan

Re: vec_to_angle and c_scan makes engine freeze - 02/04/15 08:29

try to check if (you == player), but preferably use more ignores in c-tracing to not to detect level geometry and other enemies, only the player entity. otherwise the code looks okay for me.
Posted By: Denn15

Re: vec_to_angle and c_scan makes engine freeze - 02/04/15 09:45

i tried to check if you was the player entity and made c_trace ignore other entities, but not the ground since i use it to see if the player can be seen be the enemies. however the game still freezes and stops responding.

is there any known typical cause that makes the engine freeze and stop responding that i maybe can look into. the only cuase i know so far is an endless while(1) loop with no wait() in it and i checked that it does not happen anywere
Posted By: Reconnoiter

Re: vec_to_angle and c_scan makes engine freeze - 02/05/15 16:44

No error message with high warn_level ?

Using pointers & arrays wrong can eventually let to a game crash too (like using an empty pointer in a c_trace etc.), maybe check those.

Also iirc you should not create particles within an entity event function unless you do a wait before that or something like that.

From the manual:
Quote:
The event function itself should be simple. It normally should only transfer information to the entities' main function - it shouldn't perform instructions that can trigger events itself, displace entities, start particles or change anything else in the level. Thus instructions like c_move, ent_create, ptr_remove, c_trace etc. must not be performed. If the event function must perform such 'critical instructions', precede them by a wait(1) for delaying them to the next frame. Then it's safe.
Posted By: Denn15

Re: vec_to_angle and c_scan makes engine freeze - 02/05/15 20:20

i have not tried setting a high warn_level but there was 1 single instance where i got a "script crash in enemy" error and the game could continue afterwards and the enemy that crashed did nothing afterwards, however the game froze a little later when hitting another enemy.

i have actually managed to fix the problem by putting a 1 second delay before removing the enemy entity. i guess the problem has something to do with the entity being accessed when its removed or something like that but im still not sure what excactly the problem was.

i also tried removing the effect commands from the events as i read that it was not a good idea in the manual, but it didnt seem to do any difference here.
Posted By: Reconnoiter

Re: vec_to_angle and c_scan makes engine freeze - 02/05/15 21:17

Good to hear that it is solved,

Quote:
i also tried removing the effect commands from the events as i read that it was not a good idea in the manual, but it didnt seem to do any difference here.
, I think that is cause the engine tries to recover from the fault but I don't know for sure (or something maybe with bad memory being written), but I do know you want to prevent it cause when these code bugs do let your game crash their are often alot trickier to track down cause it is not always inmediately clear where they come from.
© 2024 lite-C Forums