OFPEC Forum

Editors Depot - Mission Editing and Scripting => ArmA - Editing/Scripting General => Topic started by: Knight Trane on 02 Dec 2008, 18:56:35

Title: Popup Target, How to Increase Number of Shots Before Animation Starts(Solved!)
Post by: Knight Trane on 02 Dec 2008, 18:56:35
Hey guys,
I was wondering how to get the the popup targets to take 3 shots before going into their animation.  Any thoughts?
Title: Re: Popup Target, How to Increase Number of Shots Before Animation Starts
Post by: johnnyboy on 03 Dec 2008, 02:20:26
Maybe use a Hit EventHandler on the popup target to adjust the damage of each hit...
Title: Re: Popup Target, How to Increase Number of Shots Before Animation Starts
Post by: Tyger on 03 Dec 2008, 02:22:18
I believe that without making an addon, something such as that cannot be changed.

Instead, you can use the non-popup TargetE. This takes 3 hits to knock down. When you want it to pop back up, just use:
Code: [Select]
myTarget setDammage 0

@johnnyboy

I don't believe that your idea would work as proposed. When a single round strikes the target, the target falls down. The hit event handler would trigger after the target has fallen, or began to fall, since I believe editor placed event handlers have a lower precedence than event handlers local to the object. This means that the target would be reset when the animation was either completed or partially in progress. I could be wrong though... :)
Title: Re: Popup Target, How to Increase Number of Shots Before Animation Starts
Post by: Worldeater on 15 Dec 2008, 01:28:10
I'm sure you have already noticed the PopUpTarget.sqs that is included in several of the PBOs. Well, look at it again and ask youself what the global variable nopop is for? :whistle:


What might happen if we set it to true somewhere, put this in the Initialization field of a pop-up target:
Code: [Select]
this addEventHandler ["Hit", {_this execVM "PopUpTarget_3Rounds.sqf"}];
... create a file named PopUpTarget_3Rounds.sqf in our mission's directory, put the following code in it:
Code: [Select]
_target  = _this select 0;

if not nopop exitWith {};

_hitCount = _target getVariable "HitCounter";
if (isNil "_hitCount") then { _hitCount = 0 };

_hitCount = _hitCount + 1;

if (_hitCount == 3) then {
_hitCount = 0;
_target animate ["terc", 1];
sleep 3;
};

_target animate ["terc", 0];
_target setVariable ["HitCounter", _hitCount];

... and fire three rounds at a target? ;)
Title: Re: Popup Target, How to Increase Number of Shots Before Animation Starts
Post by: Knight Trane on 16 Dec 2008, 06:34:03
Well, I know that nopop keeps the Target from popping back up. 

I tried your script and it works great.   But, I can't make it work with the mission I made.

The mission is a CQB range.  I put
Code: [Select]
t1 animate["terc",1];in the Init Field of the Target named "t1".
that puts the Target in the down position from the start.

I have a game logic with
Code: [Select]
nopop=true; in the Init Field.  That keeps a Target in the down position in the down position.
I activate the Target with a trigger that has
Code: [Select]
t1 animate["terc",0];in the On Activation field.  That allows the Target to popup into position.

I've been trying everything I can think of.  That isn't much.  I just can't seem to get the your script to do what I want.   :confused:
Title: Re: Popup Target, How to Increase Number of Shots Before Animation Starts
Post by: Worldeater on 16 Dec 2008, 07:43:05
Hrrmmm, besides the equal signs after the t1, it looks fine. Did you check if your trigger is working correctly?
Title: Re: Popup Target, How to Increase Number of Shots Before Animation Starts
Post by: Knight Trane on 16 Dec 2008, 07:55:54
Thank you, 
I fixed my post.  It was working fine.  As you enter the range all targets are down.  When you activate the trigger the targets popup.  You shoot them and they go down and stay down until the next player goes through.
This is quite a pickle.
Title: Re: Popup Target, How to Increase Number of Shots Before Animation Starts
Post by: Worldeater on 16 Dec 2008, 16:01:59
Note that setting nopop to true, does not keep the target down here! The sole purpose of setting it to true is to prevent the built-in PopUpTarget.sqs from interfering with the script I've posted.


Edit:
I made a beefed up version of the script. It also requires nopop to be true but provides additional functionality. It allows to specify the number of hits it takes to knock the target down and whether the target will pop up again or not.

The init fields should look like this:
Code: [Select]
this addEventHandler ["Hit", {[_this, 3, true] execVM "AdvancedPopUpTarget.sqf"}];
Where 3 is the number of hits it takes to knock it down and true tells the pop-up target to stay down (set to false otherwise).


AdvancedPopUpTarget.sqf:
Code: [Select]
#define ERROR_NOPOP hint str(__FILE__ + "\n\nERROR:\nThis script requires the global variable nopop to be true")

if isNil("nopop") exitWith { ERROR_NOPOP };
if not nopop      exitWith { ERROR_NOPOP };

_eventInfo    = _this select 0;
_requiredHits = _this select 1;
_stayDown     = _this select 2;

_target = _eventInfo select 0;
if (_target animationPhase "terc" > 0) exitWith {};

_hitCount = _target getVariable "HitCounter";
if isNil "_hitCount" then { _hitCount = 0 };

_hitCount = _hitCount + 1;
_keepUp = true;

if (_hitCount == _requiredHits) then {
_hitCount = 0;
_target animate ["terc", 1];
sleep 3;
_keepUp = not _stayDown;
};

if _keepUp then {
_target animate ["terc", 0];
};

_target setVariable ["HitCounter", _hitCount];
Title: Re: Popup Target, How to Increase Number of Shots Before Animation Starts
Post by: Knight Trane on 18 Dec 2008, 17:14:22
Didn't work.  I put nopop=true in a game logic off to the side.  Put you r script in and the script call in the Init field, but it just took one shot.  Anything I did wrong.  And could you explain a little more about how your script works and what does what.

Thanks for all your attention and help.
Title: Re: Popup Target, How to Increase Number of Shots Before Animation Starts
Post by: Worldeater on 18 Dec 2008, 22:58:13
Well, this became a personal thing between me and Mr. Pop-Up Target. Looks like he's about to loose! :cool2:

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! :D

Code: [Select]
/* 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...  :weeping:

The code outside the _hitHandler is not really relevant. It's some arcane event handler stuff that my brain came up with.
Title: Re: Popup Target, How to Increase Number of Shots Before Animation Starts
Post by: Knight Trane on 19 Dec 2008, 00:47:43
Works as advertised!!! :clap: :clap: :clap:

I'm testing it in MP this week.  i'll let you know what happens.

Thanks
Title: Re: Popup Target, How to Increase Number of Shots Before Animation Starts
Post by: hoz on 19 Dec 2008, 02:12:40
We don't seem to have a good example or popup script in the ED. So if you don't mind could you provide a demo mission and submit it too the ED?
Title: Re: Popup Target, How to Increase Number of Shots Before Animation Starts
Post by: johnnyboy on 19 Dec 2008, 03:13:18
Yes please do post a popup target resource to the site (I have a target mission in mind as well, and could use it!).

Worldeater, you are a productive dude...glad you joined the site!
Title: Re: Popup Target, How to Increase Number of Shots Before Animation Starts
Post by: Worldeater on 19 Dec 2008, 15:01:13
@hoz
Let's wait for Knight's OK. If it's working in MP, too, I'll make a small example mission and add it to the depot.

@johnnyboy:
Thanks. I'm glad, too. :D  It looks like this is the place to be if one is serious about ArmA scripting/editing.
Title: Re: Popup Target, How to Increase Number of Shots Before Animation Starts
Post by: Knight Trane on 19 Dec 2008, 17:14:20
  I'd like submit on Worldeaters behalf my project.  He did all the heavy script lifting. 

I've been pursuing this Popup Target problem for some time now.  I've seen plenty of posts that went nowhere and I'm just glad that this aspect has finally been cracked.  I can now get out of testing and into the actual mission.

  This is the place for people that want to do exceptional work.  I've never been disappointed in the help and knowledge to gained from these forums......

Alright back to the mission.  Anyone have an idea on how to make these targets move?
Title: Re: Popup Target, How to Increase Number of Shots Before Animation Starts
Post by: johnnyboy on 19 Dec 2008, 17:41:37
Code: [Select]
Anyone have an idea on how to make these targets move?
I'm guessing you want a target sliding left or right, as a moving target eh?

A simple while loop where you setpos the target left or right a small amount with each iteration should do the trick.   The following line should shift the target left or right depending on whether the _moveIncrement is negative or positive.

Code: [Select]
myTarget setpos (myTarget modelToWorld [_moveIncrement, 0, 0]);

Title: Re: Popup Target, How to Increase Number of Shots Before Animation Starts
Post by: Knight Trane on 19 Dec 2008, 22:29:42
Never heard of "modelToWorld" command.  I'll play with this over the weekend and edit this post with what I get.
Thanks.
Title: Re: Popup Target, How to Increase Number of Shots Before Animation Starts(Solved!)
Post by: hoz on 30 Dec 2008, 20:50:23
I hope this example still makes it into the ED.  :clap: