Repeating
Postal3Script has a function called Repeat
where you can repeat actions over and over again.
This is also possible in AngelScript thanks to Wait function.
Here's how you can do an infinite repeat as an example:
| class Tut : IPostal3Script
{
// This has to be executed once! (in it's own context)
void CheckPlayer()
{
CP3SObj@ pPlayer = GetPlayer();
// For some reason it's null
if (pPlayer == null)
return;
string behavior = pPlayer.GetBehavior();
behavior.toLower();
// The Player is dead (the behavior's name changes when Dude dies in CR)
if (behavior != "bh_player")
return;
// This checks if the Player's health is under 50, and if yes it'll set it back to 50.
if (pPlayer.GetAttr("ea_health") < 50)
pPlayer.SetAttr("ea_health", 50);
Wait(3.0);
// Recall the function
CheckPlayer();
}
}
|
Just make sure to add a way to break out from this infinite loop, like this:
| class Tut : IPostal3Script
{
int iRepeat;
CP3SObj@ self;
Tut(CP3SObj@ obj)
{
iRepeat = 0;
if (@obj != null)
{
@self = @obj;
}
}
// This has to be executed once! (in it's own context)
void CheckPlayer()
{
CP3SObj@ pPlayer = GetPlayer();
// For some reason it's null
if (pPlayer == null)
return;
string behavior = pPlayer.GetBehavior();
behavior.toLower();
// The Player is dead (the behavior's name changes when Dude dies in CR)
if (behavior != "bh_player")
return;
// This checks if the Player's health is under 50, and if yes it'll set it back to 50.
if (pPlayer.GetAttr("ea_health") < 50)
pPlayer.SetAttr("ea_health", 50);
iRepeat++; // this adds +1
// If it's over 10 then abort by returning from it!
if (iRepeat > 10)
{
return;
}
Wait(3.0);
// Recall the function
CheckPlayer();
}
}
|