Jump to content
Sign in to follow this  
seba1976

What does it means to publicVariable an object?

Recommended Posts

I found this on the bug tracker. I'd like to ask a question regarding this specific bit of code:

obj = createVehicle ["C_Offroad_01_F", position player, [], 0, "NONE"];
obj setvariable ["myvar","test",true];
publicVariable "obj";

What does the sentence publicVariable do in this context, where the parameter is representative of an object? I don't get it at all. I thought it was enough, for a client to retrieve the variable from an object, using getVariable, to make the last argument of the setVariable, true. I've used many times publicVariable, along with addPublicVariableEventHandler, but always using variables as arguments, never objects like in this case.

Share this post


Link to post
Share on other sites
I thought it was enough, for a client to retrieve the variable from an object, using getVariable, to make the last argument of the setVariable, true.

Broadcasted setVariables are always present, but if there's no name to know the object by, it can be hard to access it. Essentially you don't "publicVariable the object" when you do this, you pV a variable that has the object as its value. In other words, it's to create a way to refer to the object, which isn't around by default.

To clarify, if you put something in the "name" field in the editor, it creates a variable by that name and assigns the object as its value (and you can be sure that all clients know that identifier, because it's done when the mission is being loaded). But you can also assign the object as a value to other variables, and they'll work identically to the one in the name field, provided they've been defined on every client you need them on.

On the other hand, vehicles/units/triggers created via script can be easily defined only on the client that creates them. Executing publicVariable on a variable that's defined while creating the vehicle is the easiest way to make sure that every client has a way to refer to the object. createUnit has the init parameter that you can use to define an identifier, but createVehicle and createTrigger do not.

Sorry if this is hazily put, I've not slept for a while.

Share this post


Link to post
Share on other sites

To sum it up:

publicVariable "myVariable";

Executed on the server, the variable becomes available for every client connected to the server.

Executed on a client, the variable becomes available for the server AND all clients. If you want it to be available for the server only, use publicVariableServer.

In both cases, the variable will only be broadcast once "as-is". That means, if you change the variable after you "publicVariable-ed" it, you need to "publicVariable" it again to broadcast the "updated version".

In the corresponding BIKI article, you find all information you need.

Edited by waltenberg

Share this post


Link to post
Share on other sites

When you publicVariable something ,you send it to all clients on the server and server too !

Imagine he used createvehiclelocal ,the object will be then existent only on the client who used that command, publicvariable 'ing that command would create that vehicle on each client side!

Share this post


Link to post
Share on other sites
When you publicVariable something ,you send it to all clients on the server and server too !

Imagine he used createvehiclelocal ,the object will be then existent only on the client who used that command, publicvariable 'ing that command would create that vehicle on each client side!

Soooooooooooooooo not true! publicVariable does not create local objects on other clients. It only broadcasts the value, and since local object can ONLY exist on one client, the value of this object on other clients will be NULL.

Share this post


Link to post
Share on other sites
Broadcasted setVariables are always present, but if there's no name to know the object by, it can be hard to access it. Essentially you don't "publicVariable the object" when you do this, you pV a variable that has the object as its value. In other words, it's to create a way to refer to the object, which isn't around by default.

...

On the other hand, vehicles/units/triggers created via script can be easily defined only on the client that creates them. Executing publicVariable on a variable that's defined while creating the vehicle is the easiest way to make sure that every client has a way to refer to the object. createUnit has the init parameter that you can use to define an identifier, but createVehicle and createTrigger do not.

Exactly what I was asking about. Thanks a lot. You would still the addPublicVariableEventHandler on the other end, right?

Share this post


Link to post
Share on other sites
You would still the addPublicVariableEventHandler on the other end, right?
No. Only if you wanted to know that the variable had been updated.

Basically think of PV as a command for syncing variables.

If the variable exists on the receiving client machine it is updated. If it does not exist, it is created and set to the value that was broadcast.

If you specifically want to know WHEN that variable is updated use a PVEH.

Edited by Larrow
scrubbed 'client' may have been confusing, as its not necessarily a client

Share this post


Link to post
Share on other sites
No. Only if you wanted to know that the variable had been updated.

Basically think of PV as a command for syncing variables.

If the variable exists on the receiving client it is updated. If it does not exist, it is created and set to the value that was broadcast.

If you specifically want to know WHEN that variable is updated use a PVEH.

Perfectly clear now. Thanks.

Share this post


Link to post
Share on other sites
Imagine he used createvehiclelocal ,the object will be then existent only on the client who used that command, publicvariable 'ing that command would create that vehicle on each client side!

That doesn't sound right at all!

Share this post


Link to post
Share on other sites
Soooooooooooooooo not true! publicVariable does not create local objects on other clients. It only broadcasts the value, and since local object can ONLY exist on one client, the value of this object on other clients will be NULL.

If you broadcast a script with createVehicleLocal using publicvariable , it won't create that object on every client ? since creating the vehicle locally is what should actually be broadcasted ???

Unless objects created with that command are visible to other clients! then i am wrong :)

Edited by Xalteva

Share this post


Link to post
Share on other sites

The object is created locally (on the single client running it), as the name suggests.

---------- Post added at 11:32 PM ---------- Previous post was at 11:31 PM ----------

if you PV it, you're sending: "hey all other machines i just created this variable and it's referenced to this object, which you guys have no idea what it is, because i created it locally".

Share this post


Link to post
Share on other sites
The object is created locally (on the single client running it), as the name suggests.

---------- Post added at 11:32 PM ---------- Previous post was at 11:31 PM ----------

if you PV it, you're sending: "hey all other machines i just created this variable and it's referenced to this object, which you guys have no idea what it is, because i created it locally".

And no one will see that object ? i get it now !

thanks ;)

Share this post


Link to post
Share on other sites

I think, if it hasn't already been said that everyone will have a reference to the object, but only the person on whose machine it was created can actually see it.

Share this post


Link to post
Share on other sites
If you broadcast a script with createVehicleLocal using publicvariable , it won't create that object on every client ? since creating the vehicle locally is what should actually be broadcasted ???

Unless objects created with that command are visible to other clients! then i am wrong :)

I think you might be a bit confused :)

[color="#FF8040"]myquad [color="#8B3E2F"][b]=[/b][/color] [color="#7A7A7A"]"C_Quadbike_01_F"[/color] [color="#191970"][b]createVehicleLocal[/b][/color] [color="#191970"][b]position[/b][/color] [color="#000000"]player[/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]publicVariable[/b][/color] [color="#7A7A7A"]"myquad"[/color][color="#8B3E2F"][b];[/b][/color][/color]

will create variable "myquad" on all clients, present and future, but it will be NULL on all of them, apart from your own client

[color="#FF8040"]myscript [color="#8B3E2F"][b]=[/b][/color] [color="#8B3E2F"][b]{[/b][/color]
[color="#7A7A7A"]"C_Quadbike_01_F"[/color] [color="#191970"][b]createVehicleLocal[/b][/color] [color="#191970"][b]position[/b][/color] [color="#000000"]player[/color][color="#8B3E2F"][b];[/b][/color]
[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]publicVariable[/b][/color] [color="#7A7A7A"]"myscript"[/color][color="#8B3E2F"][b];[/b][/color][/color]

will create variable "myscript" with the code on all clients present and future but it will not create quadbike anywhere.

Share this post


Link to post
Share on other sites
I think you might be a bit confused :)

[color="#FF8040"]myquad [color="#8B3E2F"][b]=[/b][/color] [color="#7A7A7A"]"C_Quadbike_01_F"[/color] [color="#191970"][b]createVehicleLocal[/b][/color] [color="#191970"][b]position[/b][/color] [color="#000000"]player[/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]publicVariable[/b][/color] [color="#7A7A7A"]"myquad"[/color][color="#8B3E2F"][b];[/b][/color][/color]

will create variable "myquad" on all clients, present and future, but it will be NULL on all of them, apart from your own client

Ah right. I assumed that everyone else will essentially have a reference to an non existing object. Meaning they might be able to affect it with commands that don't have to be local to the object, for example setpos.

Share this post


Link to post
Share on other sites
Ah right. I assumed that everyone else will essentially have a reference to an non existing object. Meaning they might be able to affect it with commands that don't have to be local to the object, for example setpos.

They will (myquad isEqualTo objNull) => true

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  

×