Script to make enemies change anim when player in range

aL BeasTie

Well-known member
It seems I get worse at scripting rather than better :p

This is what I'm trying to do -

I'm trying to make an enemy that spawns and stays motionless until player approaches it (gets within range)

So the idea is too give the enemy a SPAWN anim that is looped so they don't move,
image.png


then @script  or animationscript in this animation to check if player is in range then change to ANI_FOLLOW2
image.png


This is my basic script but, I've tried numerous ways but this is basically what I have.

Code:
void alert()
{
void self = getlocalvar("self");
int player = getlocalvar("player");
int anim = getentityproperty(self, "animationid");
if(checkrange(self, player))
	{
	changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW2"), 1);
	}
}

Obviously this is very wrong...

Can someone please show me the proper way to do this?
 
Instead of correcting that script, I'll show you the function I usually use:

void attack1(int RxMin, int RxMax, int Rz, void Ani)
{// Attack interruption with range check
    void self = getlocalvar("self");
    void target = findtarget(self); //Get nearest player
    float x = getentityproperty(self, "x");
    float z = getentityproperty(self, "z");
    int dir = getentityproperty(self, "direction");

    if(target!=NULL()){
      float Tx = getentityproperty(target, "x");
      float Tz = getentityproperty(target, "z");
      float Disx = Tx - x;
      float Disz = Tz - z;

      if(Disz < 0){
        Disz = -Disz;
      }

      if( Disx >= RxMin && Disx <= RxMax && Disz <= Rz && dir == 1) // Target within range on right facing?
      {
        performattack(self, openborconstant(Ani)); //Change the animation
      } else if( Disx >= -RxMax && Disx <= -RxMin && Disz <= Rz && dir == 0) // Target within range on left facing?
      {
        performattack(self, openborconstant(Ani)); //Change the animation
      }
    }
}

Usage example:

@cmd attack1 240 2000 100 "ANI_FOLLOW1"
 
Should of asked you about this like 2 years ago...  :)

It works great! :), I just need to get the values right so they attack at the right range/moment.

Many thanks! as always

EDIT: I don't quite get how the values work, I got weird results.  But I found the right settings for me, works fantastic!

@cmd attack1 80 300 100 "ANI_FOLLOW1"



 
spawn animation must also be set to 'loop 1' so without the script, they are stuck on the spot.  Script tells them to switch to follow1 anim when player in range.


 
learn something new everyday. I stop using spawn because i never see it in action. characters spawn off screen, but now they will stay in pose till i get close. sweet. just make sure not to spawn chars at the wait point off screen or they'll be stuck off screen, and the stage will no longer move. :P
 
If you change the stage direction you need to add 'flip 1' to enemy spawns.

If you add this to enemies in normal direction stages, they won't attack until you walk past them.  This could be a cool trick thou for some enemies.
 
Bloodbane said:
Instead of correcting that script, I'll show you the function I usually use:



Usage example:

@cmd attack1 240 2000 100 "ANI_FOLLOW1"

Interesting but I have some questions:
- I need to insert that script function on another script file? like animationscript or smtg..
- I declare that @cmd in the Spawn animation, but a question about BB's example: what animation is "follow 1"? a normal idle anim??
so when player is in "range", the enemy animation should change to some follow animation or to idle?

Bloodbane said:
SPAWN animation is fun to use. Now with script tag allowed in level texts, we can set various SPAWN animation for enemies ;) ;).
an example please  :)
 
- I need to insert that script function on another script file? like animationscript or smtg..

It should be in animationscript

what animation is "follow 1"? a normal idle anim??
so when player is in "range", the enemy animation should change to some follow animation or to idle?

FOLLOW1 in this case is for 'stop waiting and start action' animation. It could be anything, 2 examples:
1. See sleeping Garcias in SoR2 stage 4? To make this, use looping sleeping animation as SPAWN and use wake up then get off bench as FOLLOW1
2. You can make enemies crouching while waiting for players to get close. Then when player is in range, this enemy attacks. This is what Garcias do in SoR1.

an example please

Here's SPAWN animation from Williams:

anim spawn
@script
    void self = getlocalvar("self");
    int Enter = getentityvar(self, 6);

    if(frame==1){
      if(Enter==1){
        setentityvar(self, 6, NULL());
        changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW5"));
      } else if(Enter==2){
        setentityvar(self, 6, NULL());
        changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW6"));
      } else if(Enter==3){
        setentityvar(self, 6, NULL());
        changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW7"));
      }
    }
    if(frame==3){
      changeentityproperty(self, "Subject_to_Wall", 1);
    }
@end_script
delay 10
offset 67 130
jumpframe 2 0 1.5 0
frame data/chars/misc/empty.gif
frame data/chars/williams/run1.gif
frame data/chars/williams/run2.gif
frame data/chars/williams/run3.gif
bbox 56 73 20 62
frame data/chars/williams/run4.gif
frame data/chars/williams/run1.gif
frame data/chars/williams/run2.gif
frame data/chars/williams/run3.gif
frame data/chars/williams/run4.gif

In this animation, Williams runs as his SPAWN animation. However, he checks his entity variable before running. If it is 1, he performs FOLLOW5, if 2, he performs FOLLOW6 and if it is 3 he performs FOLLOW7.

FOLLOW5 is come from door animation FOLLOW6 is come from below, after riding platform to be exact (Double Dragon stage 2 if you know what I mean)
FOLLOW7 is wait on wall above until player gets close (Double Dragon Advance).

To set his entity variable, you should set it in level texts like this:

spawn Williams
@script
void main()
{
    void self = getlocalvar("self");
    setentityvar(self, 6, 3);
}
@end_script
coords 365 202 112
at 200


spawn Door
coords 218 205
at 400

spawn Williams
@script
void main()
{
    void self = getlocalvar("self");
    setentityvar(self, 6, 1);
}
@end_script
coords 218 195
at 400

I included door spawn too to show that Williams is coming from door.
 
Thanks a lot!!

I will work today on this!!

just a question: this will conflict with spawnscript? or I should use one or another, but  not both? (is not a big problem though)






 
this will conflict with spawnscript? or I should use one or another, but  not both? (is not a big problem though)

Starting from certain build, only one spawnscript is supported and that includes that @script tag example above.
In some spawns, I had to break and insert noscpos.c spawnscript to @script to solve this.
 
I check health in spawn animation when spawning character and if health is 10 then my character is changing to different animation.Then its easier to control, i give health 10 to enemies that are walking forward only (like arrow enemies) and more health for enemies that stay and fight with you, i think you can use aggression instead of health too for this or even remap.

This is spawn animation of my enemy:
anim spawn
loop 0
offset 139 234
bbox 0 0 0 0
delay 2
@script
                void self = getlocalvar("self");
          int HP = getentityproperty(self,"health");
if ( HP == 10 ){
  changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW1"));
}
@end_script
frame data/chars/ss1/w13.gif
frame data/chars/ss1/w13.gif

 
I got BloodBane's script working but I cannot get the results I want.

I need for one enemy this set of spawns:
1. Wait for player to approach, and then attacks (like Beastie wanted at the beginning of this thread, and as BB said, similar to Galsia on SOR2).
2. spawned as normally, offscreen to enter the screen walking.
3. enter the screen jumping (like the ninjas on TMNT).

If I do 1, the Beastie method, I cannot get the others. If I do the script and spawning in level by using
Code:
@script
void main()
{
    void self = getlocalvar("self");
    setentityvar(self, 6, X);
}
@end_script
once spawned, the enemy move to attack immediately, and I need to wait until player gets close...

it seems I will have to create clone of the enemy, changing only the way it spawns  :(
 
Yeah I had same prob but we can use this script method that was just posted

http://www.chronocrash.com/forum/index.php?topic=680.msg5329#msg5329

Code:
spawn   MrX
@script void main() {
   performattack(getlocalvar("self"), openborconstant("ANI_FOLLOW10"));
} @end_script
flip    1
map     1
item    food4
health  550
coords  -1480 470 71
at      1010

So you just make the extra spawn anims as follows and use this to control which one it will use when adding your enemy spawns to levels.
 
Back
Top Bottom