gc8 981 Posted March 16, 2020 Hi I ran to this weird coding dilemma which I basically solved but the result was so bizarre I wanted to ask you guys if you knew a better/correct way of doing this. Here's my code: myTestFnABC = { systemchat "Teeeest!"; }; // Construct the above function name _test = "ABC"; private _fnName = format["myTestFn%1", _test]; if(!isNil _fnName) then // Does the variable exist? { // Weird two calls code call call compile _fnName; }; As you can see I am construction the function name string and then compiling it and calling twice to get the myTestFnABC executed. Surely there has to be other way than two calls? (Without changing the fact that I need the function name constructed like above) thx for your thoughts Share this post Link to post Share on other sites
marceldev89 89 Posted March 16, 2020 I guess you could do private _fnName = { private _thingy = param [0]; call compile format["myTestFn%1", _thingy]; }; ["ABC"] call _fnName; 😂 But yes, it's a bit weird. Share this post Link to post Share on other sites
gc8 981 Posted March 16, 2020 @marceldev89 I think that code wont work and myTestFnABC is not called. Hence I ended up doing two calls Share this post Link to post Share on other sites
target_practice 163 Posted March 16, 2020 You could try including the first call in the formatted string to clean it up a bit, but otherwise, due to how compile works, I don't think there's any way around it; compile returns a code object, wrapped in parentheses, so you need to run that code first to return the value of the variable. Also, isNil takes a string as an argument, not a variable. Share this post Link to post Share on other sites
pierremgi 4930 Posted March 16, 2020 IMHO, a string like your _fncName is not a stringyfied code. If you want some result using a code, you need to use at least an operator, a command, or a function applied to variable. Share this post Link to post Share on other sites
killzone_kid 1333 Posted March 17, 2020 On 3/16/2020 at 3:22 PM, gc8 said: call call compile _fnName; call (missionNamespace getVariable [_fnName, {hint format ["Ooops, function '%1' is undefined", _fnName]}]); 1 1 Share this post Link to post Share on other sites
gc8 981 Posted March 17, 2020 On 3/16/2020 at 6:59 PM, target_practice said: You could try including the first call in the formatted string to clean it up a bit, but otherwise, due to how compile works, I don't think there's any way around it; compile returns a code object, wrapped in parentheses, so you need to run that code first to return the value of the variable. Yep that's what is happening. i was also thinking the same, have the other call in string to clean it up a bit On 3/16/2020 at 6:59 PM, target_practice said: Also, isNil takes a string as an argument, not a variable. I'm giving it a string variable, that worked for me Share this post Link to post Share on other sites
gc8 981 Posted March 17, 2020 On 3/16/2020 at 7:51 PM, pierremgi said: IMHO, a string like your _fncName is not a stringyfied code. If you want some result using a code, you need to use at least an operator, a command, or a function applied to variable. that a bit over my head Share this post Link to post Share on other sites
gc8 981 Posted March 17, 2020 On 3/17/2020 at 7:51 AM, killzone_kid said: call (missionNamespace getVariable [_fnName, {hint format ["Ooops, function '%1' is undefined", _fnName]}]); That works, thanks! Share this post Link to post Share on other sites