PDA

View Full Version : NPC data types crashing on NPC death



C-Dawg
01-21-2007, 12:49 PM
This is getting annoying. When you have a script that creates an NPC and a pointer to that NPC, it appears that it crashes without fail upon that NPC's death. This code, for instance, crashes upon the enemy's death.



// =====================================
// enemy_hp_ghosting - This ffc will create
// a specified enemy at the location of the
// ffc. It will set the enemy's HP to a
// defined value. The enemy will be kept
// centered at the ffc.
// This script will monitor the health of the
// creature it created, and either warps or
// changes the combo of the FFC when the NPCs
// health is below 10.
// Use to give an FFC "hit points" by creating
// a fire enemy or shooter that moves with it.
// D0 = The enemy ID you want to create.
// D1 = The HP you want the enemy to have.
// D2 = 0 -> Switch FFC combo
// Any other value -> Warp player
// D2 = The destination DMAP.
// D3 = The screen on the destination DMAP.
// D4 = The number of this FFC (for this-> bug)
// =======================================

ffc script enemy_hp_ghosting{

void run(int enemy_id, int hit_points, int warp_or_change, int dest_dmap, int dest_screen, int this_ffc_number){

npc ghost_enemy = Screen->CreateNPC(enemy_id);
ghost_enemy->HP = hit_points;
int x = this->X;
int y = this->Y;
bool enemy_defeated = false;
ffc this_ffc = Screen->LoadFFC(this_ffc_number);

while (!enemy_defeated){

ghost_enemy->X = x;
ghost_enemy->Y = y;
x = this_ffc->X;
y = this_ffc->Y;

if (ghost_enemy->HP <= 10) {

ghost_enemy->HP = 0;
ghost_enemy = Screen->LoadNPC(1);
enemy_defeated = true;
if ( warp_or_change == 0 ){
Game->PlaySound(3);
this_ffc->Data++;
}
else{
Link->Warp(dest_dmap, dest_screen);
}
}

Waitframe();

} // end of while loop

} // end of void run

} // end of enemy_ghosting


I tried to make sure this code would never, ever refer to ghost_enemy once it had died. When it is down to 10 hit points, a variable is set that disables the while loop. The enemy is then killed by setting HP to zero. There should be no time where the engine is looking for an NPC that isnt there. Yet, as soon as an enemy dies, it's crashtown.

blue_knight
01-22-2007, 05:00 AM
What is this line for: "ghost_enemy = Screen->LoadNPC(1);", which appears after you set the HP to 0? Does it crash if you take that line out?

DarkDragon
01-22-2007, 08:45 AM
I'm guessing Blue Knight is correct. LoadNPC seems to crash currently if the NPC being indexed does not exist. It's high on my list of bugs to fix as soon as I get back next week.

C-Dawg
01-22-2007, 12:20 PM
I added the line BlueKnight is talking about as an attempt to avoid crashing. Without the line, it will crash. With the line, supposedly NPC will be redirected to enemy #1, but that doesn't seem to happen either.

C-Dawg
02-05-2007, 01:22 PM
So, I don't suppose anyone has fixed this problem yet? Or figured out a way to stop Zquest from crashing the instant an NPC variable points to a dead NPC?

I suppose one could simulate the effect just by moving NPCs off the screen and setting a variable. This is sort of a sloppy solution, though.

-C

DarkDragon
02-05-2007, 03:22 PM
Yes, this has been fixed in the next beta.
http://www.armageddongames.net/forums/showthread.php?t=95616

C-Dawg
02-05-2007, 08:54 PM
That thread dealt with syntax problems with the LoadFFC(x) statement at compile time. Does your fix also solve crashing problems when the NPC pointed to by a NPC data type dies at runtime?

DarkDragon
02-05-2007, 10:29 PM
Yes. Any attempt to access an invalid NPC should now print an error to the allegro log instead of crashing ZC.

C-Dawg
02-06-2007, 01:20 PM
Cool. So, from the player's perspective, nothing happens? The NPC or FFC pointer just makes a note that there's nothing to point to, and then evaporates?

Saffith
02-06-2007, 01:36 PM
The statement does nothing in the game, but the pointer remains and the script continues running.
So you'll still want to be careful to avoid doing this, at least in loops. Writing error messages every frame can cause massive slowdown. Plus, it makes for a really huge log file.

C-Dawg
02-06-2007, 01:56 PM
No garbage collection, huh?

Does this also fix the problem where an NPC that no longer points to a live enemy will re-assign itself to point to another enemy?

C-Dawg
04-21-2007, 04:31 PM
Note: This problem persists in the latest betas. Once a ghosted enemy dies, ZScript crashes.

C-Dawg
04-27-2007, 07:24 PM
I take that back. It appears that the problem has been fixed for at least some situations. Once DarkDragon fixes the Sidequest file, I'll try to figure out why and when it works.