Jump to content
Sign in to follow this  
Smoerble

how to share variables between different sqf files?

Recommended Posts

Hello all.

I want to use a variable in different sqf files.

init.sqf:

smo_ammoBoxLoadout = [""];

test1.sqf:

player globalChat format["test: %1", count smo_ammoBoxLoadout];

error message:

error in line 52:
_a = count smo_ammoBoxLoadout;
Error Undefined variable in expression: smo_ammoboxloadout

I also tried to define the variable in init.sqf like this:

this setVariable ["smo_ammoBoxLoadout",["test"], false];

Anyone has an idea please?

Edited by Smoerble

Share this post


Link to post
Share on other sites

--- removed in deference to better response below ---

Edited by ACyprus

Share this post


Link to post
Share on other sites

Usually global variables are visible to all scripts that are being executed after the variable is defined. So if you define a variable globally (like smo_ammoBoxLoadout; local variables have an underscore in front of them: _ammoBoxLoadout), it should already be available to your test1.sqf. But you have to call your test1.sqf after your init.sqf, or after your definition of smo_ammoBoxLoadout in the init.sqf.

So

smo_ammoBoxLoadout = ["1"];
[] execVM "test1.sqf";

should work.

If you're already doing it like that, the empty entry may be a problem. But that's just a guess.

Share this post


Link to post
Share on other sites

If timing of script execution can wait, putting

waitUntil {!isNil "smo_ammoBoxLoadout"};

in your script will suspend it until the variable is defined.

Remember than waitUntil can only be used in scheduled enviroment, and for isNil the variable you're looking for should always be a string, or it will try to read the variable.

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  

×