This is an edited version of General Barron's classical MP sticky "How do scripting commands behave in MP?". Many scripting commands work in very unexpected ways, in a MP environment. The purpose of this thread is to find out how.
What this thread is for
We want to know how OFP scripting commands work in MP:
- Will the effect of the command be seen by all computers (globally), or only by the client it was run on (locally)?
- What happens when you pass a local object to the command? What about a remote object?
- How is the AI affected by this command in all of the above situations (if applicable)?
- Is there anything else that must be done to get the command working properly in MP? Are there any other ways this command behaves differently in MP than in SP?
What this thread is NOT for
- Asking questions about how to solve your personal scripting problems.
Only report your discoveries here! Do not ask questions in this thread!
Discussion and questions are only allowed if they concern how scripting commands behaves in general in a MP environment.
Commands covered so far
action
addAction
animate
animationPhase
camCommand
- camCommand
- camCommit
- camCommitted
- camCreate
- camDestroy
- cameraEffect
- camSetBank
- camSetDir
- camSetDive
- camSetFov
- camSetFovRange
- camSetPos
- camSetRelPos
- camSetTarget
- camSetTarget
createVehicle
cutObj
cutRsc
cutText
deleteVehicle
distance
leader
objStatus
player
publicVariable
random
setpos
setvelocity
setdammage
setViewDistance
setFog
setOvercast
setRain
side
skipTime
titleText
titleCut
titleRsc
Is it local, global, private or public?
The difference between the global and the local scope causes as much confusion for beginner MP scripters as it does for the experts. Here's a simple explanation of the terms, as they are used in this thread.
We need to be able to tell the difference between local/global commands and private/public execution of commands.
When a command is started from a script, that only runs on the server or a single client, we say that it's execution is private. If the effects is then seen on all computers, the command is global. deleteVehicle and setDamage are both good examples of global commands who works like this - they are fool proof. A unit that is deleted or killed on one computer will be deleted or killed on all computers.
When a command is executed on all computers simultaneously we say that it's execution is public. If it is a local command, then the effects will only be seen once, on all computers (exactly as in a SP environment). However, if a global command is executed in public, then the effect will take place once for each computer in the network (4 players on a dedicated server would execute the command 4+1=5 times). createUnit is a good example of a global command that should never be executed in public (unless you want to create a mess). If we, following our 4+1 example above, wish to create a soldier, and we execute the createUnit command in public on all computers, then we would create five soldiers instead of one.
Fragorl came up with the following summary, that shows how global/local works when they are executed in private/public:
global command + public execution = repeated command on all computers
global command + private execution = single command on all computers
local command + public execution = single command on all computers
local command + private execution = single command on one computer
Local and remote objects
All units, vehicles and objects are either local or remote. A local object is a unit or vehicle controlled by the computer you are playing on. Your player unit, as an example, is always local to you. AI-only groups are always local to the server. Mapobjects are local to everyone. Vehicles should be considered local to players - or AI under a players command - riding in them (this needs testing though, what happens if two players, in different groups, ride in the same vehicle?)
Commands list [To be continued...]actionThe action command should be run where ever the unit performing the action is local, for players or AI in a players group it is usually best to run it on all computers. Results and effects will be seen on all computers.
addActionThe action added will ONLY appear to the computer who executed the command. So if a client adds an action to an object, none of the other player's or their AI will be able to see/use the action. When a client adds an action to that machine's player, then only THAT player (and AI under his command) will be able to see/use that action. So if you want all players to be able to see and use an action, you have to make sure the addaction command is run on all computers. When a player uses the action, the attatched script will only be run on that player's computer.
animateThis must be run on only one machine. It doesn't seem to matter wich one, but the server is recommended. If a vehicle with animated parts recieves multiple "orders" from several computers it will most likely behave very unpredictable. (i.e one computer tells it to open the door, when the next computer tells it to start shutting the doors, and so on.) This has to do with synchronization between the server and the clients, and is not a problem connected with local/global space or private/public execution.
animationPhaseSee comments on animate above. Might return a different value from different computers, due to desync and lag. Works best on the server.
camCommand and camCreate - Local
All camera type commands behave in the same way. They are strictly local and must be created on each computer to work properly. This applies to the following commands:
- camCommand
- camCommit
- camCommitted
- camCreate
- camDestroy
- cameraEffect
- camSetBank
- camSetDir
- camSetDive
- camSetFov
- camSetFovRange
- camSetPos
- camSetRelPos
- camSetTarget
- camSetTarget
Example - camCreate
The effects of this command will only be seen by the computer the command is run from. This means camera cutscenes must be started on each computer individually. Bullets, explosives and bombs made with this command will only explode on the computer where the command is run, but damage will be registered on all machines. You can use this command for creating effects, never use it to create units in game (see createVehicle command).
createVehicle - Global
createVehicle will create the desired vehicle on all machines, even if it's run locally on only one computer. Hence the command works best if it is run only on the server. Otherwise one vehicle will be created for each computer that participates in a MP mission. With a dedicated server and four players that would create 4+1=5 vehicles in game if the script is run on all machines.
cutObj - Local
These objects are local and can be displayed on one computer, but remain invisible to the others.
cutRsc - Local
These resources are local and can be displayed on one computer, but remain invisible to the others.
cutText - Local
This text is local and can be displayed on one computer, but remain invisible to the others.
deleteVehicle - Global
Always works normal in MP, regardles of where the command is run. If a vehicle is deleted on one computer it will be deleted on all computers. Period.
distanceAlways meassure distance on the server. Technically the command is global and should return the same value, no matter where it is run, but since lag and desynchronization can produce different results on server and client the result would be unpredictable. The server is always right.
leader - Local
In MP this command will always return the original leader of a group, even after he dies (at which time it will return his corpse, which become static objects on the 'civilian' side). Notice that in SP, this command will always return the current leader of a group (so it will always return a living unit when the group has at least one member). This may not stay true after adding new units to the group via the "join" or "createunit" commands. (need more research). This command will work on local or remote objects (if a unit is passed). If passed a group, the group must be defined locally.
moveThis command will only work for sure if the passed groupname is defined on all computers. A group is usually defined in the init field of the commander using the syntax: myGroup = GROUP this
Notice: In MP there is always a short delay, a few seconds, before the groupname is defined on all machines in the network.
objStatus - Local
Status "ACTIVE", "FAILED", "DONE","HIDDEN" is local and may be different on all computers. Thus it is possible for an objective to be completed on a client, but not on the server. Very useful in Death Matches with different briefings for different sides.
player - Local
This useful command will return something different on each computer. It is always local and will always return the current body of the player who's computer runs the command. On a dedicated server this command will return null.
publicVariableThis is one of the most useful scripting commands a MP mission maker or scripter can find. Let's say you have a variable that you know is local, it only exists on one machine. Now it's time for publicVariable. PublicVariable "broadcasts" the variable to all computers every time you use the command. So if you had the following script run on the server:
a = 4
publicvariable "a"
a = 6
Then "a" would be 4 on all computers, except on the server, where it would be 6. Each of those computers could then change their own, personal "a" variable as much as they like. However, once any computer broadcast "a" followed by the publicvariable command again, then everyone else's "a" would be set to that one value (after which they could be changed again, and so on). Basically, "publicvariable" should really be thought of as "broadcast a private variable in public".
random - Local
This is local to each computer. If you're running a script on all machines that uses a random function you will need to make sure that just ONE machine rolls the dice then passes it's results to all other machines via a server routine.
setpos - Global
This command works normally in MP, regardless of what computer it is run on, or whether the object affected is local or remote.
setvelocity - Global
This command works normally in MP, regardless of what computer it is run on, or whether the object affected is local or remote.
setdammage - Global
This command works normally in MP, regardless of what computer it is run on, or whether the object affected is local or remote.
setViewDistance - Local
ViewDistance can be different on two computers. Thus it is possible to set alow viewDistance for infantry and a higher one for pilots in the air, for example.
setFog - Local
The environemental changes from this command will ONLY take place on the computer where the command is run from. It is entirely possible for one client to see fog, while another sees a clear day.
setOvercast - Local
The environemental changes from this command will ONLY take place on the computer where the command is run from. It is entirely possible for one client to see clouds, while another sees a clear sky.
setRain - Local
The environemental changes from this command will ONLY take place on the computer where the command is run from. It is entirely possible for one client to be soaked, while another remains dry.
side - Local
This command work exactly as the command player, exept that it returns the side of any unit. There is a difference between the "side" data type and what gets displayed when you convert a side into a string. When you convert a side into a string, it is converted into a case sensitive string:
Side (data) vs. Side (string)
west - "WEST"
east - "EAST"
resistance - "GUER"
civilian - "CIV" (not entirely sure)
enemy - "ENEMY"
skipTime - Local
This command must be run on all computers. Otherwise the time will only change on the computer where the change is made. It is possible for one player to see night while another sees day.
titleText - Local
TitleText is local to each computer so you can display text to one computer that is not visible on the others.
titleCut - Local
Titlecuts are local to each computer so you can fade one player to black while all others retain normal vision.
titleRsc - Local
These resources are local and can be displayed on one computer, but remain invisible to the others.
The original thread was started by General Barron, and all credits for the initiative should go to him and the people who answeered his questions in the forums. This is an edited, and somewhat cleaner version, summarizing the results of that thread. All mistakes in it are mine, and the original authors quoted in this text are not to be blamed for any erroneous information that made it's way into the manuscript during editing.
- Nominesine, August, 2006