Re: publicVariable - In use (if not in fact) there are three different types of variables, not two - private, public and "shared public".
Private variablesAs we all know (
) private variables are local only to the script in which they appear. So for instance - I could write one script that spawns another script any number of times and have each of these spawned scripts use the same private variable at the same time. This is not a problem as each variable is only relevant within its own environment.
Example -
;parent.sqs
_x=10
#loop
this exec "offspring.sqs"
_x=_x-1
~1
if _x >0 then goto "loop"
;offspring.sqs
_x=10
#loop
"Shell73" camCreate (getPos player)
_x=_x-1
~1
if _x >0 then goto "loop"parent.sqs will spawn offspring.sqs ten times and each of these spawned scripts will in turn camCreate a Shell73 on the head of the player ten times (100 Shell73's in total). :noo: :beat:
In this case _x is used in the parent script AND in each of the spawned offspring scripts. At any given time the value of this variable will be different in each and every script, but this won't matter as they are only ever referred to internally. The _x in parent.sqs has absolutely nothing in common with the _x in any other script that uses that same variable name apart from the label "_x".
Public variablesUnlike private variables, public variables DO care about other scripts,
but only if they are running on the same machine !. In this way you can create a "persistent" variable that can be set by one script (eg init.sqs) and then modify it throughout the mission by running other scripts (eg via addActions, triggers etc).
Example -
;init.sqs
playerCanteens = 12
;drinkWater.sqs
if playerCanteens < 1 then exit
playerCanteens = playerCanteens - 1
<player effects go in here>
:
In this instance init.sqs sets the variable at the outset and each use of the script drinkWater.sqs reduces it. playerCanteens may also be reduced by other scripts or influences (eg - triggers), but again, only if they are called on that machine.
In fact, the above scripts could be run simultaneously on all machines running in an MP mission and there would be no problem - each machine would update it's own version so that I might be completely out of canteens while you still had nine left, for example.
"Shared Public" variablesThis is the same as above, but in this case other machines DO affect the variable. To make a public variable "shared" all you have to do is execute the command "publicVariable XXX" where XX is the variable to be broadcast.
Taking the above example as a start, if one machine were to broadcast "publicVariable playerCanteens" then the current value of ITS version of playerCanteens would be broadcast to all other machines, instantly updating them to this value. So if I was empty and you still had nine left, and I broadcast my value then you and every other player in the game would now also be empty. And I would instantly become very unpopular !
Note that in most cases publicVariables will want to be shared, but not always.
The above sample is a good example of when you WOULD NOT want to broadcast it. In this case one script works for all players, each running their own version and keeping track of their own canteen levels - there is no need to write one version of the script for each player.
A good example of when you WOULD want to broadcast it would be a scoring script - one machine would update the score then broadcast this to all other machines, the others would just read the broadcast value and update their own version of this variable accordingly.
Server loops and client loopsThis brings up the best way to use shared public variables - server loops and client loops. Check out the following time dilation script -
; timeDilation.sqs
; set time dilation factor
_dilationFactor = 1/1200
; Send client machines to client loop
? !(local server) : goto "clientloop"
; This loop runs continuously on host machine actioning time dilation.
; Time dilation is sent to all client machines via public variable declaration
#serverloop
skipTime _dilationFactor
realTime = daytime
publicVariable "realTime"
; Pause, continue loop
~0.1
goto "serverloop"
#clientloop
; This loop runs continuously on all client machines actioning time dilation.
; Dilation factor is derived continuously from public variable realTime distributed by host machine
? realTime > daytime : skipTime (realTime - daytime)
; Pause, continue loop
~0.1
goto "clientloop"This script uses a server loop which update the server's game time and broadcasts this to the network, and a client loop which simply receives the server-determined game time and update its own local game time accordingly. This saves having to write two scripts and makes it easier for a scripter to understand and edit it.
Converting "public" and "private" variablesOnce you have defined a variable you can convert for what type to another by simply reassigning it. For example - you might have a private variable that runs within a script that at its conclusion spits out a public version of the same value. The syntax is simple - all you do is drop the preceding underscore in the public version (eg - playerCanteens = _playerCanteens)
For instance, you might want to run one single script on 10 different units that returns ten different values, then takes the highest or lowest one of these values. Similarly, you might want to run a script on ten different PLAYERS (ie machines), then take the highest or lowest of these results. In this case, the script would run, the public variable would be assigned, then it would be broadcast to the network. (eg - playerCanteens = _playerCanteens, publicVariable playerCanteens)
Well, that's my latest two cents on the subject. Once again, I started writing and my fingers got ahead of me. I apologise in advance for any poorly written or boneheadly obvious info contained in this post.
Cheers all
roni