Jump to content
Sign in to follow this  
tortuosit

Different data type possible in userconfig, how to handle this?

Recommended Posts

Hi,

I have the following code for reading userconfig file entries:

_t_trend = getText (configfile >> "TORT_DYNAMICWEATHER_Settings" >> "trend");

My problem: I want to allow different data types, i.e. string or array. _t_trend can be "blah" or [1,2,3]...

But with getText/getArray I already decide what data type it is, before the script can know... How do I handle this?

Share this post


Link to post
Share on other sites

Hi,

Use "isArray" or "isText" for checked if the config entry represents an array or a text.


//_t_trend will always an Array

_t_trend = [];
if (isArray  (configfile >> "TORT_DYNAMICWEATHER_Settings" >> "trend") ) then
{
_t_trend = getArray (configfile >> "TORT_DYNAMICWEATHER_Settings" >> "trend");
};
if (isText  (configfile >> "TORT_DYNAMICWEATHER_Settings" >> "trend") ) then
{
_t_trend = [getText (configfile >> "TORT_DYNAMICWEATHER_Settings" >> "trend")];
};





Edited by TittErS
code

Share this post


Link to post
Share on other sites

Don't know if this is of any help to you but you can always play a wildcard by saving everything as string and then compile it. It's not nice but I do that sometimes, for example when I have multi-dimensional arrays saved in configs. (I know it's also possible to save them correctly in configs but it makes copy-paste operations easier and then there's my lazyness...)

Example - Config:

magazines = "[['30Rnd_556x45_Stanag', 10], ['16Rnd_9x21_Mag', 3], ['HandGrenade', 4], ['SmokeShell', 2], ['SmokeShellRed', 1], ['SmokeShellGreen', 1]]";

And then somewhere in my code:

_magazines = call(compile(getText(_class >> "magazines")));	
{_unit addMagazines _x} forEach _magazines;

Just a small hint, I'd go for TittErS' approach in your case.

Share this post


Link to post
Share on other sites

Oh thanks guys, have overseen isArray/isText and IndeedPete, another interesting option, thx.

---------- Post added at 13:48 ---------- Previous post was at 13:05 ----------

Now I remember, I once tried isArray, but it didn't work. This was only a scope problem, because without private statement _t_trend is only known inside the if-brackets...

Edited by tortuosit

Share this post


Link to post
Share on other sites

Yes, either set it private or (if you know that it can only be array or text) you could also try:

_t_trend = if (isArray(configfile >> "TORT_DYNAMICWEATHER_Settings" >> "trend")) then {
   (getArray(configfile >> "TORT_DYNAMICWEATHER_Settings" >> "trend")
} else {
   ([getText(configfile >> "TORT_DYNAMICWEATHER_Settings" >> "trend")])
};

But isArray should work, I use it on a regular basis.

Share this post


Link to post
Share on other sites

Yes I used if/else because it only can be array or string (or not available, which I'm to lazy to catch) :)

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  

×