[Zscript] Weird crash with +FRIENDLY flag regarding custom A_ChaseFunction

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
User avatar
JUSSOMONE
Posts: 35
Joined: Fri Oct 07, 2022 8:55 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia (Modern GZDoom)

[Zscript] Weird crash with +FRIENDLY flag regarding custom A_ChaseFunction

Post by JUSSOMONE »

Greetings, I've been trying all day with no avail in regarding friendly monsters custom movement functions...for some weird reason, when the player fires with any weapon and makes a sound with it, the game simply crashes.

If you want to know why I am doing this, it's simply because enemies and allies in my mod keeps firing continuously if their target is close enough to their melee range, plus there is the problem that the engine doesn't calculate the 3D distance to their target so, sometimes NPC's may keep firing at one another indefinitely if the player's ally target is on another ground level...so I had to create a custom A_Chase function with Distance3D to correct this...although I didn't expect it would crash.

The only thing I know it's that it always crashes with the +FRIENDLY flag, so I can be certain that it must be some sort of engine bug.

Here is my custom A_Chase function

Code: Select all

class Allies : Actor
{
	Default
	{
	          // INFO
		  Species                 "Allies";
		  
		  // FLAGS
		  MONSTER;
		  +FRIENDLY;
		  +DONTFOLLOWPLAYERS;
		  -COUNTKILL;
		  +DONTTHRUST;
		  +DONTGIB;
		  +FIXMAPTHINGPOS;
                  +FLOORCLIP;
                  +NOINFIGHTSPECIES;
	}
	Void A_ExtChase()
	{
		  if(target.health <= 0) //Otherwise the game will crash..since he still targets dead enemies
		  {
			    A_TakeInventory("ForgetTarget", 20);
		            A_ClearTarget();
			    SetStateLabel("Idle");
		  }
	         else if(target.health > 0 && Distance3D(target) < Radius + MeleeRange && CheckSight(target))
		  {
		        SetStateLabel("PointBlank");
		  }
		  else
		  {
		        A_Chase(null, null);
		  }
	}
	[...]

And finally, the friendly actor code:

Code: Select all


class UNLARegular_Bunker : Allies 
{  	
    Default
	{
	          // INFO 
		  Tag                            "U.N.L.A. Regular G.I.";
	          Obituary                    "%o made the Regulars angry.";
		  
		  // PROPERTY
		  Scale                         1.0;
		  Mass                          175;//100;
                  Health                       100;
	          Radius                       16;
	          Height                       54;
	          Speed                        4;
	          PainChance                256;
		  MaxStepHeight           24;
		  MaxTargetRange         1024;
		  MeleeRange               128; //MaxTargetRange/8
		  
		  // MISC
		  BloodType                  "WolfRedBloodSplash";
		  DamageFactor            "LCaliber", 1.0;
		  DamageFactor            "MCaliber", 1.0;
		  DamageFactor            "EnergyCaliber", 1.0;
		  DamageFactor            "MiniExplosive", 1.0;
		  DamageFactor            "Explosive", 1.0;
		  
		  // SOUNDS 
	          SeeSound                  "A_Infantry/Sight";
		  ActiveSound              " ";
	          PainSound                 "A_Infantry/Pain";
	          DeathSound              "A_Infantry/Death";
		  
		  // FLAGS
		  +NOTELEPORT;
	}
	States
	{
	// SPAWN AND IDLE
	Spawn:
	      UFA1 N 1 NoDelay;
	Idle:
	          //Wander
		  TNT1 A 0 A_LookEx(0, radius, 1536, 1024, 180);
		  UFA1 AAAAA 1 A_Wander();		  
		  UFA1 A 1;
		  
		  TNT1 A 0 A_StartSound("Walk/L_Step", 4, CHANF_OVERLAP, 1.0, 1.0);
		  
		  //Wander
		  UFA1 BBBB 1 A_Wander(); 
		  
		  //Wander
		  TNT1 A 0 A_LookEx(0, radius, 1536, 1024, 180);
		  UFA1 CCCCC 1 A_Wander();
		  UFA1 CC 1;
		  
		  TNT1 A 0 A_StartSound("Walk/L_Step", 4, CHANF_OVERLAP, 1.0, 1.0);
		  
		  //Wander
		  UFA1 DDDD 1 A_Wander(); 
		  TNT1 A 0 A_Jump(32, "Stand"); //25%
		  Loop;
	
		   
    // CHASE  
    See:
	          //Chase
		  UFA1 AAAAA 1 A_ExtChase();	  
		  UFA1 A 1;
		  
		  TNT1 A 0 A_StartSound("Walk/L_Step", 4, CHANF_OVERLAP, 1.0, 1.0);
		  
		  //Chase
		  UFA1 BBBB 1 A_ExtChase();
		  
		   //Chase
		  UFA1 CCCCC 1 A_ExtChase();
		  UFA1 CC 1;
		  
		  TNT1 A 0 A_StartSound("Walk/L_Step", 4, CHANF_OVERLAP, 1.0, 1.0);
		  
		  //Chase
		  UFA1 DDDD 1 A_ExtChase();
		  TNT1 A 0 A_GiveInventory("ForgetTarget", 1);
		  TNT1 A 0 A_JumpIfInventory("ForgetTarget", 20, "Forget");
		  TNT1 A 0 A_Jump(64, "CheckConditions"); //25%
		  Loop;
    	[...]

The crash:

Image

Can someone help me? it's for a project of mine that I've been working since april and it's nearing it's final beta stages.

Thanks in advance.

PS.: If you want anymore details, I will be happy to show it :)
Blue Shadow
Posts: 5039
Joined: Sun Nov 14, 2010 12:59 am

Re: [Zscript] Weird crash with +FRIENDLY flag regarding custom A_ChaseFunction

Post by Blue Shadow »

Code fragments aren't usually useful for debugging and testing, so this is just a shot in the dark: try doing a null check on target in the custom function before reading its fields. It could be null.
User avatar
JUSSOMONE
Posts: 35
Joined: Fri Oct 07, 2022 8:55 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia (Modern GZDoom)

Re: [Zscript] Weird crash with +FRIENDLY flag regarding custom A_ChaseFunction

Post by JUSSOMONE »

I did as you asked and it stopped crashing, although the actor frozes in place, and the only solution was to make him deaf

Code: Select all

	Void A_ExtChase()
	{
	      if(target != null) //In case it's the player...
              {
		       if(target.health <= 0) //Otherwise the game will crash..since he still targets dead enemies
		       {
				 A_TakeInventory("ForgetTarget", 20);
				 A_ClearTarget();
			         SetStateLabel("Idle");
		       }
	               else if(target.health > 0 && Distance3D(target) < Radius + MeleeRange && CheckSight(target))
		       {
		                 SetStateLabel("PointBlank");
		       }
		       else
		       {
		                 A_Chase(null, null);
		       }
             }
             else
             {
		       SetStateLabel("Forget"); //In case it's the player forget him if damaged.
	     }
     }
And in the actor I changed flags from 0 to LOF_NOSOUNDCHECK.

Code: Select all

[...]
A_LookEx(0, radius, 1536, 1024, 180);
[...]

[...]
A_LookEx(LOF_NOSOUNDCHECK, radius, 1536, 1024, 180);
[...]
Still, it's strange how even with the limited fov, they can see ALLAROUND them...anyway, can I optimize the code even more? Do you think it may have problems in the future? especially regarding targets that disappears from the game?

PS.: had to put a statelabel to forget in case the player damages him.
Blue Shadow
Posts: 5039
Joined: Sun Nov 14, 2010 12:59 am

Re: [Zscript] Weird crash with +FRIENDLY flag regarding custom A_ChaseFunction

Post by Blue Shadow »

JUSSOMONE wrote: Sun Nov 03, 2024 8:16 am Still, it's strange how even with the limited fov, they can see ALLAROUND them...
There's currently an issue with the FOV in A_LookEx being ignored or something like that. There are a couple of bug reports about this: here and here .
JUSSOMONE wrote: Sun Nov 03, 2024 8:16 am can I optimize the code even more? Do you think it may have problems in the future? especially regarding targets that disappears from the game?
I don't have experience with custom chase behavior, so I can't give you any tips on that. Sorry.
Post Reply

Return to “Scripting”