Jump to content
Sign in to follow this  
unknownx9

Change a public Variable

Recommended Posts

Hello,

Lets assume I have this public variable in a script called script1.sqf:

publicVariable "changeThis";

changeThis = 0;

while {changeThis == 0} do{statements};

Then I have another script called script2.sqf. In this script I want to add a code that will change the value of script1.sqf publicVariable "changeThis"; to a value of '1'. How would I go about doing that?

Thanks.

Share this post


Link to post
Share on other sites

I think you miss-understand how publicVariable works. All variables are either local or global there are no "true" public variable's in arma.

global

_local

The command publicVariable just broadcasts a variable across the network.

ie...

If you change your global variable on one machine you have to tell all the others about the change. publicVaraible is how you do that.

http://community.bistudio.com/wiki/Variables

http://community.bistudio.com/wiki/publicVariable

Edited by Riouken

Share this post


Link to post
Share on other sites

Thanks for the reply Riouken.

So is there any way I can change the value of that variable from another script?

Thanks.

Share this post


Link to post
Share on other sites

So is there any way I can change the value of that variable from another script?

Yes, you have to actually read the links provided by Riouken.

set in script1.sqf

globalVariable = 0;

in script2.sqf its changed with:

globalVariable = 1;

Share this post


Link to post
Share on other sites
Yes, you have to actually read the links provided by Riouken.

set in script1.sqf

globalVariable = 0;

in script2.sqf its changed with:

globalVariable = 1;

Thanks, and I will retry that again and see if it will work this time.

---------- Post added at 03:06 PM ---------- Previous post was at 02:58 PM ----------

Yes, you have to actually read the links provided by Riouken.

set in script1.sqf

globalVariable = 0;

in script2.sqf its changed with:

globalVariable = 1;

Did not work.

---------- Post added at 03:11 PM ---------- Previous post was at 03:06 PM ----------

I think you miss-understand how publicVariable works. All variables are either local or global there are no "true" public variable's in arma.

global

_local

The command publicVariable just broadcasts a variable across the network.

ie...

If you change your global variable on one machine you have to tell all the others about the change. publicVaraible is how you do that.

http://community.bistudio.com/wiki/Variables

http://community.bistudio.com/wiki/publicVariable

Reason I got messed up is because of this, which is on http://community.bistudio.com/wiki/Variables:

private "_myLocalVariable";

_myLocalVariable = 0;

So I assumed you had to declare it as a public/private variable then initialize it.

Share this post


Link to post
Share on other sites

You make variable's private to keep them from corrupting other variables in the same script but in a different scope

if (blabla) then {

private "_myLocalVariable";

_myLocalVariable = "this and that";

};

This does work, you must be having some other type of error. Please post your whole script so we can view it.

set in script1.sqf

globalVariable = 0;

in script2.sqf its changed with:

globalVariable = 1;

Share this post


Link to post
Share on other sites

I do not know what happened but it works now. This happens often, my game would run a script normal sometimes and others it wont.

Thanks for your help Riouken and Demonized.

unknownx9

Share this post


Link to post
Share on other sites

Global variables are only global within the session where they are defined/changed.

For example if you have a script running on the server session which sets variable "missionEnd=true", only scripts running on the server session will see that the variable has been created or changed. The same goes for player sessions, if you have a script running on all players connected and the script sets the variable on Tom's session, only scripts running on Tom's session will see the new value, players Dick and Harry and the server wont see the changed value.

The publicVariable command broadcasts the variable and its last value across the network to all players and the server session. Another useful feature is that all publicVariables are broadcast to players joining a mission that's already started, effectivily ensuring players JIP get the current value of the variable.

So to use public variable successfully you change the variable then use the publicvariable command to broadcast it to all players and the server.

Or at least that my understanding of global variables and publicVariable.

Share this post


Link to post
Share on other sites

Hi Guys,

ok, if i get this and the info in the biki, then, when i create marker on script wich runs on the server only, with global variable it will be "viwed" from all server side scripts?

myMarker = createMarker ["Marker_Sample", _somePos];

myMarker setMarkerShape "ICON";

myMarker setMarkerType "DOT";

! biki say that createMarker is global, but according to the post of fencr0c - "global" = server side scripts or all machines connected in the session this i cant understand and confuse me.

Accordeing to the previous post - the marker that i create will be visible to the server and may be people who start the session in the begining (i dont know, just guess).

To make the marker visible to all clients do i must make something like this:

myMarkerPos = getMarkerPos "Marker_Sample";

publicVariable "myMarkerPos";

and then in script who run only on client machines:

myMarkerOnClient = createMarker ["Marker_Sample_On_Client", myMarkerPos];

myMarkerOnClient setMarkerShape "ICON";

myMarkerOnClient setMarkerType "DOT";

Do its right or wrong? And do it will work, cause i dont have pc with arma around to test it now, but i am curious....

If we accept that its ok, and it "print" copy on that marker on any players map, what about the JIP players? Do they will see it too? And what if the position is changed? I mean if the position of the marker is changed in the server side script? If i get it right - if during the mission the position of the myMarker is changed, then it must be publicVariabled to the clients and on this way changed on their maps too.

Can anyone make simple code example for morons like me. Lets say - short example of two threads with server side and player side and interaction between. I read tons and tons and i confess - still cant understand the publicVariables. Last test before month finish with rage quit, because there was always different behavior from this that i expect to happen. :)

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  

×