Jump to content
tuskohopkins

How to use EventHandlers init in a mod?

Recommended Posts

Hi,

 

I am fairly new to modding, so I don't understand too much yet. I searched the forums but not found much info about this.

I made a script which adds a "HandleDamage" eventHandler and some code in it to a unit.

I want to add this script to every man class unit created either on mission init or spawned later. Actually on mission start I can do it with a simple '{} forEach allUnits', but I have troubel with the spawned ones.

I think it would be the best to add the code to every units init field. As I understand the eventHandler init should do the trick but I dont really understand how shold I set it up.

 

Sofar I've tried to add this to my mods config.cpp

class CfgVehicles
{
   class EventHandlers
   {
      class Man;
      {
         init = "systemChat format ["Man created: %1", _this]";
      };
   };
};

Of course I would put run a 'fnc_somescript.sqf', but first I just tried to display a text for testing purposes.

Could someone explain how this should work? Or show alterntive ways to run script for every man created.

 

Thanks.

Share this post


Link to post
Share on other sites

You got the EventHandlers and Man class inverted, try it like this:

 

class CfgVehicles
{
	class Land;
	class Man: Land
	{
		class EventHandlers
		{
			class UNS_DSAI_UNITINIT
			{
				init = "if (isNil 'RUG_DSAI_INIT') then { '\RUG_
DSAI\RUG_DSAI\' call compile preProcessFile '\RUG_DSAI\RUG_DSAI\DSAI_Init.sqf'};
 d=[_this select 0] spawn RUG_DSAI_fAddUnit;";
			};
			class uns_disposable_LAW
			{
				fired = "_this spawn uns_weap_fnc_m72law";
			};
		};
	};
};

it's also important to introduce the sub class below EventHandlers, to not overwrite the default EventHandler and to make the event handler stackable.

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
2 hours ago, tuskohopkins said:

I made a script which adds a "HandleDamage" eventHandler and some code in it to a unit.

I want to add this script to every man class unit created either on mission init or spawned later. Actually on mission start I can do it with a simple '{} forEach allUnits', but I have troubel with the spawned ones.

I think it would be the best to add the code to every units init field. As I understand the eventHandler init should do the trick but I dont really understand how shold I set it up.

 

Of course I would put run a 'fnc_somescript.sqf', but first I just tried to display a text for testing purposes.

Could someone explain how this should work? Or show alterntive ways to run script for every man created.

 

 

The scripted alternative way is not difficult:

 

[] spawn {

  while {true} do {

    _allUnits = allUnits select { isNil {_x getVariable "MGI_passedEH"} };

    { _x addEventHandler ["handleDamage", {< your code>}];

      _x setVariable ["MGI_passedEH",true] } foreach _allUnits;

    uiSleep 1;
  };

};

 

It's easy to make a "black list", for example all edited OPFOR units (present in Eden):

Just add before:

{_x setVariable ["MGI_passedEH",true]} forEach (allUnits select {side _x == EAST});

 

If you spawn units on different PC (headless client or local script), make the variable public (add true in setVariable as 3rd param).

 

      

 

  • Like 1

Share this post


Link to post
Share on other sites

Hi TeTeT, pierremgi

 

Thanks for the quick replies! I will try both versions.

I was already thinking about a do while endless loop but I tought running the script on init would be more elegant.

 

I will let you know how it worked out.

 

Regards.
 

Share this post


Link to post
Share on other sites

Hi,

 

I have tried both methods.

The loop version works perfectly.

But the Init version has a some weird problems:

1. The called script runs for even snakes, rabbits etc. Can it be limited to only Humans?

2. I tried to call this script

this addEventHandler ["HandleDamage", {somecode here...}];

But it does not work with 'this' parameter. After a couple of hours of testing I managed to come up with this workaround:

currentUnit = _this select 0;
currentUnit addEventHandler ["HandleDamage", {somecode here .. }];  

Why 'this' param doesn't work? What is in it? If I tried to show it's content, it writes: 'any'

 

Thanks.

Share this post


Link to post
Share on other sites

About this vs _this:

To be honest after reading the link I am still not not sure what is in the 'this' variable in my case. But my mod works anyways. I just wanted to understand better, how it works.

 

The Animal problem seems to be solwed with this modification to the code:

class CfgVehicles
{	
	class CAManBase
	{
		class EventHandlers
		{
			class Senki_mortalWounds
			{
				init = "call compile preProcessFile 'mortalWounds\functions\fnc_setUnconcius.sqf'";
			};
		};
	};
};

So thanks for helping me!

 

Maybe I will publish it after some finishing touches. Maybe I will try to add some sound, make it configurable and MP compatible.

 

By the way it is a lightweight mod, which simulates non instant mortal wounds. Units are affected in random chance based on hit location (head: 0%, body: 60%, arms/legs: 100%). Affected units will agonize on the ground befor they die after a random amount of time (30-120 sec), ot they can be finished off.


It is partly inspired by dam_injuredAI and the finale effects are somwhat similar (no code used from it, mine works with different methods), but my mortally Wounded victims are helpless. No way to save them, sorry (it is purely for immersion).

 

Regards.

  • Haha 1

Share this post


Link to post
Share on other sites

Roughly:  this,  is a variable pointing at the object (unit, logic) in its own init field. exception: ThisTrigger for trigger.

_this  is a "magic" variable referring to the parameter passed in a called or spawned code (imagine from outside to inside):

[lot of params] call {your code}

then:

_this = [lot of params] in your code.

 

Now, for event handlers (EH), _this (inside params) is implicit and depends on the nature of the EH. See BIKI.

 

So, when you write, in init field of a unit:

this addEventHandler ["AnimChanged", { params ["_unit", "_anim"]; }];

There are two scopes:

The visible this referring to an existing object.

the invisible _this, depending on EH, here an array of 2 params: the unit and its animation, The command params gives some local variable names you can change.

 

Now, if you write your EH in the class code, as an attribute of any potential vehicle of this class, not in a specific object in Editor, then you need to write _this outside the EH, and play with _this (params in fact) inside of it.

Share this post


Link to post
Share on other sites

@pierremgi

 

Thanks for the explanation. Now it's much clearer.

 

I got all the answers I needed, so I guess the topic can be closed.

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

×