Jump to content
chow86

Passing a global variable to a script then back again

Recommended Posts

This feels like an obvious question but my Googling has failed...

 

I want to pass the name of a global variable to a script, e.g.

[myVariableGlobal] execVM "myscript.sqf"

(this script will deal with other global variables, hence why the name of the variable needs to be part of the argument to execute it)

And in the script "myscript.sqf", e.g.

_myVariableLocal = _this select 0;

Say "myVariableGlobal" was previously defined globally as "false".  I now want "myscript.sqf" to define it as true.  Simply writing the following in "myscript.sqf" should not work, since the variable is now only local:

_myVariableLocal = true;

How, then, do I make it global again?

Share this post


Link to post
Share on other sites
myVariableGlobal = _myVariableLocal;

That would do it.  Also there is no need to pass the global variable to the script, as since its its global you can reference it directly in any script (after its been defined of course).

 

Its probably cleaner to use namespace variables though.  Take a look at setVariable and getVariable.

 

And welcome to the forums and the whacky wily wonderful world of arma scripting!  :wave:

  • Like 1

Share this post


Link to post
Share on other sites

If it's a global variable then you can just use it without needing to pass any parameter. This is the exact reason why you shouldn't make too heavy use of them.

myGlobalVariable = "Hello World";
[] spawn {
    sleep 2;
    hintSilent myGlobalVariable;
};

Fun fact,

missionNamespace setVariable ["myGlobalVariable", "Hello World"];
myGlobalVariable = "Hello World";

Are the same thing, or I suppose it's more accurate to say "a global variable is a missionNamespace variable"

 

Proof:

missionNamespace setVariable ["myGlobalVariable", "Hello hint"];
myGlobalVariable2 = "Hello systemChat";
[] spawn {
	sleep 2;
	hintSilent myGlobalVariable;
	systemChat (missionNamespace getVariable "myGlobalVariable2");
};

 

  • Like 1
  • Thanks 2

Share this post


Link to post
Share on other sites
46 minutes ago, dreadedentity said:

Are the same thing, or I suppose it's more accurate to say "a global variable is a missionNamespace variable"

Interesting, thanks.  Why then bother hassling with missionNameSpace set/get?  I guess its to be more explicitly readable.

Share this post


Link to post
Share on other sites
1 hour ago, johnnyboy said:

Why then bother hassling with missionNameSpace set/get?

For me, the main benefit is that getVariable can return a default value if the variable is nil so you can do a 1-line If statement, plus won't throw error for undefined variable:

if (missionNamespace getVariable ["myInt", 42] < 100) then {
	//do something
};
//vs
myInt = //some value
if (myInt < 100) then { //I think would throw error if it didn't exist also
//etc.

But there are 2 other reasons as well:

  1. They don't know about this
  2. They like to do a lot of typing

I'm pretty sure this also works with other namespaces if you switch contexts using the with command:

with profileNamespace do {
	DE_Greeting = "DreadedEntity was here";
	saveProfileNamespace;
};
hintSilent (profileNamespace getVariable "DE_Greeting");

So in a way, you can think about missionNamespace being the "default" namespace, at least I do

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
1 minute ago, dreadedentity said:

For me, the main benefit is that getVariable can return a default value if the variable is nil so you can do a 1-line If statement, plus won't throw error for undefined variable:

True, that is an important benefit.  Thanks.

 

I've mostly converted to using missionNameSpace vars over the last year or two.

Share this post


Link to post
Share on other sites

Thanks everyone, super helpful.  Turns out I was using global variables correctly but they were still not working.

 

Not quite sure why.  Possibly some bug related to them being undefined, even though I had defined them properly in init.sqf before using them in the script.

Anyhow, using missionNameSpace fixed the issue, so that's a very good tip.

 

Quote
6 hours ago, johnnyboy said:

And welcome to the forums and the whacky wily wonderful world of arma scripting!  :wave:

 


Thanks!

  • Like 1

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

×