Jump to content
Sign in to follow this  
Bart.Jan

Macros in functions

Recommended Posts

I want know something about macros. Can anybody give me several simple examples, please?

I found only one example, but it's too abstract for me.  confused.gif

Share this post


Link to post
Share on other sites

Thanks for the tip, but there is nothing about macros. I found nothing about macros in tutorials and in any function there.

Share this post


Link to post
Share on other sites

As far as I know a simple macro can be included when using preProcessFile on an sqf like this

#define PI 3.1415

{ _circumfence = PI*_d;};

if we look at this example you had:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">#define MOD(_X, _Y) ((_X % _Y) - (_X % _Y) % 1)<span id='postcolor'> we have #define macroName(arguments) (statement)

macroName is MOD and it can be inserted into your sqf just as any other function

(arguments) is _X and _Y

the arguments can be passed into the macro much like passing arguments into a function, you can have no arguments and just the (statement)

(statement) is ((_X % _Y) - (_X % _Y) % 1) and this is the body of the macro like the body of a function, the _X and _Y can be passed in when calling the macro through the defined arguments.

then you can simply call this in the sqf like a function in C, not like with preProcessed sqf files where you have to say call preProcessedName, but simply MOD(9,3).

_X becomes 9, _Y becomes 3 and htis line is executed:

((9 % 3) - (9 % 3) % 1)

Hope this makes some sense.

Share this post


Link to post
Share on other sites

Thanks. It's clear now. smile.gif

Just few another questions :

I can use math macros. example: #define plus(_x,_y) (_x+_y)

1a)Can I use any (non reserved) names for macro variables? example #define plus2(_frt,_pke,_hoho) (_frt+_pke+_hoho)

1b)I presume I can not use names of macro variables in the body of function. Am I right?

example: private ["_x","_frt"];

I can also use commands macros. examples:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

#define kill(_x) _x setDamage 1

#define minus2(_x,_y) [_x,_y] call loadFile "minus2.sqf"

...

kill(player);

_v=minus2(4,6);

...

<span id='postcolor'>

2a)Can I include somehow "minus2.sqf" into macro?

model example:

minus2.sqf

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

private ["_a","_b","_c"];

_a=_this select 0;

_b=_this select 1;

if (_a>=_b) then {_c=_a-_b} else {_c=_b-_a};

_c=2*_c;

_c

<span id='postcolor'>

transformed into:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

#define minus(_x,_y) if (_x>=_y) then {_z=_x-_y} else {_z=_y-_x};_z=2*_z;_z

<span id='postcolor'>

2b)Or definition must be one line code* with variables that are "defined" in arguments?

*If not, how compiler recognize end of definition?

Share this post


Link to post
Share on other sites

wow.gif9--></span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Bart.Jan @ April 15 2003,08wow.gif9)</td></tr><tr><td id="QUOTE">Thanks. It's clear now. smile.gif

Just few another questions :

I can use math macros. example: #define plus(_x,_y) (_x+_y)

1a)Can I use any (non reserved) names for macro variables? example #define plus2(_frt,_pke,_hoho) (_frt+_pke+_hoho)<span id='postcolor'>

I believe you can yes.

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE">

1b)I presume I can not use names of macro variables in the body of function. Am I right?

example: private ["_x","_frt"];<span id='postcolor'>

You CAN use the variable names also used in macro definitions. Do NOT use the name of the macro definition itself as a variable, unless you know exactly why and what you are doing. smile.gif

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE">I can also use commands macros. examples:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

#define kill(_x) _x setDamage 1

#define minus2(_x,_y) [_x,_y] call loadFile "minus2.sqf"

...

kill(player);

_v=minus2(4,6);

...

<span id='postcolor'><span id='postcolor'>

Yes of course, + - / are commands as well. The thing is you seem to be unable to define more than one statement in a macro... the good news is you can create a kind of macro this way and still pass in arguments:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">#define DIV(_X, _Y) {player setDamage _X;player setDammage _Y;_X*_Y;}

private ["_result","_x"];

_x =50;

_result = call DIV(10,3);

player sidechat format ["%1",_x];

_result;<span id='postcolor'>Result becomes 30 and of course the player is killed. smile.gif He would sidechat 50;

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE">2a)Can I include somehow "minus2.sqf" into macro?

model example:

minus2.sqf

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

private ["_a","_b","_c"];

_a=_this select 0;

_b=_this select 1;

if (_a>=_b) then {_c=_a-_b} else {_c=_b-_a};

_c=2*_c;

_c

<span id='postcolor'>

transformed into:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

#define minus(_x,_y) if (_x>=_y) then {_z=_x-_y} else {_z=_y-_x};_z=2*_z;_z

<span id='postcolor'><span id='postcolor'>

Again the one thing I can recommend for this if you have to use macros is : </span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">#define minus(_x,_y) {if (_x>=_y) then {_z=_x-_y;} else {_z=_y-_x;};_z=2*_z;_z;}<span id='postcolor'>

called with </span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">call minus (2,3);<span id='postcolor'>

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
Sign in to follow this  

×