Jump to content
M1ke_SK

[help] create function (variable) from string

Recommended Posts

I have many eventHandlers re-writed

_unit addEventHandler["fired", myTag_fnc_event_handler_fired];
_unit addEventHandler["HandleDamage", myTag_fnc_event_handler_handle_damage];
_unit addEventHandler["hit", myTag_fnc_event_handler_hit];

Trying to create

{
    _unit removeAllEventHandlers _x;
    _unit addEventHandler[_x, myTag_fnc_event_handler_{toLower(_x)}];
}
forEach ["fired", "HandleDamage", "hit"];

How to create function name from string?

Share this post


Link to post
Share on other sites

Rather than continually adding and removing eventHandlers, why not control their logic via a variable?

 

Otherwise: 

// calls fn_whatever
call compile format["call fn_%1", "whatever"];

Share this post


Link to post
Share on other sites

 

How to create function name from string?

 

You can store in variables functions instead of strings:

HandlerTable = [
	["hit",				{systemChat "Help, i am hit!"}],
	["fired",			{call Fn_MyEventHandler}],
	["HandleDamage",			Fn_HandleDamage]
];

	
private _unt = player;
private _typ = "hit";
private _rec = {if (_x select 0  == _typ) exitWith {_x}} forEach HandlerTable;
if (isNil {_rec}) then {
	// handler record not found
} else {
	private _fnc = _rec select 1; // here _fnc = {systemChat "Help, i am hit!"}
	_unt removeAllEventHandlers _typ;
	_unt addEventHandler[_typ, _fnc];
};

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

×