Well, this became a personal thing between me and Mr. Pop-Up Target. Looks like he's about to loose!
Try this one. It's more user-friendly (no need to set nopop and fits in the in Init field). I tested it more thoroughly this time. If you find any ...uhm... shortcomings in my testing procedures, let me know!
/* InitPopUpTarget.sqf
*
* SYNTAX: _dummy = [Object, Number, Boolean] execVM "InitPopUpTarget.sqf
*
* PARAMETERS: Object: A pop-up target
* Number: The number of hits it takes to knock the target down
* Boolean: If set to True, the target will pop up again.
*
* NOTES: This function is meant to be placed in the Initialization field
* of a pop-up target.
*/
#define HIT_COUNTER "HitCounter"
#define ONHIT_PARAMS "AdvPopUp_OnHit_Params"
#define ONHIT_HANDLER "AdvPopUp_OnHit_Handler"
#define ONHIT_INDEX "AdvPopUp_OnHit_Index"
nopop = true;
_target = _this select 0;
_requiredHits = _this select 1;
_isPopUp = _this select 2;
_hitHandler = {
private ["_target", "_requiredHits", "_isPopUp", "_hitCount", "_keepUp"];
_target = _this select 0;
_requiredHits = _this select 1;
_isPopUp = _this select 2;
if (_target animationPhase "terc" > 0.1) exitWith {};
_hitCount = (_target getVariable HIT_COUNTER) + 1;
_keepUp = true;
if (_hitCount == _requiredHits) then {
_hitCount = 0;
_target animate ["terc", 1];
sleep 3;
_keepUp = _isPopUp;
};
if _keepUp then {
_target animate ["terc", 0];
};
_target setVariable [HIT_COUNTER, _hitCount];
};
_target setVariable [HIT_COUNTER, 0];
_target setVariable [ONHIT_PARAMS, _this];
_target setVariable [ONHIT_HANDLER, _hitHandler];
_code = {
_t = _this select 0;
(_t getVariable ONHIT_PARAMS) spawn (_t getVariable ONHIT_HANDLER); // weird!
};
_index = _target addEventHandler ["Hit", _code];
_target setVariable [ONHIT_INDEX, _index];
Explanation:
The big problem is that the built-in script is working against this script. It tries to knock the target down on every hit (if you comment out the
_target animate ["terc", 1]; it will still be working). So the above script has to keep the target up until the required number of hits is reached. The number of hits is saved in a variable that is attached to the pop-up target itself (
_target setVariable [HIT_COUNTER, _hitCount];).
The next problem occurs when the target is moving down. If the above script would not exit, it would put the target in a upright position again.
if (_target animationPhase "terc" > 0.1) exitWith {}; prevents the script from running while the the target is not up. This is where the problem with the other script was: it checked for "> 0" which looks fine at a first glance. But sometimes the built-in script has already started the "go down" animation. So
animationPhase returned a number bigger than zero (like 0.00042) and my script bailed out...
The code outside the
_hitHandler is not really relevant. It's some arcane event handler stuff that my brain came up with.