Jump to content
Sign in to follow this  
eddyaod

Declaring variables as private is a pain

Recommended Posts

From what I understand, if you use "call" to execute code within a script the code called by "call" has access to any variable in scope in the parent script at the "call" command. It's a pain to declare all variables in any sqf as private at the start to prevent interfering with the outside scope, has anyone got any better solutions?

Share this post


Link to post
Share on other sites

Hmm you could probably create an regex that reads out all variables starting with an underscore and returns the correct private-array for your script.

Oh and if you're too lazy for that you could still use execVM instead of defining it as a function.

Guess there are not really any other "simple" solutions.

The regex would probably be the best idea.

---------- Post added at 04:51 PM ---------- Previous post was at 03:06 PM ----------

I've put something together, try this:

http://tajin.evilhosting.de/arma/private.html

just paste your script in the textbox, send it and it returns the private[] array for your script.

Share this post


Link to post
Share on other sites

That's neat, but is a little buggy.

This:

_unit = _this select 0;

_hi = getPos player;
_unit setPos _hi;
_dist = _unit distance player;
_dist

Gave me:

private [,"_hi","_unit","_dist"];

And this:

_text = "Hi!";

_unit = _this;
_unit

Gave me:

private ["_unit"];

---------- Post added at 11:11 AM ---------- Previous post was at 11:09 AM ----------

I entered my cam.sqf, got this:

private [,"_list","_projectile","_type","_relPos","_fov"];

It missed a few.

Share this post


Link to post
Share on other sites

Yup, noticed it too. I'm trying to fix that right now.

Regex is always good for a headache. ^^

Thx for the test.

Edit:

Ok found the problem, the array of variables somehow got cut off. It did find everything, it just failed in telling you so ^^.

Should work now.

Edited by Tajin

Share this post


Link to post
Share on other sites

private [,"_list","_projectile","_type","_relPos","_fov","_disablePP","_accTime","_enableParticles","_camera","_cancel","_pShape","_pSize","_pColor","_pSource"];

Still has a leading comma.

Share this post


Link to post
Share on other sites

alphabetic order ?

not a problem, just didn't see a need for it ^^

Share this post


Link to post
Share on other sites

That should have been the case already, since it reads the file from beginning to end.

Maybe it was because some of the variables appeared in the comments in you file. It doesn't ignore comments. (I'll add that later maybe)

Share this post


Link to post
Share on other sites

I can see it ignores _this, but it doesn't ignore other reserved variables:

_group = _this;
{_X setDammage 1} forEach units _group;

private ["_group","_X"];

Not sure what would happen if you declared _X private but I doubt anything noticable.

---------- Post added at 12:46 PM ---------- Previous post was at 12:42 PM ----------

It should also ignore strings:

_text = "_notalocalvariable";
hint _text;

private ["_text","_notalocalvariable"];

:p

---------- Post added at 12:48 PM ---------- Previous post was at 12:46 PM ----------

I can see it ignores _this

Ok, it's case sensitive. It will only ignore _this, not _This or _THIS (even though it's bad practice to capitalize _this and other local variables).

Looks like you still got a bit of work to do on it. :p

Edit: It's pretty fun trying to break other people's stuff... lol.

Share this post


Link to post
Share on other sites
That's neat, but is a little buggy.

This:

_unit = _this select 0;

_hi = getPos player;
_unit setPos _hi;


[b]_dist = _unit distance player;
_dist

[/b]

Are you saying the second _dist re-declares the variable? Or what? I don't understand wtf is going on :(

Share this post


Link to post
Share on other sites

I'm not sure how Tajin's little program works, but that code is just an example I wrote up to test it. It's legit code though, if called such that var = [unit] call code, it would return the distance between unit and the player, and put it into var.

So in an SQF file, if you end the file with an expression that is not terminated by a semicolon, it will evaluate that expression and return the end result to the calling script/code. Does that clear things up?

Edited by Big Dawg KS

Share this post


Link to post
Share on other sites

So in an SQF file, if you end the file with an expression that is not terminated by a semicolon, it will evaluate that expression and return the end result to the calling script/code. Does that clear things up?

Assuming it was a "call" and not "execVM"?

Share this post


Link to post
Share on other sites

I'm not sure what the behaviour would be if it was execVM or spawn. It might throw an error, or it might execute flawlessly. However it wouldn't be possible to get the returned value since execVM and spawn already return a handle to the script.

Share this post


Link to post
Share on other sites

It would run but you wouldnt be able to get the return value.

As for the regex method, it's perhaps more trouble than it's worth as you'd still have to go back and use the regex every time you add / remove / rename variables. Might as well just take care of your private array as you go along editing...

Share this post


Link to post
Share on other sites

Well you could always declare them private when you define them. Ex:

// a bunch of code

private "_newVar";
_newVar = "some string";

// more code

Share this post


Link to post
Share on other sites

I'm using

ArmA Script -> Introduce private-statement

for the whole file

and

ArmA Script -> Introduce private-statement (selection)

for a selected part of the file

with my current editor :)

Notepad++ that is with ArmA Script from iOnOs.

Xeno

Share this post


Link to post
Share on other sites
Looks like you still got a bit of work to do on it.

Edit: It's pretty fun trying to break other people's stuff... lol.

^^ I don't mind. Never said its perfect, but it works for normal usage and even if you have to adjust the array a bit, it's still easier than gathering all the variables youself.

Ignoring strings is a good point.

As for the regex method, it's perhaps more trouble than it's worth as you'd still have to go back and use the regex every time you add / remove / rename variables

Well, you asked for an easier way than adding them manually and that's what it is. ^^

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  

×