Jump to content
Sign in to follow this  
tpw

Addon configuration by hpp - how to use arrays in configs?

Recommended Posts

Hi all

Recently I've been working on a house lights addon: http://forums.bistudio.com/showthread.php?134139-TPW-HOUSELIGHTS-automatic-house-light-addon

For user configuration I'm using CBA to read variables out of an external hpp file.

In my config.cpp

class Extended_Init_EventHandlers
{
class All
{
	TPW_HOUSELIGHTS_Init = "if (isNil ""TPW_HOUSELIGHTS_Init_Var"") then {TPW_HOUSELIGHTS_Init_Var = [] execVM ""\TPW_HOUSELIGHTS_103\init.sqf"";};";
};
};

class TPW_HOUSELIGHTS_Key_Setting
{
#include "\userconfig\TPW_HOUSELIGHTS\TPW_HOUSELIGHTS.hpp"
};

In my init.sqf

_movedist = getNumber(configFile>> "TPW_HOUSELIGHTS_Key_Setting" >> "TPW_MOVEDISTANCE");
_housedist = getNumber(configFile>> "TPW_HOUSELIGHTS_Key_Setting" >> "TPW_HOUSEDISTANCE");
_houseperc = getNumber(configFile>> "TPW_HOUSELIGHTS_Key_Setting" >> "TPW_HOUSEPERC");
_lightarray = getNumber(configFile>> "TPW_HOUSELIGHTS_Key_Setting" >> "TPW_LIGHTARRAY");


waitUntil { !isNull player };
nul = [_movedist,_housedist,_houseperc,_lightarray] execVM "\TPW_HOUSELIGHTS_103\tpw_houselights.sqf";

As the addon has become more complicated so has the amount of configuration. Rather than define zillions of variables in the hpp, I'd prefer to use an array.

// TPW HOUSELIGHTS SETTINGS

TPW_MOVEDISTANCE = 80; 
TPW_HOUSEDISTANCE = 100; 
TPW_HOUSEPERC = 50;
TPW_LIGHTARRAY = [[255,127,24,40,20,60,10],[255,127,24,40,20,60,10],[255,127,24,40,20,60,10],[255,127,24,40,20,60,10],[255,200,100,40,20,20,5],[255,200,100,40,20,20,5],[100,200,255,40,20,100,30]];

The problem is that whilst the first three variables are read no worries, TPW_LIGHTARRAY is not. It always just ends up as 0.

I'm sure I'm making a fundamental and stupid mistake here. Anyone care to enlighten me as to how to read an array of variables into an addon?

Share this post


Link to post
Share on other sites

Try using them like this....

In your config...

#include "\userconfig\TPW_HOUSELIGHTS\TPW_HOUSELIGHTS.hpp" 

In your code...

nul = [TPW_MOVEDISTANCE,TPW_HOUSEDISTANCE,TPW_HOUSEPERC,TPW_LIGHTARRAY] execVM "\TPW_HOUSELIGHTS_103\tpw_houselights.sqf";

Edited by twirly

Share this post


Link to post
Share on other sites

Nah, no dice twirly. That doesn't pass any of the bloody variables.

The problem is that the hpp is not actually executed like an sqf, so the variables defined in it don't become public.

Thanks anyway mate.

Try using them like this....

In your config...

#include "\userconfig\TPW_HOUSELIGHTS\TPW_HOUSELIGHTS.hpp" 

In your code...

nul = [TPW_MOVEDISTANCE,TPW_HOUSEDISTANCE,TPW_HOUSEPERC,TPW_LIGHTARRAY] execVM "\TPW_HOUSELIGHTS_103\tpw_houselights.sqf";

Share this post


Link to post
Share on other sites

I think you need to define them in the .hpp...

#define TPW_MOVEDISTANCE 80
#define TPW_HOUSEDISTANCE 100
#define TPW_HOUSEPERC 50
#define TPW_LIGHTARRAY [[255,127,24,40,20,60,10],[255,127,24,40,20,60,10],[255,127,24,40,20,60,10],[255,127,24,40,20,60,10],[255,200,100,40,20,20,5],[255,200,100,40,20,20,5],100,200,255,40,20,100,30]]

Share this post


Link to post
Share on other sites

Hmm, didn't get that to work either.

I think you need to define them in the .hpp...

#define TPW_MOVEDISTANCE 80
#define TPW_HOUSEDISTANCE 100
#define TPW_HOUSEPERC 50
#define TPW_LIGHTARRAY [[255,127,24,40,20,60,10],[255,127,24,40,20,60,10],[255,127,24,40,20,60,10],[255,127,24,40,20,60,10],[255,200,100,40,20,20,5],[255,200,100,40,20,20,5],100,200,255,40,20,100,30]]

Share this post


Link to post
Share on other sites

Yeah...sorry... I missed the part about the init.sqf. Wouldn't have worked... but the defines should have with your original code I think.

---------- Post added at 07:55 PM ---------- Previous post was at 07:53 PM ----------

You could always ask in the Configs and Scripting forum :)

Share this post


Link to post
Share on other sites

Instead of running the Extended_Init_Eventhandler for class All (which means, every object created in the game will run the init) CBA has some fine possibilities to run a script only once.

In your case you can use the Extended_PostInit_EventHandlers:

class Extended_PostInit_EventHandlers {

[left]    class TPW_HOUSELIGHTS {[/left]


       clientInit = "call compile preprocessFileLineNumbers '\TPW_HOUSELIGHTS_103\init.sqf'";
   };
};

You have to remove waitUntil {!isNull player} from the init.sqf script file.

The advantage is, initialization is done only on a client and like already mentioned just once, and because I've replaced execVM with call compile preprocess... init is done in the same frame (always important for eventhandlers, try to avoid using execVM or spawn in eventhandlers whenever possible). PostInit means, the eventhandler runs after all inits are done (including init.sqf) and the player object is initialized.

clientInit tells XEH to execute the EH code line only on a client.

Xeno

Edited by Xeno

Share this post


Link to post
Share on other sites

You know I'm so stupid I nearly tried the command get_______!

Anyway, thanks for that. I modified my init.sqf

_lightarray = getArray(configFile>> "TPW_HOUSELIGHTS_Key_Setting" >> "TPW_LIGHTARRAY");

And even though the hpp has the line with the array of arrays

TPW_LIGHTARRAY = [[255,127,24,40,20,60,10],[255,127,24,40,20,60,10],[255,127,24,40,20,60,10],[255,127,24,40,20,60,10],[255,200,100,40,20,20,5],[255,200,100,40,20,20,5],[100,200,255,40,20,100,30]]; 

it seems like only an empty array is actually passed on to the script. All the others number variables are fine.

Myke;2152834']getNumber get's a number.

get_____ get's a Array.

Try getArray if anything else fails. ;)

Thanks very much Xeno. I understood about 17% of that' date=' but will definitely have a look once I can get the actual array passing to work :)

Instead of running the Extended_Init_Eventhandler for class All (which means, every object created in the game will run the init) CBA has some fine possibilities to run a script only once.

In your case you can use the Extended_PostInit_EventHandlers:

class Extended_PostInit_EventHandlers {

[left]    class TPW_HOUSELIGHTS {[/left]


       clientInit = "call compile preprocessFileLineNumbers '\TPW_HOUSELIGHTS_103\init.sqf'";
   };
};

You have to remove waitUntil {!isNull player} from the init.sqf script file.

The advantage is, initialization is done only on a client and like already mentioned just once, and because I've replaced execVM with call compile preprocess... init is done in the same frame (always important for eventhandlers, try to avoid using execVM or spawn in eventhandlers whenever possible). PostInit means, the eventhandler runs after all inits are done (including init.sqf) and the player object is initialized.

clientInit tells XEH to execute the EH code line only on a client.

Xeno

EDIT:

So even after changing config.cpp and init.sqf as per Xeno, and the getAray as per [FRL]Myke, the whole thing is still passing an empty array to the script.

Interestingly, I tried using getText and it passed the array as a string no problems. I'm really stumped here.

EDIT:

OK so I took a quick crash course in configs and realised that I needed to present my arrays thusly:

TPW_ARRAY[] = {value, value2,value3};

Seem to finally have it all working, thanks for all of you for your help and input!

Edited by tpw

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  

×