Distance to player cancel

Hi All,

I have an enemy boss that has a move where it teleports to the player and executes a custom grab sequence (I have that aspect of it running). I would however like the player to be able to evade by running or jumping out of the way, and so I put together a script aiming to calculate the distance between player and boss once the boss has teleported, with the aim the change the animation if the player is too far. I have the following, which really should work, but it crashes. Hoping someone will see a bug I don't.


@script
void self = getlocalvar("self");
void trgt = findtarget(self);
int x = getentityproperty(self, "x");
int z = getentityproperty(self, "z");
int Tx = getentityproperty(trgt, "x");
int Tz = getentityproperty(trgt, "z");
float Disx = Tx - x;
float Disz = Tz - z;
if(Disz < 0){
Disz = -Disz;
}
if(Disx < 0){
Disx = -Disx;
}
int RxMin = 8;
int RzMin = 3;
if(frame==37){
if( Disx >= RxMin || Disz >= RzMin ){
performattack(self, openborconstant("ANI_Freespecial5"));
}
}
@end_script

int RxMin and int RzMin are arbitrary distances. Exceeding them should switch the boss into a different animation, namely, a teleport away.
 
You need to add check if there's any target found or not. If no one is found, don't bother acquiring his/her/its stats let alone performing animation change.
 
You need to add check if there's any target found or not. If no one is found, don't bother acquiring his/her/its stats let alone performing animation change.
Doesn't findtarget just return the nearest enemy? If the player is the only enemy, it should always return that, no?
 
When scripting I often debug using text objects to display the output to variables to see if they hold the correct values or a simple way is to log values so you could do this.

void trgt = findtarget(self);
log("target found:" + trgt + "\n");
int x = getentityproperty(self, "x");

then check the log file for the text "target found" to confirm if a entity was returned.
 
Doesn't findtarget just return the nearest enemy? If the player is the only enemy, it should always return that, no?
True, but there might be rare case in which there is no player at all when script is run and that could cause crash.
Therefore it's best to add check before attempting to acquire target's properties :)
 
I'll be damned. This works:

@script
void self = getlocalvar("self");
void trgt = findtarget(self);
if(trgt != NULL()){
int x = getentityproperty(self, "x");
int z = getentityproperty(self, "z");
int Tx = getentityproperty(trgt, "x");
int Tz = getentityproperty(trgt, "z");
float Disx = Tx - x;
float Disz = Tz - z;
if(Disz < 0){
Disz = -Disz;
}
if(Disx < 0){
Disx = -Disx;
}
int RxMin = 8;
int RzMin = 3;
if(frame==33){
if( Disx >= RxMin || Disz >= RzMin ){
performattack(self, openborconstant("ANI_Freespecial5"));
}
}
} else {
performattack(self, openborconstant("ANI_Freespecial5"));
}
@end_script
 
Also
When scripting I often debug using text objects to display the output to variables to see if they hold the correct values or a simple way is to log values so you could do this.

void trgt = findtarget(self);
log("target found:" + trgt + "\n");
int x = getentityproperty(self, "x");

then check the log file for the text "target found" to confirm if a entity was returned.
Also, thanks for this! Didn't know you could output to log. I've done plenty of coding, but I'm still learning the subtleties of C.
 
Also

Also, thanks for this! Didn't know you could output to log. I've done plenty of coding, but I'm still learning the subtleties of C.
Welcome to the forum @wrenchman82!

Just FYI, OpenBOR script uses the C syntax, but it's a subset of C with some unique rules. Since you know C, I'll give you a few key differences in OpenBOR Script:
  1. Variables are weak typed. See here for more details on variable type and scope.
  2. No struct support.
  3. Arrays allow inserts and deletes on the fly (they're actually hash table double linked lists under the hood).
There are naturally a lot of added functions unique to the engine. Log() is one of them. In C, you'd use printf().

HTH some, and enjoy our world! :cool:

DC
 
Last edited:
Back
Top Bottom