Jump to content
Sign in to follow this  
ZNorQ

Removing an addPublicVariableEventHandler

Recommended Posts

To remove a PV EH for a specific variable, can I just use the following statement;

"theVariable" addPublicVariableEventHandler {};

ZNorQ

Share this post


Link to post
Share on other sites

Might seem a bit stupid what I'm asking for, so I'll add some extra info; Is the event completely removed from the event checks, or will it still lurk in the background and triggering no code ({}) whenever "theVariable" have been publicVariable'd? I do not want to use CPU on event handlers that are no longer needed.

ZNorQ

---------- Post added at 12:33 ---------- Previous post was at 12:30 ----------

Oh, another thing; Can the addPublicVariableEventHandler also delete itself?

"theVariable" addPublicVariableEventHandler { "theVariable" addPublicVariableEventHandler {};  };

This also might seems a bit stupid, but it would of course contain a lot more complex code, for example it could do some operations up to a point, and when certain criteria was met, it would remove it self.

ZNorQ

Share this post


Link to post
Share on other sites

Odd that removePublicVariableEventHandler doesn't seem to exist. *shrug*

For that you're wanting to do however you'll probably want to just use a trigger or == check or just a waitUntil or something simple like that. publicVariableEventHandlers seem to be for variables that will be changing often, if you just want to check once you wouldn't want to use this command.

Share this post


Link to post
Share on other sites

Oh, not at all, I will be using them frequently (for same variables), but sometimes there are certain conditions that occur where I want to remove the addPublicVariableEventHandler for a/several specific variable(s).

I just don't want to end up event handlers that triggers empty code just because I can't remove them from the event lists. CPU is a precious commodity; never enough! ;)

Share this post


Link to post
Share on other sites

The key word is add. All of your attempts above will just add another event handler that does nothing. Worse yet, this will just add more and more useless handlers every time the variable changes.

TAG_pubVarHandler =
{
  private ["_var", "_value"];
  _var   = _this select 0;
  _value = _this select 1;
  // your implementation
};
//Assign the handler
"TAG_pubVar" addPublicVariableEventHandler {_this call TAG_pubVarHandler};


// *snip*

// Handler no longer necessary, let's "remove" it
TAG_pubVarHandler = {};

Edited by Deadfast

Share this post


Link to post
Share on other sites
The key word is add. All of your attempts above will just add another event handler that does nothing. Worse yet, this will just add more and more useless handlers every time the variable changes.

TAG_pubVarHandler =
{
  private ["_var", "_value"];
  _var   = _this select 0;
  _value = _this select 1;
  // your implementation
};
//Assign the handler
"TAG_pubVar" addPublicVariableEventHandler {_this call TAG_pubVarHandler};


// *snip*

// Handler no longer necessary, let's "remove" it
TAG_pubVarHandler = {};

This was what I was afraid of. But your example doesn't stop the CPU spending time on useless events just by setting TAG_pubVarHandler to {}. But your solution is a good way to make the pvEH more flexible I guess.

And yes, before any of you go on claiming that a few EH's doesn't hug much CPU resources, I'm not too worried about that in a small framework, but when you add on multiple functionality, eventually every CPU tick counts.. I'm not quite there though myself, but I do want to make the code as CPU resource effective as possible.. ;)

Thanks for the feedback, Deadfast.

Share this post


Link to post
Share on other sites
And yes, before any of you go on claiming that a few EH's doesn't hug much CPU resources, I'm not too worried about that in a small framework, but when you add on multiple functionality, eventually every CPU tick counts.. I'm not quite there though myself, but I do want to make the code as CPU resource effective as possible.. ;)

Thanks for the feedback, Deadfast.

If you are not going to use the handler anymore then there is probably no reason to keep using publicVariable on that variable right? - Problem solved.

If that is not an option, then why not just use a single public event handler for everything? Like how CBA does?

Share this post


Link to post
Share on other sites
If you are not going to use the handler anymore then there is probably no reason to keep using publicVariable on that variable right? - Problem solved.

1) One of the reasons to my post was to learn how the addPVEH worked, and how to throw out (remove) old no-longer-needed addPVEHs. However, I see the conclusion of my first question (which also cancels my second question) is "can't be done". One just have to find workable way around, like the one Deadfast suggested. It would be nice to see a removePVEH, though.

2) Not quite, because certain clients still needs to receive the information, but only up to a point where it's no longer needed. Hence, stop using publicVariable isn't a solution to the "challenge".

If that is not an option, then why not just use a single public event handler for everything? Like how CBA does?

First off, I'm doing as much coding myself to learn (best way is to do it yourself) with great help from this community. Second, I wouldn't dare poke in other people products after reading quite a few "wars" on this forum; I just don't need the aggravation. Last, I'm on a bit of a time schedule, and trying to go through unfamiliar complex code and trying to understand it is a bit more time consuming than what I'd care for right now.

Thanks for the feedback though, Muzzleflash, much appreciated.

Edited by ZNorQ
Correcting spelling mistake(s)

Share this post


Link to post
Share on other sites

Yes, I know about these two functions, they are a welcome addition to the already huge pool of functions in ArmA2. However, they came a bit late as I've already implemented my back-ward code; too much hassle to re-write at this time, and I'm not even sure they will give me a solution to my current challenges... I will of course utilize these as much as I can.

Share this post


Link to post
Share on other sites

First off, I'm doing as much coding myself to learn (best way is to do it yourself) with great help from this community. Second, I wouldn't dare poke in other people products after reading quite a few "wars" on this forum; I just don't need the aggravation. Last, I'm on a bit of a time schedule, and trying to go through unfamiliar complex code and trying to understand it is a bit more time consuming than what I'd care for right now.

I don't actually know for sure, but I'd assume the way CBA does it is this:

TAG_ownPubVarHandlers = [];

TAG_pubVarHandler =
{
  {
     _this call _x;
  } forEach TAG_ownPubVarHandlers;
};
//Assign the handler
"TAG_pubVar" addPublicVariableEventHandler {_this call TAG_pubVarHandler};

//Functions for adding and removing stuff from TAG_ownPubVarHandlers...

Share this post


Link to post
Share on other sites

If you somehow know people should receive then maybe http://community.bistudio.com/wiki/publicVariableClient will be useful.

Yeah Deadfast something like that - slightly bit more involved since its events are based on names. E.g. example usage:

During init:

MyEventId = ["MyEvent", {
   hint format ["My event has happened at time %1", _this select 0];
}] call CBA_fnc_addEventHandler;

Some other time another machine can raise the event (send the message) do:

["MyEvent", [time]] call CBA_fnc_globalEvent;

Whilst the publicVariableHandler used cannot be removed, the event handler can:

["MyEvent", MyEventId] call CBA_fnc_removeEventHandler;

Maybe some of the commands was misspelled - was written from memory.

CBA does it by using a single publicVariableHandler and some object to store the events. For example all events called "MyEvent" could be stored in an array on that object. For example when the event "MyEvent" is raised it could do.

_events = EventStoreObject getVariable ["MyEvent", []];
{
    //Call all the handlers.
   _theArguments call _x;
} forEach _events;

As you see you can remove any given handler by removing it from the array.

I don't think you will get in trouble from using a CBA-like approach. I have a file which basically replicates the functionality in case my mission must not use CBA. Domination use(d) a very similar approach also.

Share this post


Link to post
Share on other sites

I'm already using Xeno's Domination network event concept which is basically built around one variable (used by publicVariable/addPublicVariableEventHandler) for handling all custom network events. This is a really good way of creating events between clients/server, and could for all I know be the same concept built in to CBA..? But that was not really the point of this topic; it was more for me to know the more detailed inner working of addPublicVariableEventHandler. Unfortunately it seems that once a aPVEH is set up, it cannot be removed.

Appreciate your feedback, guys, thanks.

ZNorQ

---------- Post added at 07:10 ---------- Previous post was at 06:53 ----------

One question I have to the CBA solution (And Xeno's for those of you familiar with his implementation in Domination); Is ArmA2(&OA) able to handle ALL events broadcasted, or may there be some collision?

Share this post


Link to post
Share on other sites

Not sure what you mean by events. If you mean data types, then yes, you can broadcast any data type you want.

Share this post


Link to post
Share on other sites
Not sure what you mean by events. If you mean data types, then yes, you can broadcast any data type you want.

I'm understanding it as you referring to my question with regards to collissions, Deadfast;

No, I'm not talking about data types. I'm talking about events like Muzzleflash mentioned with regards to CBA's CBA_fnc_globalEvent.

Now, I'm not familiar with CBA_fnc_globalEvent, but I can only guess it can be used to trigger custom events (by utilizing addPublicVariableEventHandler?) on all/certain clients/server. If CBA_fnc_globalEvent uses addPVEH - and only one variable for all such events - what is the probability that there will be a collision which will result in an event not being triggered as expected..? I know where talking pretty many events being broadcasted at once, combined with many clients, I guess.. Networking isn't exactly my area of expertise.. (nor is coding, for that matter...)

Sorry guys, as I said I'm not exactly the guru when it comes to coding and coding terminologies, so trying to explain what the hell I'm talking about isn't easy... Oo Hope you can grasp WTF I'm trying to say here... :P

ZNorQ

Share this post


Link to post
Share on other sites
But that was not really the point of this topic; it was more for me to know the more detailed inner working of addPublicVariableEventHandler.

ZNorQ

Yes, I know that. But in the first couple of post it was established that you cannot remove a publicVariableEventHandler. And your primary reasons for removing it was to, A: not have "take action" on a publicVariable anymore and B: save cpu time. Me bringing up was that there is indeed to way to accomplish both. Using this event-based approach, where the handler code is not tied directly to a publicVariableEventHandler gives you the flexibility to later remove the handler code as demonstrated. I know that you said that you are on time-pressure and do not have the time required to really get into this stuff, just wanted to let you know that there is indeed a way.

If CBA_fnc_globalEvent uses addPVEH - and only one variable for all such events - what is the probability that there will be a collision which will result in an event not being triggered as expected..? I know where talking pretty many events being broadcasted at once, combined with many clients, I guess.. ....

I have never experienced any issues even under when a lot of events (boiling down to publicVariable commands) are sent. It seems that anything sent using publicVariable is both reliable (it does not get lost), and sequential, (meaning if a single machine does: a = 10; publicVariable "a"; a = 20; publicVariable "a"; then they arrive in the correct order).

Good luck with your project :)

Share this post


Link to post
Share on other sites
Yes, I know that. But in the first couple of post it was established that you cannot remove a publicVariableEventHandler. And your primary reasons for removing it was to, A: not have "take action" on a publicVariable anymore and B: save cpu time. Me bringing up was that there is indeed to way to accomplish both. Using this event-based approach, where the handler code is not tied directly to a publicVariableEventHandler gives you the flexibility to later remove the handler code as demonstrated. I know that you said that you are on time-pressure and do not have the time required to really get into this stuff, just wanted to let you know that there is indeed a way.

Yepp, this is how I understood it too. ;)

I have never experienced any issues even under when a lot of events (boiling down to publicVariable commands) are sent. It seems that anything sent using publicVariable is both reliable (it does not get lost), and sequential, (meaning if a single machine does: a = 10; publicVariable "a"; a = 20; publicVariable "a"; then they arrive in the correct order).

Good luck with your project :)

Sounds good! I doubt I would end up with this problem in my current project anyway, but - again - it's a good thing to be educated. :)

Thanks a lot for the good wishes and feedback, Muzzleflash!

Share this post


Link to post
Share on other sites

As much as I don't want to bump a 2-3 year old thread and I'm sure users will have come across this issue by now, along with this thread (given that there is still no removeEH) but just for those who haven't figured it out yet you can use a single PVEH easily using switch cases with a id filter like so (ignore any obvious issues, I just put together a simplified version of my server side PVEH I run on my servers for logging of in-game admin events and cleanup of objects they spawn on the server after the event has finished

//PV Example 1 - logging things
_something = "Whatever";
_morestuff = "Random";

MY_PVEH = [0, _something, _morestuff];
publicVariable "MY_PVEH";

//PV Example 2 - Deletion of objects on server stored in array
_log = format["%1 Deleted the array of objects", name player]; //Player name in this example will be JIMBOB
OBJECTARR = ["classname1","classname2","classname3"]; //some random array of objects
publicVariable "OBJECTARR"; //PV this to all clients so anyone with access to script where this PV is run from can clean it up (Other ways of doing this with client side EH's)

MY_PVEH = [1, _log, OBJECTARR];
publicVariable "MY_PVEH";

//The EH
"MY_PVEH" addPublicVariableEventHandler {
_array = _this select 1; //Get array of elements and assign to local var so we can select separate elements easier
_id = _array select 0; //Get ID from 1st element in array passed to EH so we can filter what we need the EH to do
switch (_id) do {
	case 0 : {
		_log1 = _array select 1; //Get log1 from 2nd element of array passed to EH

		_log2 = _array select 2; //Get log2 from 3rd element of array passed to EH

		diag_log format["%1 this is %2", _log1, _log2]; //Log the results, in logs should be "Whatever this is Random"
	};
	case 1 : {
		_log = _array select 1; //Get log from 2nd element of array passed to EH remember we formatted this earlier with player name also JIMBOB in this example

		_objects = _array select 2; //Get objects from 3rd element of array passed to EH

		OBJECTARR = _objects; // No real need for this AFAIK, just makes things easier on my brain sometimes

		_logger = format ["Cleanup logger: %1 %2", _log, _objects]; //Log results, in logs should be "Cleanup logger: JIMBOB Deleted the array of objects ["classname1","classname2","classname3"]"
		diag_log _logger;

		{ deleteVehicle _x; } forEach OBJECTARR; //Objects get cleaned up

		OBJECTARR = [];
		publicVariable "OBJECTARR"; //Reset array back to blank and push to all clients
	};
};
};

Edited by KamikazeXeX

Share this post


Link to post
Share on other sites

I also use this addPublicVariableEventHandler with switch...

Now the question on optimization: is there a difference to use one addPublicVariableEventHandler with a switch, or use a lot addPublicVariableEventHandlers?

Do addPublicVariableEventHandler bear all the burden?

Share this post


Link to post
Share on other sites

I'd assume using multiple over a single with switch would cause more strain due to the fact you are having multiple events on different handlers, with a single one that filters I've noticed I don't get any temporal fluxes in performance of the server, I in particular run 2 DayZ Mod server and one ArmA 3 Epoch, all of which have a nice single event handler and all seems to work a lot smoother especially when throwing things to and from many clients

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  

×