Jump to content
AmBush03

Passing variables to event handlers

Recommended Posts

I am trying to make a simple function that will add an event, which triggered, causes a select few animations to repeat on themselves, however,

I'm struggling with finding a way to pass a variable from the function to the event itself. (The string of the animation to loop.)

 

I have searched a lot of forum posts, but I haven't found anyone with a similiar issue. Maybe there's an easier way to do what I'm trying to do?

Any help would be greatly appreciated.

 

fnc_loopAnim =
    {
        params ["_unit", "_loopedAnim"];
        _unit addEventHandler [ "AnimDone",
        {
            params["_unit","_anim", ???];
            if (_anim == _loopedAnim) then
            {
                _unit switchMove param _loopedAnim;
                _unit playMoveNow param _loopedAnim;
            };
        }];
    };

 

Share this post


Link to post
Share on other sites

I think what you're looking for is closures but sadly this concept doesn't exist in SQF. One workaround is to set the variable in unit's namespace:

_unit setVariable ["loopedAnim", _loopedAnim];

and then retrieve it inside the event handler:

_loopedAnim = _unit getVariable "loopedAnim";

Works fine as long as you don't run multiple handlers simultaneously on the same unit.

  • Like 3

Share this post


Link to post
Share on other sites

In order to pass arguments to the event handler, you need to replace the code with a string and add arguments via the format command. Try to add this handler to the player's init and you'll see how it works:

this addEventHandler 
	[
		"FiredMan", 
		format 
			[
				' 
					params ["_unit", "_weapon", "_muzzle", "_mode", "_ammo", "_magazine", "_projectile", "_vehicle"];

					systemchat  "%1";
				', 
				name player
			]
	];

 

Share this post


Link to post
Share on other sites

Thank you very much for the replies, though I can't help but feel like there's an easier way to do what I'm trying to make - I noticed that animation configs have a "looping" property which I assume governs if the animation should loop once it's complete - Could it be possible to modify the config using mission config files?

Share this post


Link to post
Share on other sites
5 minutes ago, AmBush03 said:

Thank you very much for the replies, though I can't help but feel like there's an easier way to do what I'm trying to make - I noticed that animation configs have a "looping" property which I assume governs if the animation should loop once it's complete - Could it be possible to modify the config using mission config files?

You'd need an addon, I don't think you can change this config in mission.

 

Hooking into the AnimDone event seems like the natural way to do this in Arma. If you're looking to have some ambient animations, take a look at BIS_fnc_ambientAnim. You can open it in Functions viewer to find out how Bohemia devs tackled this problem.

 

 

Share this post


Link to post
Share on other sites
21 minutes ago, _foley said:

You'd need an addon, I don't think you can change this config in mission.

 

Hooking into the AnimDone event seems like the natural way to do this in Arma. If you're looking to have some ambient animations, take a look at BIS_fnc_ambientAnim. You can open it in Functions viewer to find out how Bohemia devs tackled this problem.

 

 

Feels like such a simple thing to do, too...

 

I looked up the bohemia's function, it seems to do the exact same thing you suggested:
 

//play next anim when previous finishes
	_ehAnimDone = _unit addEventHandler
	[
		"AnimDone",
		{
			private["_unit","_anim","_pool"];

			_unit = _this select 0;
			_anim = _this select 1;
			_pool = _unit getVariable ["BIS_fnc_ambientAnim__anims",[]];

 

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×