Callaghan-
Here is a quick summary of public variables. First, if you haven't already review these in entries in the Comref:
publicVariableaddPublicVariableEventHandlerA public variable is just a global variable that has been broadcast to all clients (individual player machines) and the server. Here is an example. Player A does something that earns his team "points". Code will run on his machine that adds the point to the score (recorded in a public variable):
teamScoreW = teamScoreW + 10; // Add 10 points to the west team's score.
Now, to make sure the server and all clients know that the team's score has been increased (right now, only Player A's computer knows) you use the publicVariable command to broadcast it. So, we add the a new line of code:
teamScoreW = teamScoreW + 10; // Add 10 points to the west team's score.
publicVariable "teamScoreW"; // Broadcast the score to all clients and the server.
Now, teamScoreW will be the same on all clients and the server. If similar code runs on all the west team's computers whenever they score points then the score will be updated on everyone's machines as changes occur.
Since the 1.09 beta, we have a new command that helps us react to when publicVariables change: addPublicVariableEventHandler. Say we want to display a hint every time the score changes. We can use this command to do this for us.
"teamScoreW" addPublicVariableEventHandler {hint format ["West Score: %1", (_this select 1)]}; // Display a hint clients that did not broadcast a new public variable value.
Just remember, addPublicVariableEventHandler does not work in 1.08 and earlier. Also note that this code will NOT display a hint on the client that broadcasted the public variable, only those clients that did not change it. So, if you want to display a hint on Player A's client as well we have to add a new line to the code where we broadcast the public variable:
teamScoreW = teamScoreW + 10; // Add 10 points to the west team's score.
publicVariable "teamScoreW"; // Broadcast the score to all clients and the server.
hint format ["West Score: %1", teamScoreW]; // Display a hint on this client only with the new score.
I don't know of any scoring templates because scoring needs can vary from mission maker to mission maker. Here is a post I made in another thread that describes one way:
Re: Support-script [help]Lastly, there are various ways of changing scores. For the scenario you described:
+1 to wscore each time an opfor player is killed or vice versa, taking into account teamkills and falling etc.
You could use a
"Killed" event handler. A killed event handler will record two values whenever a unit has been added one. The unit assigned the event handler (i.e. that is killed) and the object that killed it (the killer). These are recorded in the magic variable _this:
_this select 0: Unit assigned the event handler
_this select 1: object that killed it
Here is how we can use it to fulfill your scenario above:
Add this code to the unit's init line in the editor:
this addEventHandler ["killed", {_this execVM "scoring.sqf";}];
This code will add a killed event handler to that unit and whenever that unit dies it will execute scoring.sqf and pass to it the magic variable _this (which contains the information of who was killed and who killed it).
Then, create a script called scoring.sqf:
private ["_killed", "_killer"]; // Introduce local variables to the innermost scope.
_killed = _this select 0; // Record the the killed unit in a local variable.
_killer = _this select 1; // Record the killer in a local variable.
If ((side _killed) == (side _killer)) exitWith {hint "Team Kill or Accident - No Points}; // If the the unit that was killed and the unit that killed it are on the same side then exit the script without adding points.
teamScore = teamScore + 1; // If this was a legitimate kill then add one point to the other team.
// Add publicVariable code here if applicable.
The above script receives the notification of whom was killed (_this select 0) and the killer (_this select 1) and records them for local use. Then if the killed and killer are on the same side or the killed unit killed himself (fall, collision, etc.) then the script exits without adding points (If a unit kills itself then it will be the killed and the killer and obviously on the same side). If someone from another side killed the unit then the points are added. If you want to broadcast the new value insert publicVariable where I noted in comments.
I have not tested any of the above code so excuse any typos or syntax errors but this should be enough to get you started. Good luck!