Jump to content
  • 0

publicVariableEventHandler Client & Server Performance


hambeast

Question

Ok so I'm being a bit ambitious and creating a sweet admin-controlled event system.  Been picking AxeCop's brain this weekend and figure I bugged him enough. 

 

For this example, I need to display an admin menu if the following conditions are met (Event_Started = true, Match_Started = true, Round_Started = true)

 

These variables will be initialized and handled on the server side.  However, I need my admins to know the status so that certain menus can be called depending on the status.  Aka if I start an event, wait till player slots are assigned before starting match.  The variables MUST be broadcast to all JIP and new clients ( I know how to do this so far ), so publicVariableEventHandlers are the best solution for me.

 

I have two options as I see it to grab these variables.

 

 

1. Have a separate handler for each variable

ex: 

"PV_Event_Started" addPublicVariableEventHandler { Event_Started = _this select 1; };
"PV_Match_Started " addPublicVariableEventHandler { Match_Started =  _this select 1; };
"PV_Round_Started " addPublicVariableEventHandler { Round_Started = _this select 1; };

2. Have a single handler for all variables that expects all 3 booleans to be passed at once

"PV_StatusHandler" addPublicVariableEventHandler {
  private ["_packet"];
  _packet = _this select 0;
  Event_Started = _packet select 0;
  Match_Started = _packet select 1;
  Round_Started = _packet select 2;
};

I think #2 looks cleaner but I feel both solutions are dirty...  Passing 3 booleans every time a status updates when I only need to pass 1 is kind of dumb.  Feels like too much data being handled would slow things down.

 

However, having the client handle 3 events vs 1 sounds dirty as well.

 

 

My question is mainly which of these methods would work the most efficiently?  

 

Possibly I could branch out #2 to use a switch statement to only update whatever variable is passed to it.

 

 

edit: guess I kind of answered my own question.  In looking over these options, I feel below is the most elegant of solutions.  It requires me to write the least amount of code, handle the least amount of data, and it is modular in that I can use it for more than just this project.

 

like this:

// populate client data
"PV_StatusHandler" addPublicVariableEventHandler {
  private ["_packet"];
  _packet = _this select 1;
  _variableName = _packet select 0;
  _value = _packet select 1;


  // determine what variable we want to update
  switch (_variableName) do {
    case "Event_Started": { Event_Started = _value };
    case "Match_Started": { Match_Started = _value };
    case "Round_Started": { Round_Started = _value };
  };
};
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Ok so here's what I came up with:

 

info: Ok so here we are declaring handlers to handle... our variables.  If PV_IJS_EventStatus is broadcast as a publicVariable, the server and client will both populate their local variables with the value.  what if a client joins after these variables have been set? how do we sync them?  We sync them by calling PV_IJS_JIP_EventStatus as publicVariableServer which only calls the server side handler.  The server in turn broadcasts the values of the event (as defined) back to the client.

 

 

server side, call this whenever a variable we must track on server/client is updated.

// update our variables localy and make sure they populate to the clients.
if (isDedicated) then {
	"PV_IJS_EventStatus" addPublicVariableEventHandler {
		private ["_packet","_variableName","_value"];
		_packet = _this select 1;
		_variableName = _packet select 0;
		_value = _packet select 1;
		
		// assign variable value
		switch (_variableName) do {
			case "Event_Started": { Event_Started = _value };
			case "Match_Started": { Match_Started = _value };
			case "Round_Started": { Round_Started = _value };
		};
	};
};

server side JIP handler:

// handle JIP event status synchronization
if (isDedicated) then {
	"PV_IJS_JIP_EventStatus" addPublicVariableEventHandler {
		private ["_packet","_sender","_senderID"];
		_packet = _this select 1;
		_sender - _packet select 0;
		
		_senderID = owner _sender;
		
		// send Event_Started to JIP client
		PV_IJS_EventStatus = ["Event_Started", Event_Started];
		_senderID publicVariableClient "PV_IJS_EventStatus";
		
		// send Match_Started to JIP client
		PV_IJS_EventStatus = ["Match_Started", Match_Started];
		_senderID publicVariableClient "PV_IJS_EventStatus";
		
		// send Round_Started to JIP client
		PV_IJS_EventStatus = ["Round_Started", Round_Started];
		_senderID publicVariableClient "PV_IJS_EventStatus";
	};
};

client side handler to set variables we get from server:

// retrieve data from server and populate to local variables
if (!isDedicated) then {
	"PV_IJS_EventStatus" addPublicVariableEventHandler {
		private ["_packet","_variableName","_value"];
		_packet = _this select 1;
		_variableName = _packet select 0;
		_value = _packet select 1;
		
		switch (_variableName) do {
			case "Event_Started": { Event_Started = _value };
			case "Match_Started": { Match_Started = _value };
			case "Round_Started": { Round_Started = _value };
		};
	};
};

client side JIP check (bottom of init.sqf)

// handle JIP event status synchronization
if (!isDedicated then {
	private ["_sender"];
	
	PV_IJS_JIP_EventStatus = [_sender];
	publicVariableServer "PV_IJS_JIP_EventStatus";
};
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Discord

×
×
  • Create New...