Jump to content
Sign in to follow this  
Socrate

Public Variable, JIP, isServer

Recommended Posts

Hi all i'm here for having a couple of answers from you gurus of scripting! first let me say that i've a good experience with programming languages.. i made a script for random raspawn, i tested it with a clan mate and seems to work...

if(isServer) then {

respawnX=0;
respawnY=0;

_i=floor(random 3);

if(_i==0) then {
	respawnX= getMarkerPos "markerRespawn1" select 0; 
	respawnY= getMarkerPos "markerRespawn1" select 1;
};

if(_i==1) then {
	respawnX= getMarkerPos "markerRespawn2" select 0;
	respawnY= getMarkerPos "markerRespawn2" select 1;
};

if(_i==2) then {
	respawnX= getMarkerPos "markerRespawn3" select 0;
	respawnY= getMarkerPos "markerRespawn3" select 1;
};

PublicVariable "respawnX";
PublicVariable "respawnY";

};

onPlayerConnected "Server exec""update.sqs"" ";

Player setPos[respawnX,respawnY,0];

1) should i put the declaration of the variable respawnX and respawnY

before the isServer block?!

2) i read that the PublicVariable "variableName" command syncronize the value of the variable between clients...but suppose i've 2 clients with a different value of that variable like one true and one false..what will be the effect of the PublicVariable command?

3) Related to the point #2: does the publicvariable command syncronized the value of a the variable to the value that has on the server?

4) Related to point #1: in my script how does the client know the value of respawnX and respawnY if the declaration of those variables are in the isServer block?!

5) In all these questions the differences between sqs and sqf can bring some kind of issues? cause if i'm not in wrong i read somewhere that the sqf is kept in cache istead the sqs is always read..

6) does the server act like a normal client? i mean obviusly both client and server exec at start the init.sqs/sqf , but on the trigger activation? i mean suppose that a player activate a trigger that exec a script; is the script executed only for the client who activated the trigger or on all clients or on all clients and on server too?

7) related to #6 suppose a player activate a trigger fullfilling a mission object, suppose that on activation that trigger exec a script that have to set a variable object to true like this ( object=true). <---this operation must be done by the client or better putted in another script and executed only by the server ( who will exec object=true;publicvariable object) ?

sorry for my bad english questions, these are just a couple..but i've more to ask you...hope that some at bohemia who designed the scripting language can help me and maybe write a clear and exaustive guide to how the client/server script system works...in other words for those who use UML i need a sort of sequence diagram of script execution

Edited by Socrate

Share this post


Link to post
Share on other sites

Hello, I just want to point out you can shorten this script doing this:

if (isServer) then
{
   if (isNil "respawnX") then {respawnX = 0};
   if (isNil "respawnY") then {respawnY = 0};

   _respawnMarkers = ["markerRespawn1", "markerRespawn2", "markerRespawn3"];
   _markerChoice = _respawnMarkers select (ceil(random(count _respawnMarkers - 1)));
   respawnX = getMarkerPos _markerChoice (select 0);
   respawnY = getMarkerPos _markerChoice (select 1);

   publicVariable "respawnX";
   publicVariable "respawnY";
};

onPlayerConnected = "Server exec ""update.sqs""";

waitUntil {!isNull player};
player setPos [respawnX , respawnY , 0];

1) Keep the declaration of the variables where they are. My understanding from Spooner is defining the variable on the server, is a quick easy way to keep the values syncronized between clients.

2) The publicVariable with send the value to all players, from the machine it is called from.

Ex: Client1 has respawnX = 55.222. Then Client1 execs script that updates respawnX and publicVariable's it. Now ALL clients will have respawnX value of 55.222

3) To my understanding, the server always has the most up to date, or at least the last publicVariable'd value.

4) See above.

5) Not sure what your asking in this. If I read right, I believe it depends how your calling the script. if you use it once, or if their is a loop inside it, etc. Also all arma scripts run first and have the highest priority before your scripts if I'm correct.

6) Triggers have global effects.

----------

Now a quick question, if your meaning only to run this once, and have the current respawn point syncronized with JIP clients then you would want to change the script a bit.

Edited by DaChevs

Share this post


Link to post
Share on other sites

You can also just use 1 array variable if you like, since public variables now support arrays. Eg: PV_respawnArray = [0, 0]

    _markerChoice = _respawnMarkers select (ceil(random(count _respawnMarkers - 1)));

I believe that code is unsuitable and doesn't work as expected, since I had a similar issue with someone using it recently because random returns decimals not whole numbers. So if count = 2, gives (ceil(random 1)) where "0 <= random(1) < 1" which always returns 1 and almost never returns 0

(ceil(random(count _respawnMarkers - 1)));

So use this instead (which I know works):

(floor(random(count _respawnMarkers)))

1) if there is any risk of the client referencing those variables before they are initialised, then the safest option is to do:

if (isNil "var") then {var = 0};

like in DaChevs post, but outside of the block. This will ensure the variable is initialised with some value before use and also ensure you don't accidently overwrite an already initialised public variable.

2) see DaChevs post

3) No it doesn't synchronise the value from the server. The best practice and most efficient traffic control is to have the server control the variable value and do all the publicVariable calls.

4) see (1) above

5) Don't see relevancy. I think sqf might be cached, but if you edit the file in the meantime and reload it, the updated copy will be used. It shouldn't be a concern.

6) No, triggers are local and have local effect only. It's the condition which determines which client will execute their own triggers, which is usually all anyway. If you use editor defined triggers, it automatically creates a local trigger on each client which gives the illusion of global effect. If you create the trigger manually using code, then the trigger is local to each client you created it on.

7) Both techniques are good, but the public variable option is safer if you intend to support JIP, since the joining player will then also have an up to date value, whereas using the other method he may not (depending on the condition). You can restrict the code to server anyway using

x=99; if (isServer) then {publicVariable "x"};

Edited by Dr_Eyeball

Share this post


Link to post
Share on other sites

First thanks for the replies! ;)

i will list my further doubts:

You said :

7) Both techniques are good, but the public variable option is safer if you intend to support JIP, since the joining player will then also have an up to date value, whereas using the other method he may not (depending on the condition). You can restrict the code to server anyway using

x=99; if (isServer) then {publicVariable "x"};

So are you saing that, even if a dedicated server isn't playing ,when a player activate a trigger, set with the editor, its "on act" field is executed even by the server? Cause if this is the way things works, in every script executed by a editor's inserted trigger i can always write a "isServer" condition for executing the code that updates the global variables. I thought like you said that

6) triggers are local and have local effect only.
. But what you said about the condition it's interesting that is
It's the condition which determines which client will execute their own triggers,
so using a publicvariable (a global "object") the trigger will be executed on all clients (maybe on server too). am i right?

Mixing your both answer,

-

if there is any risk of the client referencing those variables before they are initialised, then the safest option is to do:

if (isNil "var") then {var = 0};

-

Keep the declaration of the variables where they are. My understanding from Spooner is defining the variable on the server, is a quick easy way to keep the values syncronized between clients.

i understood that the definition of the variable is safe to be done both inside and outside the "isServer" block. The thing that matter is that only the server has to execute the publicvariable command (in this way we can't find clients with different public variable values).

If i'm not in wrong we can see the publicvariable more or less like we see in programming languages the shared variables betweend THREADS:

They can be safly read, but only one changed in mutual exclusion (we reach the mutual exclusion allowing only server to modify those variables).

One last question. which're the differences between a sqs and a sqf file then?! ok the second can use functions so it can be recalled by other scripts.but in the "plain" scripting (i mean without function or particular statements) situation can i use the sqf?

Share this post


Link to post
Share on other sites

Socrate you can get a lot of answers here http://community.bistudio.com

About triggers, to allow players see the score in a hint by pressing 0-0-X sequence which activates a radio trigger I did it 2 ways :

First was to place the trigger in the editor and when a player pressed 0-0-X sequence on keyboard all the other radio triggers of all the other players were activated and everyone gets the hint displayed.

Second I created the radio trigger by scripting commands in the init.sqf file (so all computers have the trigger) and this time when a player presses 0-0-X the radio trigger is only activated on his computer.

It seems that triggers placed with the editor are synchronized between all computers at least concerning radio triggers.

I think you can say this about a trigger :

Depends on how trigger was created with editor or by script,

on which machine the trigger exists,

on what conditions the trigger has (some units can exist only locally on one machine and so they will only activate the trigger on this machine)

what commands are executed on activation of the script (commands can have local or global effects).

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  

×