Jump to content
beako

Global Variables...consequences?

Recommended Posts

HI All,

 

I need a better understanding of the consequences of using global variables in my MP game.

 

I read the following Variables and KK's blog – Variables - ArmA Scripting Tutorials: Variables all three part by Killzonekid but still unsure how global variables will impact the performance of my MP game and the many scripts I have.


For example, is it better to use the following in every function that requires the enemy's side

_AIside = WEST;
if (playerSide isEqualTo WEST) then {_AIside = EAST};

or simply create the following in initServer.sqf

if (playerSide == WEST) then {AIside = EAST} else {AIside = WEST};

and use AIside in each function.

 

What is better for overall performance?

 

 

Share this post


Link to post
Share on other sites

Each time you can use a local (underscored) variable, it's better, especially if you have many inner codes (scopes in fact) inside other ones. You don't need these variables at every scope of a script.

But, IMHO, if you define a local variable on main scope of a script/ sqf, this difference is not great.

 

So it's on your side to decide if _AIside is OK everywhere it should be, and use it preferably.

 

Don't forget: a global variable runs globally on a PC (locally, not broadcast) on every sqf file you run for the mission. There is no more "scope" question in missionNameSpace. See also other name spaces.

 

Each time you want to have a specific global variable for objects / units (like loadout), you can use the set/getVariable commands which allow handy distinctive strings, in a one line code: For example,   {_x setVariable ["LDT"+name _x , getUnitLoadout _x] } forEach allUnits;

 

Now, in MP, you need to understand where the scripts are mandatory: server, clients, everywhere... The event scripts are fine.

For variables, if you run a common script, say init.sqf, each PC will run it at due time (when player is starting the mission). The global variables are working locally. That means independently. If you write myCount = 0 in init.sqf, along with some script for increasing it, all JIP will have 0 but the players in game will have their own different values. That can be weird.

 You need to publicVariable them, each time you need to broadcast them, for the same data everywhere. If you setVariable something, add the 3rd parameter TRUE in the array and don't forget to do that, each time the variable data changes.

Public or not public? Try to understand how and where scripts and variables works. No need to share (broadcast) some common data like kill count. When a unit dies, all PCs are able to work independently for the same result (here count). On the other hand, you'll have to share some data which are elaborated locally (server or client), on a local event/code,  but not everywhere. If you need that all PCs work with a common data coming from one PC, then make it public.

 

 

 

 

 

  • Like 1

Share this post


Link to post
Share on other sites
9 hours ago, beako said:

still unsure how global variables will impact the performance of my MP game

They don't.

9 hours ago, beako said:

For example, is it better to use the following in every function that requires the enemy's side

"WEST" is not a global variable. It's a script command.

Executing script commands is more expensive than reading a global variable.

And reading a global variable is more expensive than reading a local variable.

But for all of them the difference is so low that it doesn't matter for you.

 

 

  • Like 2

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

×