Jump to content
Leopard20

Adding and deleting Event Handlers multiple times - is it bad?

Recommended Posts

Hey guys.

 

I was wondering which one is worse performance-wise:
1. Creating one EH and making it not work when not needed using "if (conditionNotMet) exitWith {};"

2. Adding the EH when needed and deleting it when it's done.

Particularly I'm interested in two types of Event Handlers:
1. KeyDown, added directly to display 46 like this: (findDisplay 46) displayAddEventHandler ["KeyDown", "_this call someCode"];
2. onEachFrame, created using BIS_fnc_addStackedEventHandler.

 

Any help would be appreciated.

Share this post


Link to post
Share on other sites

I would just keep the EHs and use exitWith when not needed. I don't think we are talking about big performance hit here.

Share this post


Link to post
Share on other sites
5 minutes ago, gc8 said:

I would just keep the EHs and use exitWith when not needed. I don't think we are talking about big performance hit here.

Thanks for your input.
What about the onEachFrame event handler? Doesn't checking a condition on every frame cause a performance hit?

Share this post


Link to post
Share on other sites
Just now, Leopard20 said:

Thanks for your input.
What about the onEachFrame event handler? Doesn't checking a condition on every frame cause a performance hit?

 

If just one condition in EachFrame is checked then I wouldn't worry about it.

  • Haha 1

Share this post


Link to post
Share on other sites
4 minutes ago, gc8 said:

 

If just one condition in EachFrame is checked then I wouldn't worry about it.

Yeah it's just one condition.
BTW, is a condition like this performance heavy?

if (isNull (findDisplay _idd)) exitWith {}

 

Share this post


Link to post
Share on other sites

@Leopard20

Well again I wouldn't worry about it but that's just me. if you want it faster you could do the findDisplay earlier and store it to variable and then check that variable for null. But is there any performance gain, I doubt :)

 

  • Thanks 1

Share this post


Link to post
Share on other sites
4 minutes ago, gc8 said:

@Leopard20

Well again I wouldn't worry about it but that's just me. if you want it faster you could do the findDisplay earlier and store it to variable and then check that variable for null. But is there any performance gain, I doubt:)

 

The trouble is I close and create this display multiple times!!! ;) So it needs to be checked constantly.
Anyway, thank you for your help!

Share this post


Link to post
Share on other sites
1 hour ago, Leopard20 said:

The trouble is I close and create this display multiple times!!! ;) So it needs to be checked constantly.
Anyway, thank you for your help!

 

Well if you're in control when this display is being created/closed, why the need for an eachFrame EH in the first place?

A simple isNull findDisplay (~0.0007ms) check shouldn't have any visible impact on performance, even when checking on every frame (16.6667 ms @60fps).

 

Cheers

  • Thanks 1

Share this post


Link to post
Share on other sites
16 minutes ago, Grumpy Old Man said:

 

Well if you're in control when this display is being created/closed, why the need for an eachFrame EH in the first place?

A simple isNull findDisplay (~0.0007ms) check shouldn't have any visible impact on performance, even when checking on every frame (16.6667 ms @60fps).

 

Cheers

Thanks.

 

I need onEachFrame because it creates a real time drawIcon3D.

Share this post


Link to post
Share on other sites
11 minutes ago, Leopard20 said:

Thanks.

 

I need onEachFrame because it creates a real time drawIcon3D.

 

for drawIcon3D "Draw3D" EH would be more suitable:

 

addMissionEventHandler ["Draw3D", { drawIcon3D ["targetIcon.paa", [1,1,1,1], ASLToAGL getPosASL cursorTarget, 1, 1, 45, "Target", 1, 0.05, "TahomaB"]; };

 

  • Like 4

Share this post


Link to post
Share on other sites
3 hours ago, gc8 said:

 

for drawIcon3D "Draw3D" EH would be more suitable:

 


addMissionEventHandler ["Draw3D", { drawIcon3D ["targetIcon.paa", [1,1,1,1], ASLToAGL getPosASL cursorTarget, 1, 1, 45, "Target", 1, 0.05, "TahomaB"]; };

 

Oh. Thanks for the tip!

If you don't mind me asking, how is it more suitable?

 

Also, I use multiple draw3DIcons at the same time. Is it gonna be ok using Draw3D EH?

Share this post


Link to post
Share on other sites
9 hours ago, Leopard20 said:

If you don't mind me asking, how is it more suitable?

 

I've seen people use drawIcon3D in EachFrame, it's not that it doesn't work, but typically video games have two phases, first is to run all the code the game needs to function and call things like EachFrame EH. Then the next phase is to render (draw) everything. So having drawIcon3D in the render phase is just the right place for it.

 

 

9 hours ago, Leopard20 said:

Also, I use multiple draw3DIcons at the same time. Is it gonna be ok using Draw3D EH?

 

Sure

  • Like 1

Share this post


Link to post
Share on other sites
On 1.8.2018 at 5:53 PM, Leopard20 said:

Adding the EH when needed and deleting it when it's done.

That's better. Adding a EH is just adding a string to an array. Just like the "pushBack" script command. It's very cheap.

 

 

On 1.8.2018 at 5:53 PM, Leopard20 said:

Creating one EH and making it not work when not needed using "if (conditionNotMet) exitWith {};"

That's more expensive. For one it has to actually execute stuff. Whereas your other method doesn't execute stuff.
Second the Eventhandler is recompiled before every execution. Which again is a reason to rather not have it present at all. Rather than adding a condition to it.

 

On 1.8.2018 at 11:18 PM, Leopard20 said:

If you don't mind me asking, how is it more suitable?

Draw3D EH is supposed to be used for draw commands.
It's executed after simulation cycle and just before rendering.

 

EachFrame is before simulation cycle. Meaning if a vehicle moves in Simulation, it will be later rendered at it's new position. But your icon will still be rendered at it's old position.

  • Like 2

Share this post


Link to post
Share on other sites

@Dedmen @gc8

Thank you guys for your invaluable help.

 

I think I'll delete the event handlers and also use Draw3D instead of onEachFrame.

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

×