Jump to content
Sign in to follow this  
dwinar

Using one script's variables by other scripts

Recommended Posts

Is there someone who can give me an advice how to freely pass variables that were initialized in one script to others that were started later? Is there way to make global variables inside sqf script?

For example there is an event handler fired on getting hit by AI now it launches a script with objects of shooter and player who got shot, now the sqf takes those arguments and create markers on positions of those objects. The question is: how can the script launched for example via radio use those positions?

Share this post


Link to post
Share on other sites

#define all of your global variables in one file (macros, defines, gvars, whatever), then #include that file into the .sqf that you want those GVARs in.

Share this post


Link to post
Share on other sites

I don't think #preprocessor commands are the first thing one should learn. To make a variable global (as long as it's on the same client, but I'm not expecting you to be doing this for MP), just remove the _ from _myVariable. A variable without _ prefix is a global variable. A variable with _prefix is a local variable to that script, or rather scope. A scope is anything in curly bracers { /*this is a scope */ }; - see also the command "private".

If you do want to use _local variables normally, then just assign/copy the value from that to a global variable (myGlobalVariable = _myLocalVariable).

Share this post


Link to post
Share on other sites

Is myVariable global without the _ on a dedicated server? Or are you just saying for client use only?

Share this post


Link to post
Share on other sites
Is myVariable global without the _ on a dedicated server? Or are you just saying for client use only?

That depends on which clients ran the script. If all of them did, then yes, it's global.

Share this post


Link to post
Share on other sites

Afraid we're gonna confuse OP here, but yes - only on client, and only on dedi - they live separate lives unless you use publicVariable. Another way is to use setVariable on an object that lives in the missionfile. It uses commands getVariable and setVariable to manipulate it, but supports automatic publicizing of values (own switch for that for the command).

Share this post


Link to post
Share on other sites
Afraid we're gonna confuse OP here, but yes - only on client, and only on dedi - they live separate lives unless you use publicVariable.

This is only true if the variable was declared/changed in a script or trigger that was only run by a particular client or server. In most cases, publicVariable isn't necessary because the script/trigger/init line is being run by all clients as well as the server. Common exceptions include scripts activated via addAction and anything activated within an isServer-conditional scope.

Edited by ST_Dux

Share this post


Link to post
Share on other sites

Thanks for those advices, now I know where to start. I didn't know that prefix "_" actually mean something :P and the defining variables in another file sounds good anyway it's what I do for dialogs but didn't get the idea to use it with simple scripts.

Share this post


Link to post
Share on other sites

@ST_Dux:

Yes it depends on how it was started, but your "in most cases" are my "exceptions" :). If you make sure a script does the same thing on all clients, the result may be the same, but often you exit a script if it doesn't apply to that client. I.e. "is client leader of his group" then {continue and publicize a variable} else {just quit} and for both (maybe the leader status changes) add some addPublicVariableEventHandler to listen for changes. Also dialoges would fall under your exceptions list.

It's more a convention or style we choose to follow, not right or wrong. Do heavy math on all clients or cause extra net traffic by sharing the result? Depends (for me) on what size of the data is and the frequency.

---

Btw, can you #define a variable? Doesn't this exist only as a constant from there on? For me, preprocessor commands messes up my line number reporting in errors, so I'm not a big fan of them, other than the usual #defines.

Edited by CarlGustaffa

Share this post


Link to post
Share on other sites

#define is for the preprocessor, means if you use #define X 5 then everywhere you write X the preprocessor will write 5 instead, and only then go on and compile your script.

If you then try to write in the middle of your script X=6, you will effectively be writing 5=6, and not actually change the value of X.

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  

×