Jump to content
Lala14

Help finding an alternative to using a #define in #include

Recommended Posts

Basically what I want to do is currently not possible through what is said on the wiki. I've also tried some alternatives which so far have proven to be more complicated than what it really should be. I'm wondering if someone has found a different alternative to the problem.
The problem
I basically want to use the same set of scripts over and over again however with passing different arguments that will be located in a file via #include.
The current working solution I have contains 3 sets of the same script with the #include path changed.
e.g. of the problem


macros.hpp
#define planeMaxLength 12.1
#define planeMaxWidth 4.1
#define planeMaxHeight 4.3
#define planeMaxWeight 35900
 
script1.sqf
#include "macros.hpp"


e.g. of one of my other solutions


defines.h
#define planeMaxLength 12.1
#define planeMaxWidth 4.1
#define planeMaxHeight 4.3
#define planeMaxWeight 35900
[planeMaxLength, planeMaxWidth, planeMaxHeight, planeMaxWeight]
 
script1.sqf

_arr = call compile preprocessFileLineNumbers "defines.h";
_arr params [["_planeMaxLength", 0], ["_planeMaxWidth", 0], ["_planeMaxHeight", 0], ["_planeMaxWeight", 0]];


However, as you can if I was to add anything else to defines.h I would need to add in the new variable to every script which is quite time consuming when you have 15.
Any suggestions?

Share this post


Link to post
Share on other sites

However, as you can ...

Nope I can't!

defines.h
#define planeMaxLength 12.1
#define planeMaxWidth 4.1
#define planeMaxHeight 4.3
#define planeMaxWeight 35900
#define NEWVAR 123
[planeMaxLength, planeMaxWidth, planeMaxHeight, planeMaxWeight, NEWVAR]
 
script1.sqf

_arr = call compile preprocessFileLineNumbers "defines.h";
_arr params [["_planeMaxLength", 0], ["_planeMaxWidth", 0], ["_planeMaxHeight", 0], ["_planeMaxWeight", 0]];

No problem there ^^^^. params can parse input array that is larger or smaller than required size

Share this post


Link to post
Share on other sites
I basically want to use the same set of scripts over and over again however with passing different arguments that will be located in a file via #include.

 

Sounds like you should just write a function to handle that. That way params are just set when you call the function.

Share this post


Link to post
Share on other sites

Just gonna hijack this thead for a quick question: What exactly is the advantage of using #define instead of defining a variable the usual way and how is the locality?

 

From this page, https://community.bistudio.com/wiki/PreProcessor_Commands#.23defineI understand that it makes sense to use that in configs where one can't assign variables the usual way, but why in sqf?

Share this post


Link to post
Share on other sites

Nope I can't!

defines.h
#define planeMaxLength 12.1
#define planeMaxWidth 4.1
#define planeMaxHeight 4.3
#define planeMaxWeight 35900
#define NEWVAR 123
[planeMaxLength, planeMaxWidth, planeMaxHeight, planeMaxWeight, NEWVAR]
 
script1.sqf

_arr = call compile preprocessFileLineNumbers "defines.h";
_arr params [["_planeMaxLength", 0], ["_planeMaxWidth", 0], ["_planeMaxHeight", 0], ["_planeMaxWeight", 0]];

No problem there ^^^^. params can parse input array that is larger or smaller than required size

 

Yes, however, what I was trying to say was that I would also need to change _arr in each of the files(currently there is 9). I have since anyway pursed this method for the time being.

Sounds like you should just write a function to handle that. That way params are just set when you call the function.

 

They're all functions, I just said scripts.

Just gonna hijack this thead for a quick question: What exactly is the advantage of using #define instead of defining a variable the usual way and how is the locality?

 

From this page, https://community.bistudio.com/wiki/PreProcessor_Commands#.23defineI understand that it makes sense to use that in configs where one can't assign variables the usual way, but why in sqf?

There only a small advantage I guess, less lines of code. Basically what you do is you have a file with all the definitions and then you include it into your file and thus everything is already defined and waiting to be used.

Share this post


Link to post
Share on other sites

Just gonna hijack this thead for a quick question: What exactly is the advantage of using #define instead of defining a variable the usual way and how is the locality?

 

From this page, https://community.bistudio.com/wiki/PreProcessor_Commands#.23defineI understand that it makes sense to use that in configs where one can't assign variables the usual way, but why in sqf?

 

#define == replace

#define abc 123

abc will be replaced with 123 everywhere in current document before everything, this is why it is called preprocessor. 

Share this post


Link to post
Share on other sites

This topic is clarifying it, too.

 

#define is it local to scripts its defined in ?

 

I think an Advantage is that if u use many constants in different scripts then u only Need to define it in a Header file and include this into ur scripts instead of setting all those constants to variables.

After that u only need to Change one file if u think u have to Change a value. you dont have to Change it in all scripts.

  • Like 1

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

×