champ-1 40 Posted April 4, 2014 How to pass local variable into EventHandler? I can't use Public and Global variables, because this script gonna run in multiple instances. Or can I? Share this post Link to post Share on other sites
zapat 56 Posted April 4, 2014 (edited) What Eventhandlers are we talking about? I ususally tie the vars (by setvariable) to the unit/object the eventhandler is called on. There is some OOP present in sqf as global variables are nothing more than missionNamespace setVariable. If you consider this it'll be easy to use the same on objects. I mean you are using setVariable already when you use global and public variables only on a static global object, not a dynamic one. Edited April 4, 2014 by zapat Share this post Link to post Share on other sites
champ-1 40 Posted April 4, 2014 Something like this: private ["_veh","_req_rank","_req_rating","_rank_unit"]; _veh = _this select 0; _req_rank = toUpper (_this select 1); _req_rating = 500; _veh addEventHandler ["GetIn",{ _unit = _this select 2; hint str [_req_rank,_req_rating]; }]; This script gonna run from init of multiple vehicles so it's gonna overwrite own public and global variables. Share this post Link to post Share on other sites
Dwarden 1125 Posted April 4, 2014 event handlers accept string as well as code so he can pass local variables within the string to EH. _veh addEventHandler ["GetIn","some code" + _var1 + "some more code" + _var2 + "more code"]; 1 Share this post Link to post Share on other sites
champ-1 40 Posted April 4, 2014 What if I have string in that event handler? How to deal with strings on top of strings? Double quotes or something? Share this post Link to post Share on other sites
Dwarden 1125 Posted April 4, 2014 _var1 = "lol"; _var2 = 10; _veh addEventHandler ["GetIn", format ["a = 'string'; b = '%1', c = %2;", _var1, _var2]]; or _veh addEventHandler ["GetIn","a = 'string'; b = '" + _var1 + "'; c = " + _var2 + ";"]; 1 Share this post Link to post Share on other sites
brians200 51 Posted April 4, 2014 ' "string1" "string2" ' Share this post Link to post Share on other sites
champ-1 40 Posted April 4, 2014 Oh yeah, it works. Though it became quite a bit more complicated. I wish it could be possible just pass arguments through brackets, like everywhere else. Anyway, thank you for the quick answer, guys. Share this post Link to post Share on other sites
f2k sel 164 Posted April 5, 2014 I must be missing something , how would you observe the result in the code _var1 = "lol"; _var2 = 10; _veh addEventHandler ["GetIn", format ["a = 'string'; b = '%1', c = %2;", _var1, _var2]]; or _veh addEventHandler ["GetIn","a = 'string'; b = '" + _var1 + "'; c = " + _var2 + ";"]; Share this post Link to post Share on other sites
igneous01 19 Posted April 5, 2014 I must be missing something , how would you observe the result in the code _var1 = "lol"; _var2 = 10; _veh addEventHandler ["GetIn", format ["a = 'string'; b = '%1', c = %2;", _var1, _var2]]; or _veh addEventHandler ["GetIn","a = 'string'; b = '" + _var1 + "'; c = " + _var2 + ";"]; the addEventHandler will call compile the string, when passing _var1, _var2 to format (or concatenation) it treats it similarly to that of a macro: "b = _var1" becomes "b = lol" however in dwardens example lol will be returned without quotes (implying that a variable called lol exists and can be copied from) this would make it format as a string assignment: format ["b = %1;", str _var1]; It will basically take a snapshot of the local variable's value and substitute it in the code. however when dealing with objects/units this is not recommended as: format ["b = %1;", _myGrp]; will replace it as: b = O Alpha 1-1; which will fail to compile/execute Share this post Link to post Share on other sites
f2k sel 164 Posted April 5, 2014 Thanks I get it now , example for anyone else _var1 = 3; _var2 = 1; player addeventhandler ["fired", format ["hint str (%1 + %2);", _var1,_var2]]; Result 4 Thanks for the info about object being passed, that confirms what I found the other day when trying to use it to make a marker with the same name. I did sort it another way. Share this post Link to post Share on other sites