Jump to content

Recommended Posts

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 :smile_o:

 

 

Share this post


Link to post
Share on other sites

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

@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

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

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
16 hours ago, gc8 said:

call call compile _fnName;

 

call (missionNamespace getVariable [_fnName, {hint format ["Ooops, function '%1' is undefined", _fnName]}]);

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
13 hours ago, 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

 

13 hours ago, 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
12 hours ago, 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 :smileee:

Share this post


Link to post
Share on other sites
57 minutes ago, killzone_kid said:

 

call (missionNamespace getVariable [_fnName, {hint format ["Ooops, function '%1' is undefined", _fnName]}]);

 

That works, thanks! :thumbsup:

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

×