Jump to content
Sign in to follow this  
nuxil

#define is it local to scripts its defined in ?

Recommended Posts

Hi all.

i am trying to use #define on some contant numbers. idc numbers to be precise.

however i am expecting some problems with it.

it seams its local to the scripts its defined in. is this so ?

i wanted to avoid using global vars for my idc. but it seams like i have to. unless someone can explain whats going on with it.

here is a short example!

init.sqf

#define MYDEFINE "it works"

call compile preprocessFileLineNumbers "fnc.sqf";

sleep 3;
call fnc1;

fnc.sqf

fnc1 = 
{
hint str(MYDEFINE);
};

having #define in init.sqf does not work.

if i move the #define to the fnc.sqf file. it works.

basicly what i have to do now is have #define in each scripts that needs the defined thing. this becomes verry ugly in scripting maners..

have i missunderstood the function of #define ?

for what i understand from the wiki preprocessFileLineNumbers and preprocessFile supports macros defined with #define.

i have not much experiance with using #define exept from putting them in my *.cpp file to define idc/types etc etc.

any help explaining this is appreciated.

Thanks!

Share this post


Link to post
Share on other sites

Prior to executing the script the preprocessor takes every occurrence of 'MYDEFINE' and replaces it with '"it works"'. You could compare it to text editors' Replace function.

Share this post


Link to post
Share on other sites

The reason it doesn't work is because #define is a pre-processor command; ie before the game itself truly compiles the code.

For example:

#define MYVAR "test"

if ("test" == MYVAR) exitwith {};

The game engine during compilation see's this as:

if ("test" == "test") exitwith {};

Therefore, with your code:

#define MYDEFINE "it works"

call compile preprocessFileLineNumbers "fnc.sqf";

sleep 3;
call fnc1;

Becomes:

call compile preprocessFileLineNumbers "fnc.sqf";

sleep 3;
call fnc1;

The code is then executed by the game engine, and becomes the following:

call {fnc1 = {hint str(MYDEFINE)}};

sleep 3;
call {hint str(MYDEFINE)};

The compiler has already made its pre-processor run through, long before this, so therefore MYDEFINE will not work.

Try the following instead:

init.sqf

#define MYDEFINE "it works"

MYDEFINE call compile preprocessFileLineNumbers "fnc.sqf";

sleep 3;
call fnc1;

fnc.sqf

fnc1 = compile format ["hint %1", _this];

Therefore:

call fnc1 == call {hint "it works"};

Edited by Rommel

Share this post


Link to post
Share on other sites

Yeah, they must be defined at each script-file. If you want to use the same defines at many places you can put them in a file, let's say defines.h, then at the start of each script you want to use them put:

#include "defines.h"

Edited by Fincuan

Share this post


Link to post
Share on other sites

Thank you guys :)

it makes sense now

Share this post


Link to post
Share on other sites

>Yeah, they must be defined at each script-file. If you want to use the same defines at many places you can put them in a file

But beware that include paths are only relative to the current file. Vote for http://dev-heaven.net/issues/show/5121 if you want to change that.

Share this post


Link to post
Share on other sites

i putted the defines in a def.h file. works fine.

anyway. just one more thing,

since i need constant numbers acceseble in several script.will #define be the best way ?

you may say.i will save memory by using it, but will i actually?

these defines needs to be stored in memory anyway.

i would get around this problem with including the deh.h file in every script that needs the constans if i use global vars. and the global vars only has to be init once, therfor loaded once into the memory like #define.

i can see the benefit by using define with functions and such, but not really with constant numbers. or am i wrong

so. to define or not to define ?

Share this post


Link to post
Share on other sites

Don't worry about the memory usage- it's completely irrelevant. The main advantage of using a single include file is that you only change the number once in the header file to affect all the dependent scripts. More importantly, they can't get out of step with each other.

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  

×