Jump to content
🛡️FORUMS ARE IN READ-ONLY MODE Read more... ×
Sign in to follow this  
RonnieJ

Local/global values how does that work?

Recommended Posts

Hey guys im going nuts trying to figure out how the local/global things work... as I understand using "_" before a variable makes it local... and by local it is only set on that persones computer? og by not doing the "_" it is a server set variable?

There is also something about setting a local var inside an if right?

Would be nice to get theese things clarified :)

Thx :)

Share this post


Link to post
Share on other sites

A local variable is only visible to that script or block of code on the same client depending on where it is defined and is presented with a "_" such as _myLocalVar = 0;

A global variable is visible to all the scripts etc on the same client, presented without the "_" such as myGlobalVar = 0;

A public variable is visible to all clients, variables are usually made public with publicVariable.

There is probably a better way to explain it but I have been drinking.

Edited by xZeven

Share this post


Link to post
Share on other sites

You got it wrong. The local/global thing which is covered with the leading underscore _ hasn't to do anything with Multiplayer.

Local means:

this variable exists only inside the script it is defined. It stops existing once the script is terminated. Other scripts can not access it's value.

Global means:

this variable exists only on the client it was defined. All scripts that run on this client can access this variable.

But also global variables can be different on each client.

To make global variables valid for all clients (and also server) you need to broadcast them with "publicVariable" command.

:EDITH:

Beaten by a newcomer by 2 minutes. :lol:

Share this post


Link to post
Share on other sites

Hi RonnieJ,

There are two things to understand with variables:

First:

Local variables are all that begins with "_" and its visibility is only in the script that is runing.

Example:

//Local variable
_myvar = 5;

_myvar is local and it only is visible in the script that it isdefined.

Global variables are all that not begins with "_"

//Global variable
Myvar =5;

is a global variable, is visible in any side the game but only in the machine that is runing the script. What this means?

If there are two machines playing a mission, one a server and another the client, if the server runs a scritp that sets Myvar =6; only the server has this value, the client not see the change.

The correct thing to do when programing is setting value of global variables only in the server, and then propagate its value to all the clients:

if (!isServer) exitwith{};

Myvar =5;

//Now propagates the value to all machines publicating de variable
publicvariable "Myvar";

publicvariable only must be done with global variables.

Share this post


Link to post
Share on other sites

Had once thought about a easy, non-scripting-language-related explanation of it. I don't know if the explanation is any good so i would throw it in here to see if this works. Here we go:

Imagine a school building. This is a MP game going on. Each classroom is a client (or server, doesn't matter here) ans each schoolar is a script runnin.

Now, a schoolar A defines for itself on it's very own paper that _A = 2.

One seat next to him, schoolar B defines _A = "teachers are dumb".

Although both used _A as variablename, they won't interfere as these variables are local to the schoolar.

Now schoolar C stands up and goes to the board in front of the class and writes on it A = 35.

This is now a variable which is valid for the whole class. Whenever a schoolar has a variable A in his script, he has to refer to the board. If his script does change the variable, he has to get up and update it on the board.

So far we stayed inside the same classroom. Whatever was defined here does not affect other classrooms.

Let's assume schoolar C says to the teacher "this is a public variable", so the teacher gets up and opens the classrooms door and yells "To all: A = 35 from now on!".

In the other classrooms, the teachers hear that, stand up and write this new (or changed) variable to the board also. Now this variable has become valid in all classrooms.

That's it. Maybe it could be explained much simpler if i only could better write in english.

Share this post


Link to post
Share on other sites

Unfortunately it's a little more complex than that even. But, important to know, even for beginners.

Local _variables aren't just only local to the script file they are used in, but even the script block, unless declared. This means that:

_a = 2;

_b = 2;

if (_a == _b) then {_test = "yay"};

hint format ["value of _test = %1", _test];

fails... Why? Because what happens inside then { } is considered a code block. So whatever is defined there, in this case _test, no longer applies to what comes after the block. In order to fix this, you can either use

private ["_test"];

at the beginning of the script, or initialize it with i.e.:

_test = "";

before the block is executed.

Please correct me if I'm getting this wrong or don't understand it well enough myself :)

Share this post


Link to post
Share on other sites

There is a street. In the street lives a man called Dave, and everyone in the street calls him Dave, and everyone knows who he is.

Also, each house has a person called _Dave. Each _Dave is unique to that house, and lives ONLY in that house, and only people in the house can talk to _Dave. Each _Dave can have different qualities.

Street = game, house = script :)

Share this post


Link to post
Share on other sites

Thanks guys the was exatctly what I was looking for... suddenly alot of scripting issues make sence :) Much appreciated guys!

Share this post


Link to post
Share on other sites
Sign in to follow this  

×