Jump to content
gc8

passing arguments to string of code

Recommended Posts

Hi

I have a question 

 

suppose I want to make a call like this when the expression is called:  group call test;

 

I try to put something like this in order to pass the group as argument to test function:

 

["Test", [2], "", -5, [["expression", format["%1 call test;", _group] ]], "1", "1", ""] // error

 

but that wont work , it will give error message.

 

and putting quation marks wont work either :

 

["Test", [2], "", -5, [["expression", format[" '%1' call test;", _group] ]], "1", "1", ""] // group becomes string

 
so how do you properly pass group to a string of code?
 
thanks

Share this post


Link to post
Share on other sites

I don't know if there is a true proper way, but another alternative could be simply:

str(_group)

Share this post


Link to post
Share on other sites

format["%1", _group] will return something like "B: Alpha-1-1". I believe str _group has the same behavior.

 

You can convert vehicle/unit variable names into string using vehicleVarName, so a workaround would be.

format["(group %1) call test", vehicleVarName ((units _group) select 0)]

Share this post


Link to post
Share on other sites

 

format["%1", _group] will return something like "B: Alpha-1-1". I believe str _group has the same behavior.

 

You can convert vehicle/unit variable names into string using vehicleVarName, so a workaround would be.

format["(group %1) call test", vehicleVarName ((units _group) select 0)]

 

It seems like that aporach would require to set the vehicle var name for all groups... 

Share this post


Link to post
Share on other sites

I don't know if there is a true proper way, but another alternative could be simply:

 

str(_group)

 

if you could convert string (group name) to actual group object that would work.

 

My concern was also that when group object is converted to string the string is not always unique.

Share this post


Link to post
Share on other sites

Can you explain as to why it needs to be a string in the first place?

Share this post


Link to post
Share on other sites

Can you explain as to why it needs to be a string in the first place?

 

because the 

 

["Test", [2], "", -5, [["expression", format[" '%1' call test;", _group] ]], "1", "1", ""]

 

uses string as expression that is executed. 

Share this post


Link to post
Share on other sites

In multiplayer you can use

netId _group

 

if its only for multiplayer I probably will use my own unique ID system for the groups.

Share this post


Link to post
Share on other sites

because the 

 

["Test", [2], "", -5, [["expression", format[" '%1' call test;", _group] ]], "1", "1", ""]

 

uses string as expression that is executed. 

What is it? And why does it not take normal arguments array for the expression? If this array is used in your code you will probably want to rewrite it (code) to support array form of something like:

["Test", [2], "", -5, [["expression", [_group], "_this select 0 call test" ]], "1", "1", ""]

Share this post


Link to post
Share on other sites

 

What is it? And why does it not take normal arguments array for the expression? If this array is used in your code you will probably want to rewrite it (code) to support array form of something like:

["Test", [2], "", -5, [["expression", [_group], "_this select 0 call test" ]], "1", "1", ""]

 

the code is for commanding menu

 

so I just have to cope with it.

Share this post


Link to post
Share on other sites

Then why are you using format? Why cant you just write

"_group call test"

 

_group does not exist in the scope when the code is called...

Share this post


Link to post
Share on other sites

_group does not exist in the context when the code is called...

check the edit. group _caller or group _target maybe?

Share this post


Link to post
Share on other sites

gc8, from the very link you gave us:

Following arguments are passed into the expression field:

[caller,pos,target,is3D,ID]

  • callerObject - unit which called the item, usually player
  • posArray in format Position - cursor position
  • targetObject - cursor target
  • is3DBoolean - true when in 3D scene, false when in map
  • IDString - item ID as returned by BIS_fnc_addCommMenuItem function

So, I guess,

"(group (_this select 0)) call test"

Share this post


Link to post
Share on other sites

the group im passing in is different from the callers group etc.

 

but no worries if I just pass the group ID as number it will work just fine.

 

eg:

["Test", [2], "", -5, [["expression", format["%1 call test;", _group call getGroupID ] ]], "1", "1", ""]

test =
{
_group = _this call getGroupByID;
};

Share this post


Link to post
Share on other sites

 

the group im passing in is different from the callers group etc.

 

but no worries if I just pass the group ID as number it will work just fine.

 

eg:

["Test", [2], "", -5, [["expression", format["%1 call test;", _group call getGroupID ] ]], "1", "1", ""]

test =
{
_group = _this call getGroupByID;
};

 

You can also setVariable on caller or target with required group and then retrieve it with getVariable

  • Like 1

Share this post


Link to post
Share on other sites

You can also setVariable on caller or target with required group and then retrieve it with getVariable

 

actually I cant because the com menu has many lines and all different group as argument.

Share this post


Link to post
Share on other sites

This is a classic problem that has existed in some form since OFP. The string version of a group will have a space in it, breaking everything, so you cannot "format it in" like you can with other data types. It sounds like you've already found a workaround for your particular problem, but for the benefit of others, a quick way to get around this is by using the group leader:

format ["(group %1) call test;",leader _group] 

This still winds up using the group, but because you are formatting in an object (the group leader) rather than the group itself, it doesn't break.

Share this post


Link to post
Share on other sites

This is a classic problem that has existed in some form since OFP. The string version of a group will have a space in it, breaking everything, so you cannot "format it in" like you can with other data types. It sounds like you've already found a workaround for your particular problem, but for the benefit of others, a quick way to get around this is by using the group leader:

format ["(group %1) call test;",leader _group] 

This still winds up using the group, but because you are formatting in an object (the group leader) rather than the group itself, it doesn't break.

 

If I recall the group leader may return null if the leader has just died, so there's a change this approach wont work.

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

×