Jump to content
Deadfast

The magical "local" keyword

Recommended Posts

I've recently discovered that in addition to your usual local command there is the local keyword.

 

What does it do?

 

Well, pretty much the same thing as private but much more conveniently:

// Using private...
private "_test";
_test = 1;

if (true) then
{
   private "_test";
   _test = 2;
};


// Using the local keyword...
local _test = 1;

if (true) then
{
   local _test = 2;
};

The only disadvantage is that the assignment must follow, without it you're just invoking the command version.

 

As far as I can tell this has existed for a very long time (at least Arma 1 I suspect). Was anyone else aware of its existance?

  • Like 2

Share this post


Link to post
Share on other sites

I'm no expert by any means but I've never seen that used before.

Share this post


Link to post
Share on other sites

Whatever you read here, forget it :P

Share this post


Link to post
Share on other sites

But it is. That's the point of this thread. :)

 

https://resources.bisimulations.com/wiki/local_%28Keyword%29

 

I can't see conveniency of it though.

 

 

Didn't know about that and putting local into the wiki revealed the known local command.

I guess this command  is obsolete, since it basically does the same as private !? If I think about this twice, I agree with the OP, this is actually way more convinient than using private.

Share this post


Link to post
Share on other sites
private ["_someVar1", "_someVar2", "_anotherVar", "_yetAnother", "_oneMore", "_andEvenMore"];
local _someVar1 = param [0,[0,0,0]];

You could use it like this, couldn't you?

Share this post


Link to post
Share on other sites
private ["_someVar1", "_someVar2", "_anotherVar", "_yetAnother", "_oneMore", "_andEvenMore"];
local _someVar1 = param [0,[0,0,0]];

You could use it like this, couldn't you?

 

 

With a slightly atltered param statement, you wouldn't need any of these.

params [
   ["_someVar1", [0,0,0], []],
   ["_someVar2", "", [""]],
   "_anotherVar"
];

All these vars will be created in the innermost scope.

Share this post


Link to post
Share on other sites

With a slightly atltered param statement, you wouldn't need any of these.

params [
   ["_someVar1", [0,0,0], []],
   ["_someVar2", "", [""]],
   "_anotherVar"
];

All these vars will be created in the innermost scope.

 

 

Yes of course, didn't think about params (facepalm)

Share this post


Link to post
Share on other sites

I've recently discovered that in addition to your usual local command there is the local keyword.

 

What does it do?

 

Well, pretty much the same thing as private but much more conveniently:

// Using private...
private "_test";
_test = 1;

if (true) then
{
   private "_test";
   _test = 2;
};


// Using the local keyword...
local _test = 1;

if (true) then
{
   local _test = 2;
};

The only disadvantage is that the assignment must follow, without it you're just invoking the command version.

 

As far as I can tell this has existed for a very long time (at least Arma 1 I suspect). Was anyone else aware of its existance?

 

Awesome find dude!

Share this post


Link to post
Share on other sites

With a slightly atltered param statement, you wouldn't need any of these.

params [
   ["_someVar1", [0,0,0], []],
   ["_someVar2", "", [""]],
   "_anotherVar"
];

All these vars will be created in the innermost scope.

 

Sure, this works assuming the only variables you use inside a script/function are those you get as parameters. But usually, that's not the case, at least when you're writing more advanced scripts. So you will need an additional private array to cover the variables you didn't get as parameter. In that case, this:

private ["_someVar1", "_someVar2", "_anotherVar"];

could probably look like this:

local _someVar1   = "";
local _someVar2   = true;
local _anotherVar = 0;

So in my eyes, the useful cases for the local keyword are rather limited, like when have only one additional private variable.

Share this post


Link to post
Share on other sites

Sure, this works assuming the only variables you use inside a script/function are those you get as parameters. But usually, that's not the case, at least when you're writing more advanced scripts. So you will need an additional private array to cover the variables you didn't get as parameter. In that case, this:

private ["_someVar1", "_someVar2", "_anotherVar"];

could probably look like this:

local _someVar1   = "";
local _someVar2   = true;
local _anotherVar = 0;

So in my eyes, the useful cases for the local keyword are rather limited, like when have only one additional private variable.

 

From what the documentation states, any passed variable name will be private'd. Irrelevant if there's an actual value given for this var as default or in _this. So just as with private, every param'd variable is set to private, even if it is nil.

Share this post


Link to post
Share on other sites

Sure, this works assuming the only variables you use inside a script/function are those you get as parameters. But usually, that's not the case, at least when you're writing more advanced scripts. So you will need an additional private array to cover the variables you didn't get as parameter. In that case, this:

private ["_someVar1", "_someVar2", "_anotherVar"];

could probably look like this:

local _someVar1   = "";
local _someVar2   = true;
local _anotherVar = 0;

So in my eyes, the useful cases for the local keyword are rather limited, like when have only one additional private variable.

 

If you have say 3 elements array, after you have defined vars for the 3 elements you can continue with other vars, it will be as if you used "private"

[1,2,3] params [_one, _two, _three, _private1, _private2....]

local seems nice for defining vars on the fly

instead of

private "_myvar";

_myvar = 123;

it is more convenient to write

local _myvar = 123;

also good for scheduled scripts

Share this post


Link to post
Share on other sites

Ooh nice find, will be very handy for those little throw away _tmp variables that dont really belong in the main private but you want to make sure your not destroying something in a higher scope and as KK pointed out you always feel dirty having to private then define only to let it get cleaned up moments later.

Share this post


Link to post
Share on other sites

If you have say 3 elements array, after you have defined vars for the 3 elements you can continue with other vars, it will be as if you used "private"

[1,2,3] params [_one, _two, _three, _private1, _private2....]

I haven't had such a scenario yet, but you might actually be right. I'mma test this next chance.

 

also good for scheduled scripts

You mean unscheduled, don't you? Scheduled scripts (running in scheduled environment) are spawned scripts. They can't access the variables of the surrounding code, but called scripts (running in unscheduled environment) can, because a call is pretty much just the execution of code in a new scope.

 

Just realized that I probably don't have to tell you that. ;)

Share this post


Link to post
Share on other sites

I haven't had such a scenario yet, but you might actually be right. I'mma test this next chance.

 

You mean unscheduled, don't you? Scheduled scripts (running in scheduled environment) are spawned scripts. They can't access the variables of the surrounding code, but called scripts (running in unscheduled environment) can, because a call is pretty much just the execution of code in a new scope.

 

Just realized that I probably don't have to tell you that. ;)

No I meant scheduled. The script suspension can happen anywhere in the script, even between declaring private variable and assigning value to it. Not a biggie, but at least with local you can be sure the operation will be done all in one no matter what.

Share this post


Link to post
Share on other sites

Looks handy for on-the-fly scripting. Private'ing variables is a rather tedious task, and perhaps local can be used while coding the script and then, once done, go back and create a private array and remove all the locals.

 

Other than that I can't see myself using it.

Share this post


Link to post
Share on other sites

No I meant scheduled. The script suspension can happen anywhere in the script, even between declaring private variable and assigning value to it. Not a biggie, but at least with local you can be sure the operation will be done all in one no matter what.

 

I don't see the problem yet. What could worst case happen if the script gets suspended between declaration and assignment?

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

×