Jump to content
Noigel

Need help initializing repetitive global variables as efficiently as possible.

Recommended Posts

In a mission I'm creating I have several laptops set up to handle the same actions but each with it's own separate "target".  Working with dialogs I need to initialize and use eight global variables that are in a similar situation.  Basically I'm making sure that if the variable is nil it gets assigned a base value but if it is not nil I leave it alone.  Simple, right?  My code works but there's got to be a better, more efficient way.  Check it out:  

 

if (isNil "Distance_Adjust_Laptop1") then {
Distance_Adjust_Laptop1 = 25;
};
 
if (isNil "Distance_Adjust_Laptop2") then {
Distance_Adjust_Laptop2 = 25;
};
 
if (isNil "Distance_Adjust_Laptop3") then {
Distance_Adjust_Laptop3 = 25;
};
 
if (isNil "Distance_Adjust_Laptop4") then {
Distance_Adjust_Laptop4 = 25;
};
 
if (isNil "Distance_Adjust_Laptop5") then {
Distance_Adjust_Laptop5 = 25;
};
 
if (isNil "Distance_Adjust_Laptop6") then {
Distance_Adjust_Laptop6 = 25;
};
 
if (isNil "Distance_Adjust_Laptop7") then {
Distance_Adjust_Laptop7 = 25;
};
 
if (isNil "Distance_Adjust_Laptop8") then {
Distance_Adjust_Laptop8 = 25;
};

 

Is there a way to get the same functionality using less repetition?  I tried using a foreach loop but after my attempt when I "hinted" one of the variables to test it and it responded back as "any" instead of the number "25" that I was expecting.  Thanks for reading.  

 

Thanks,

Noigel     

Share this post


Link to post
Share on other sites

Is number the same for everything?  Does it change? Do you really need 8 different global variables?  Can you just save the variable on the laptops?  How is this number used?  What does it do with your dialogs?

Share this post


Link to post
Share on other sites

1, 2, 5) The number is a range, same beginning distance for each laptop.  It does change, starts at 25 meters and increases/decreases depending on how far out the player wants the target  

 

3, 6) Unfortunately yes... to my knowledge I'm stuck with needing 8 different global variables.  Would be elated if I'm proven wrong.  So far I've not found a way to change and pass local variables from a dialog (.hpp file) to sqf:

 

class RscSlider_Distance: RscSlider
{
idc = 1900;
onSliderPosChanged = "Distance_Adjust_Laptop1 = 25 * (round ((_this select 1) / 25)); ctrlSetText [1901, str Distance_Adjust_Laptop1 + ' meters'];"
};

 

Ultimately the dialog collects the user-inputted distance and stores it in the global variable, the global variable later stores it in an array, and the array gets called when I need to do things like change the type of target on the lane.

 

4) Could I save it to the laptops?  Maybe?  I'm not super familiar with this practice but each variable is only used on each respective laptop.  My requirements are that any player can be able to adjust the range/change the number for any laptop... I don't foresee storing it on the laptop affecting that though.  I am wary of storing variables/value on objects due to Killzone Kid's comments (I think it was him) that if the players are out of range when a variable is changed it could cause issues.

Share this post


Link to post
Share on other sites

https://community.bistudio.com/wiki/sliderPosition

 

Don't worry about variables disappearing - those comments applied to map objects (where data was dynamically streamed) and isnt even a thing anymore from my tests.  (It might be but I haven't seen it happen in this game storing lots of stuff to map objects).  ANyway putting variables on objects like laptops is fine.

Share this post


Link to post
Share on other sites

I am wary of storing variables/value on objects due to Killzone Kid's comments (I think it was him) that if the players are out of range when a variable is changed it could cause issues.

 

That's only for map objects, not mission objects.  So things that are part of the map like buildings or road segments or whatever.  Laptops you spawn in via script or place in the editor are not affected by the streaming thing he was talking about. So no worries for that.

 

You can grab a variable with this, and it'll default to 25 if it's not found:

_laptop1Distance = laptop1 getVariable["noi_Distance_Adjust", 25];

Problem is I don't know if you can use that in dialogs, so you might be stuck with globals. :)  Someone that is better with dialogs will hopefully chime in.  I wonder if uiNamespace might be useful?

Share this post


Link to post
Share on other sites

iirc you do:

 

with {whateverNamespace} do {//blah blah} to find one within another

Share this post


Link to post
Share on other sites

Since you can also setVariable/getVariable to displays it should be fine.

 

Do this in the init or after mission start:

findDisplay 46 setvariable ["MyLaptops",[laptop1,laptop2,bleh,bleh,bleh]];

This will save the variable "MyLaptops" on the primary display.

 

now if you need to retrieve the value of one of these laptops, no matter if from a script or a dialog, you'd go:

_laptops = findDisplay 46 getvariable ["MyLaptops",[]];//returns an empty array just in case
_laptops params ["_laptop1","_laptop2","bleh","bleh","bleh"];//the quotation marks are the key here
_laptop1 getvariable ["MyLaptopVariables",25]//returns a default number for range, can also be an array of numbers or basically whatever you want

For a start I'd recommend these websites in no specific order, sure helped me out a ton, besides helpful folks on this forum:

Kylania

KK

Code Optimisation

 

Cheers

Share this post


Link to post
Share on other sites

If you want a less repetitive way then use this:

for "_i" from 1 to 8 do
{
  _var = call compile format["Distance_Adjust_Laptop%1", _i];
  if (isNil "_var") then { call compile format["Distance_Adjust_Laptop%1 = 25", _i] };
};

Share this post


Link to post
Share on other sites

Thanks to all of you!  A lot of good info here that I will incorporate.  Great to start off with a question and get tons of other things that will help in the same package.  :)  

Share this post


Link to post
Share on other sites

 

If you want a less repetitive way then use this:

for "_i" from 1 to 8 do
{
  _var = call compile format["Distance_Adjust_Laptop%1", _i];
  if (isNil "_var") then { call compile format["Distance_Adjust_Laptop%1 = 25", _i] };
};

No need to call compile, just query them straight from missionNamespace..

for "_i" from 1 to 8 do {
    _varName = format[ "Distance_Adjust_Laptop%1", _i ];
    if (isNil { missionNamespace getVariable _varName } ) then {
        missionNamespace setVariable [ _varName, 25 ];
    };
};

Or alternative for if your variables where not all the same/consecutively named..

{
    _x params[ "_var", "_value" ];
    if ( isNil _var ) then {
        missionNamespace setVariable [ _var, _value ];
    };
}forEach[
    [ "myVar", 123 ],
    [ "myOtherVar", "hellowWorld" ]
];

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

×