Jump to content
blackyesp

Help understanding how Variables work in MP

Recommended Posts

Hi once more!

So... I've been playing with the PublicVariable command for a while now, but I never really got to understand the workflow of Variables. I just used them as basic true/false trigger conditions and they always worked fine, so I never bothered to find out how they really work until I started my dedicated server today, invited a friend to play and realized that those triggers activated at the beginning of the mission without any reason.
Pissed, I tried to fix it by myself and maybe I wasn't using the right command or Var type that suited my needs, so I decided to do some research. I understood the difference between Local, Global and Public, but now I'm more confused, because more concepts as i.e. PublicVariableServer appearead and I still don't know which one to use for MP. Let me show you a simple example of my problem:
 

There are 3 Editor-spawned triggers:

 

trig1
OnAct: Dog = true; PublicVariable "Dog";
------

trig2

OnAct: Cat = true; PublicVariable "Cat";

------

trig3
Condition: Dog AND Cat    
OnAct: hint "It worked!"

------

 

All of this works fine in SP and also in dedicated MP alone. But whenever a player joins the mission (Not necessary JIP) the variables mess up and trig3 gets activated without any aparent reason.
I guess the answer is simple and probably I already found an explanation to my problem on the wiki pages, but I'm struggling trying to process all the new information. So, before I start changing stuff to fix it I want someone to please explain me what is happening and how to do it the right way.  :)

 

Thank you in advance.

 

 

 

Share this post


Link to post
Share on other sites

Try setting the triggers to server only and use publicVariableServer, should even work with JIP.

Also initialize the variables inside initServer.sqf.

Could work, not sure since I usually never work with triggers.

 

Cheers

  • Like 1

Share this post


Link to post
Share on other sites

pubilcVariableServer sends defined information (lets call it a packet) from a client (you, for example) to the server. This packet is normally picked up by a public variable event handler for proccesing. This would be used if a client has data that the server needs to process, for example, the set of addons the client is running could be sent via publicVariableServer and filtered through a defined addon whitelist/blacklist on the server. This command should also only be ran on a client

 

publicVariableClient sends a packet to a specified client. Same thing applies here for the event handler, whichever client it is intended for will be the proccessing entity in this case. This command should be executed on the server, to then send a packet to the defined client or set of clients. An example could be if the server wants to send a welcome message to a client that hasn't connected before. The message itself would be sent via publicVariableClient and picked up by that client and sent through the event handler to show a hint or similar with the message.

 

That's the quick and dirty of those two commands.

 

EDIT: The event handler isn't always necessary. A direct publicVariableServer "myVariable" will also suffice to share the value and reference of that variable for use by the server from a client. As GOM is pointing out.

  • Like 1

Share this post


Link to post
Share on other sites

You could also use:

 

missionNamespace setVariable ["Cat",true,true];//Will make this variable globally available, also for JIP players

Just make sure this code is only executed once on the server!

  • Like 1

Share this post


Link to post
Share on other sites

Thank you for the help, friends :)
I tested what GoM said and it seems to work now. I had more problems with some other vars and thanks to your explanations I managed to fix ir all :)

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

×