OFPEC Forum

Editors Depot - Mission Editing and Scripting => OFP - Editing/Scripting Multiplayer => Topic started by: FiLL2d on 22 Aug 2006, 03:37:30

Title: a couple of questions on public variables
Post by: FiLL2d on 22 Aug 2006, 03:37:30
can someone help me with a couple of questions on public variables.

1. Making a public variable transmits the variable's value amongst all machines, although if the variable changes, is the change transmitted to all the machines or must the variable be changed to public again for the changes to be recongized by everyone.

2. Does a loop making a variable public continously continue to build up data in the memory or does it overwrite/ignore it


thx in advance
Title: Re: a couple of questions on public variables
Post by: Garcia on 22 Aug 2006, 07:23:57
1: Public variable is not a type of variable, it is a command to make global variable public. If you got a global variable on one client that you wish to transfer to other clients, you PV it. When you change the information in the global variable, you have to PV it for other client to get the information. That is, unless the global variable is set to the same on all clients.

2: I'd think it overwrites...


Sorry for any unclear answers...it's early in the morning and I've gotta go so I don't miss my bus to the uni :zzz:
Title: Re: a couple of questions on public variables
Post by: SyB on 22 Aug 2006, 13:09:03
Hi there,

Think of the 'PublicVariable' command as more like a 'Broadcast' or 'Publicize' this global variable value to all computers type of command.

The one rule you should observe (in my opinion) is... "Only ever have 1 computer modifying and 'Broadcasting' the particular global variable" AND preferably (but not restricted) have this done on the server/host computer.

Clear as mud?  :dry:,  :afro:

Cheers
Title: Re: a couple of questions on public variables
Post by: Terox on 22 Aug 2006, 16:37:41
Additional Info

Publicvariable will not transmit
a) Strings:
eg "This is a string"
b) arrays:
eg   Tx_MyArray = [1,2,3.5,4.2]


to broadcast these across a network
You either
Transmit a number from machineA to machineB and have a script waiting for that number to be transmitted then compare it and select a string variable from it


eg

MachineA
Quote
tx_String = 1; Publicvariable "Tx_String"

MachineB
Quote
#START
@ tx_string > 0
?(tx_string == 1): Tx_Message = "hello"
?(tx_string == 2): Tx_Message  = "Goodbye"
hint tx_message"
tx_string = 0
goto "START"


or if you want to transmit dynamically created arrays or strings then you need to use CoC_Ns



and to transmit multiple publicvariables in one pass use

Tx_A= Etank
Tx_B = getpos (ETank select 0)
Tx_C = getpos (ETank select 1)
{publicvariable _x} foreach ["Tx_A",Tx_B","Tx_C"]
Title: Re: a couple of questions on public variables
Post by: Mr.Peanut on 22 Aug 2006, 19:17:39
Few other comments on publicVariable.

1) It is a good practice to initialise any variables you intend to publicVariable in your init.sqs, even if you just assign them dummy values.

2) You can only  publicVariable global variables i.e. variables without a leading underscore in their names.

3) The publicVariable command can be unreliable, especially when called near the beginning of a mission, or from the init.sqs, and extra especially if the network connections are laggy.  I have gotten in the habit of double pv'ing variables when near the start of a mission.
Title: Re: a couple of questions on public variables
Post by: Killswitch on 05 Sep 2006, 11:54:42
Few other comments on publicVariable.3) The publicVariable command can be unreliable, especially when called near the beginning of a mission, or from the init.sqs, and extra especially if the network connections are laggy.  I have gotten in the habit of double pv'ing variables when near the start of a mission.
People who use publicVariable in parts of the init.sqs that runs on every machine anyways, always, should be strung up in the nearest tree. 

(Obviously, if one has a init.sqs that's written so as to only execute on one machine, publicVariable use again becomes relevant.)
Title: Re: a couple of questions on public variables
Post by: h- on 05 Sep 2006, 12:16:02
Quote
People who use publicVariable in parts of the init.sqs that runs on every machine anyways, always, should be strung up in the nearest tree. 
:D :D
Read: "Don't use pV in init.sqs"


Wait, is there a nearestTree in OFP?  :dunno:
Title: Re: a couple of questions on public variables
Post by: Mr.Peanut on 05 Sep 2006, 18:15:21
Some people have used PV in the init.sqs for MP broadcasting of random numbers needed near the mission start. This is not recommended.  If you need to use PV near the mission start, put in a script with a non-zero pause at the top, and call the script from the init.sqs.  A pause must wait until the mission starts.  I had a lot of problems with a simple script that was to generate a random mission start time (which can also be done using by specifying a random number as a possible param in the description.ext but that is beside the point).  The only solution was this:
Code: [Select]
publicVariable "myNumber"
~random 0.1
publicVariable "myNumber"
~random 0.1
publicVariable "myNumber"
~random 0.1

One other thing. If you have multiple variables to PV at the same time do this:
Code: [Select]
{publicVariable "_x"} forEach [x1, x2, x3 .....] to help prevent lag.