Jump to content
Sign in to follow this  
Double Doppler

Public Variables and PVEHs

Recommended Posts

I want to know what are the best practices on usage of publicVariables and their PVEHs to send commands to run code. Also how do other scripters use PVEHs? I haven't found straightforward documentation on the BIKI yet (although I would like a link to some if there is).

Right now I have three different methods:

  1. Use a number variable and add a PVEH to it which uses the value select a string from an array and compile it upon PV recieval.
  2. Use an array variable and add a PVEH to use the value as a parameter for a function call upon PV receival.
  3. Use a string variable and add a PVEH to compile the string upon PV recieval.

They all have their positive and weak sides. For example:

  • Method 1 cannot accept parameters - yet it is very efficient because it only sends integers across the network.
  • Method 2 requires multiple variables & functions per different scripting command - but it is a blend of flexibility and efficiency.
  • Method 3 sends the most traffic, but this is still not very large due to it not being live code - yet it is very flexible since you can send any type of code you want through the same variable.

Here are the scripts:

Method 1:

   array_myPVcommands = [
       "",                         // No code as default
       "player setdammage 1",
       "_this  say ""taser_hit""", // Dont be concerned about the local variable
       "factory setdammage 1",
       "if (isPlayer _this) then {disableuserinput true}"
   ];

   pvCommandID = 0;
   "pvCommandID" addPublicVariableEventHandler {
       call compile (array_myPVcommands select (this select 1))
   };

   // Usage would be of the sort:
   pvCommandID = 1;
   publicVariable "pvCommandID";

Method 2:

   pvAnim = [objNull,""]; // Initialize variable to a suitable value, does this have advantages?

   "pvAnim" addPublicVariableEventHandler {
       [(_this select 1),false] call fnc_public_switchMove
   };

   fnc_public_switchMove = {
       _parameters = _this select 0;
       _who  = _parameters select 0;
       _what = _parameters select 1;
       _broadcast = _this select 1;
       _who switchMove _what;
       if (_broadcast) then {
           pvAnim = [_who,_what];
           publicVariable "pvAnim";  
       };
   };

   // Usage would be of the sort:
   [[player,"adthppnemstpsraswpstdnon_2"],true] call fnc_public_switchMove;

Method 3:

    
   pvStr = 'unit say "taser_hit"';
   "pvStr" addPublicVariableEventHandler {
       [(_this select 1),false] call fnc_public_broadcast;
   };

   fnc_public_broacast = {
       _code        = _this select 0;
       _broadcast = _this select 1;
       call compile _code;
       if (_broadcast) then {
           pvStr = _code;
           publicVariable "pvStr";  
       };
   };

   // Usage would be of the sort:
   format ["%1 switchMove %2",player,"adthppnemstpsraswpstdnon_2"] call fnc_public_broacast;

Which data type is the most taxing to send over the network?

Also is it better to have many public variables with small values? Or have one public variable with a medium sized value?

Is it more taxing to send the message itself (thinking of the header and packet encryption methods) or is it the value of the message that affects performance (size of string or array etc.)?

Edited by Double Doppler
Cleaned it up alot now

Share this post


Link to post
Share on other sites

Your second example is the most common, an all around example. although your first example is ok as well, just depends on your needs. That said... if you are going to be building a large system around this and it is going to be used in the addon/mission a lot you really should be looking at using the CBA event system .

Here is a good thread to look at:http://forums.bistudio.com/showthread.php?124663-Locality-of-Variables-Between-Client-Server There are other good threads about this as well if you search. But the short of it is CBA just does this better/more efficiently.

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  

×