Jump to content
austin_medic

Private command and undefined variables

Recommended Posts

Alright so I'm left scratching my head as the biki page on the private command seems to contradict what is going on in my logistics system currently.

 

Its set up to have a couple loops running in different threads using spawn.

 

I have a set of functions compiled using cfgFunctions, which also works fine for the most part, until I try to use these functions from the addActions created.

 

I am having issues with constant undefined variable errors no matter where I use private, which I get a feeling has to do with the addActions itself as when the variables are changed to global it all works fine.

 

I think that may cause some issues though when its used in MP with multiple people trying it at the same time.

 

I am very confused with the private command, and where my variables are magically being overwritten from when they're at the very top of the scope and nothing below overwrites it

Share this post


Link to post
Share on other sites

Hard to tell where the problem is, with no examples given, heh.

Private variables are actually pretty straightforward.

Care to show an example?

 

Cheers

Share this post


Link to post
Share on other sites

If you use spawn, none of your private variables are available to the spawned function. The two sets of scopes (the spawning and spawned) are unrelated. Anything you need to pass in privately needs to be passed in as an argument:

 

[_a, _bunch, _of, _privates] spawn

{

    params ["_a", "_bunch", "_of", "_privates"];

 

    ...

}

 

Also, if you fail to use "private" in any "call"ed functions, they will overwrite privates.

Share this post


Link to post
Share on other sites

Also, if you fail to use "private" in any "call"ed functions, they will overwrite privates.

Yeah, this can be really ugly. One variable missed to make private, things work fine for the time being, and then, one shiny day, it comes back to bite you in your ass.

In retrospective it might have been way better to implicitly declare any variables private, while having to explicitly declare variables that creep down to global scope.

This is one of the reasons why I liked Mac's Squint so much. Too bad it's no longer maintained.

Share this post


Link to post
Share on other sites

If you use spawn, none of your private variables are available to the spawned function. The two sets of scopes (the spawning and spawned) are unrelated. Anything you need to pass in privately needs to be passed in as an argument:

 

[_a, _bunch, _of, _privates] spawn

{

    params ["_a", "_bunch", "_of", "_privates"];

 

    ...

}

 

Also, if you fail to use "private" in any "call"ed functions, they will overwrite privates.

No local variables are available either, private or not. This is because spawn creates a separate thread, rather than a lower scope

Share this post


Link to post
Share on other sites

Here is a link to my mission file, still got no clue what I'm doing wrong, too many scripts to post the raw data here

 

link

Share this post


Link to post
Share on other sites

Any of them that are spawned in different threads (despite the fact that everything is being passed to them, all vars should be defined where they need to be, but regardless I still get undefined variable errors)

 

Also the entry on scopes in the wiki seems like it contradicts with what is going on in my game,

 

 
Scope

If a local variable is initialized within a Control Structures (i.e. ifforswitchwhile) its scope will stay within this structure (i.e. outside of the structure it will still be seen as undefined). This does not apply to global or public variables. - seems that isn't true in my case, global vars do not work either, returns undefined variable no matter whatif (alive player) then {_living=true}; hint format["%1",_living];Returns "scalar bool array string 0xe0ffffef", since the local variable was not initialized before being used within a control structure.

_dead=true; if (alive player) then {_dead=false}; hint format["%1",_dead]; Returns "false", since the variable was initialized before the if...then.

To initialize local variables, so that they are available throughout the whole script (including any control structures), either initialize it via theprivate command (e.g. private ["_varname"];), or by assigning a default value to it (e.g. varname=0;). - seems this doesn't matter either, undefined no matter what is done.

 

 

Share this post


Link to post
Share on other sites

One thing I noticed right in the init: allMissionObjects doesn't return anything because it runs too soon. Add some sleep before it, like waituntil {time >0}

And:

if(_x in AUSMD_acceptableVehicles) then..

_x isn't a class name, it's an object. So:

if(typeof _x in AUSMD_acceptableVehicles) then..

Slight typo AUSMD_acceptedVehicles != AUSMD_acceptableVehicles?

And in the addaction code:

{[cursorTarget,_object,"ADD"] call AUSMD_fnc_addOrRemove}

_object doesn't exist in the addaction's scope, so there's nothing to pass to the addOrRemove function.

Share this post


Link to post
Share on other sites

One thing I noticed right in the init: allMissionObjects doesn't return anything because it runs too soon. Add some sleep before it, like waituntil {time >0}

 

And:

if(_x in AUSMD_acceptableVehicles) then..

_x isn't a class name, it's an object. So:

if(typeof _x in AUSMD_acceptableVehicles) then..

 

Slight typo AUSMD_acceptedVehicles != AUSMD_acceptableVehicles?

 

And in the addaction code:

{[cursorTarget,_object,"ADD"] call AUSMD_fnc_addOrRemove}

_object doesn't exist here, so there's nothing to pass to the addOrRemove function.

Ah thanks a bunch, got it working now. 

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

×