Jump to content
Sign in to follow this  
Rafalski

#define function(X,Y) problem

Recommended Posts

r\common\functions.sqf

rSum = 
{
 _sum = (_this select 0) + (_this select 1);
 _sum 
};

#define RSUM(Y,Z)	([Y,Z] call rSum)

init.sqf

if ((!isServer) && (player != player)) then { waitUntil {player == player};};
waitUntil {!isNil "BIS_MPF_InitDone"}; 
waitUntil {BIS_MPF_InitDone}; 

#include "r\common\functions.sqf"

hint "init done";

if(true) exitWith {}; 

test.sqf

_a = 2;
_b = 3;

_c= RSUM(_a,_b);

hint format ["%1",_c];

nul= [] execVM test.sqf and always error ..._c= RSUM # (_a,_b); ....

btw _c= [_a,_b] call rSum; works OK.

why RSUM(_a,_b) does not work ???? looks like RSUM(Y,Z) is not defined ?

Share this post


Link to post
Share on other sites

Defines are not like global variables. You need to include the file with defines everywhere you want to use the define.

So split your SQF functions from your preprocessor defines, and include the define file in every file you want to use the define.

Also there's really no need for "if(true) exitWith {};" at the end of scripts.

Share this post


Link to post
Share on other sites

As SickBoy said #include before you need it. But it is a bit of an odd implementation. Why not do it one of the following ways:

As a macro that replaces every occurence of RSUM within your scripts with this:

#define RSUM(y,z) (y + z)

So the following:

_Value1=RSUM(1,2)+RSUM(3,4);
_Value2=RSUM(5,6)+RSUM(7,8);

Gets pre-processed into this when you run your script:

_value1=(1+2)+(3+4);
_value1=(5+6)+(7+8);

or #define it as a function\macro and #include it when you need it:

#define _rsum {(_this select 0)+(_this select 1)}

So the following:

_Value1=([1,2] call _rsum)+([3,4] call _rsum);
_Value2=([5,6] call _rsum)+([7,8] call _rsum);

Gets pre-processed into this when you run your script:

_Value1=([1,2] call {(_this select 0)+(_this select 1)})+([5,6] call {(_this select 0)+(_this select 1)});
_Value2=([5,6] call {(_this select 0)+(_this select 1)})+([7,8] call {(_this select 0)+(_this select 1)});

Or leave it as a local function and #include it when you need it:

_rsum={(_this select 0)+(_this select 1)}

So the following:

_Value1=([1,2] call _rsum)+([3,4] call _rsum);
_Value2=([5,6] call _rsum)+([7,8] call _rsum);

This gets pre-processed into this when you run your script:

_value1=([1,2] call _rsum)+([3,4] call _rsum);
_value2=([5,6] call _rsum)+([7,8] call _rsum);

The point being #define just replaces stuff. So while your script is easy to read, when it's loaded there is lots of duplicate code. #including the last example means the content of _rsum is defined once and the code isn't replicated multiple times.

It really depends what your doing at the time and the only difference is how much code you have loded into memory. For a simple function like your example, I would use a macro (the first example). Anything bigger and more complex, I would use the last example and #include it when I needed it. Unless I wanted to access the function from the editor or multiple scripts, then I would delcare it as a global variable and be done with it.

Edited by UNN

Share this post


Link to post
Share on other sites

Thank you, for more explanation, we learn every day :)

(My example was simple, because I just want to start '#define' to work.

Why I need it ?

I wanted to use simpler way (visually) to call functions:

instead of _a= [x,y] call rSum;

much better to see _a = RSUM(x,y);

but As I know now that all defines (of functions) must be included in every script maybe I stay with _a= [x,y] call rSum; ?

Edited by Rafalski

Share this post


Link to post
Share on other sites
I wanted to use simpler way (visually) to call functions

I see, what threw me was you declared you function as a global variable, then referenced it via a #define.

So yeah, from a visual point of view, a macro is the way to go.

but As I know now that all defines (of functions) must be included in every script maybe I stay with _a= [x,y] call rSum; ?

It's not that bad though. You could create two #include files. One that contains your global functions (if you want to stick with those). And another that holds all your #defines.

So in an init.sqf for example, you could have:

#include "functions.hpp"
#include "defines.hpp"

_value=RSUM(1,2);

functions.hpp:

rSum = 
{
 private ["_sum"];

 _sum = (_this select 0) + (_this select 1);
 _sum 
};

defines.hpp:

#define RSUM(Y,Z)	([Y,Z] call rSum)

You only need to include functions.hpp into your init script. Once a global is defined you can use it pretty much anywhere if Arma. After that any other scripts just need to #include defines.hpp. For example:

init.sqf:

#include "functions.hpp"
#include "defines.hpp"

_value=RSUM(1,2);

_sP=[3,4] execVM "anotherscript.sqf";

anotherscript.sqf:

#include "defines.hpp"

_value=RSUM(_this select 0,_this select 1);

So as long as you include defines.hpp in all your scripts, you don't have to do much else. But unless you really need rSum to be global? You could declare it as a local function and put it in #defines with everything else:

defines.hpp:

_rSum = 
{
 private ["_sum"];

 _sum = (_this select 0) + (_this select 1);
 _sum 
};

#define RSUM(Y,Z)	([Y,Z] call _rSum)

Or just.

defines.hpp:

#define RSUM(y,z) (y + z)

Again, as long as you #include defines.hpp you can easily add your functions to any script, only you won't be able to access them directly from the editors init boxes e.t.c There are a few variations to choose from and some of it is down to personnel preference.

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  

×