EventHandler vs StaticEventHander. What could go wrong?

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)

Post a reply

Smilies
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :geek: :ugeek: :!: :?: :idea: :arrow: :| :mrgreen: :3: :wub: >:( :blergh:
View more smilies

BBCode is OFF
Smilies are ON

Topic review
   

Expand view Topic review: EventHandler vs StaticEventHander. What could go wrong?

Re: EventHandler vs StaticEventHander. What could go wrong?

by Accensus » Sun May 24, 2020 5:55 pm

It was broken because it relied on using OnRegister for something it's not supposed to do. Plus I had to avoid all the problems that came with normal event handlers being reinitialized/recreated/whatever on every map. So far no serious issues have been reported.

Re: EventHandler vs StaticEventHander. What could go wrong?

by SanyaWaffles » Sun May 24, 2020 5:31 pm

I feel if the old method worked and wasn't broken don't fix it.

Re: EventHandler vs StaticEventHander. What could go wrong?

by Accensus » Sun May 24, 2020 6:58 am

Well, converted the handler to StaticEventHandler. I feel like I'm committing serious crimes here because I call EventHandler.SendNetworkEvent in StaticEventHandler.RenderOverlay, and intercept the network event in the very same StaticEventHandler in order to change some play-scope data in it. shudders

So far it works with the exception of loading a save game that uses the old handler, which causes some major interference. Starting a new game works just fine.

Re: EventHandler vs StaticEventHander. What could go wrong?

by Accensus » Sun May 24, 2020 2:11 am

That makes sense. I guess I'll refactor the code to use StaticEventHandler. I'm mainly worried about WorldThingDamaged. And what about RenderOverlay? Will those still work properly in a static context? These are the questions that keep me up at night.

Re: EventHandler vs StaticEventHander. What could go wrong?

by SanyaWaffles » Sat May 23, 2020 10:26 pm

Thanks for additional clarification on that, I figured that was the case for StaticEventHandlers.

I feel something we need to do is collaborate a bit and update the Wiki. I'd do it, but I'm still learning ZScript and I don't want to make mistakes on what is right and what isn't.

Re: EventHandler vs StaticEventHander. What could go wrong?

by 3saster » Sat May 23, 2020 8:37 pm

Using a StaticEventhandler for initialization like that is totally fine. In fact, the wiki even suggests doing that to make certain types of global variables. That commit seems to be about using OnRegister for a non-static eventhandler, which can indeed cause some problems when loading a savegame. Using OnRegister in a StaticEventHandler should be safe, because it exists outside of the scope of save games. That makes it a double edged sword for things like this however; content inside StaticEventHandlers is NOT stored across different play sessions. Keep that if something is initialized in WorldLoaded, then when it is reloaded, WorldLoaded won't be called again, but everything from it already happened. In other words, if I do

Code: Select all

class TestHandler : EventHandler
{
	private int x;
	override void WorldLoaded (WorldEvent e)
	{
		console.printf("init");
		x = 5;
	}
	
	override void WorldTick()
	{
		console.printf("%d",x);
	}
}
5 will always be printed even if I load a savegame, because the value of x is stored in that savegame.

Basically, you can safely use StaticEventhandlers for initialization, but think about what you want to do with the handler. StaticEventhandlers should be used for things that exist outside the scope of a particular instance of the game (if that makes sense); EventHandlers are for things that do depend on a particular instance.

Re: EventHandler vs StaticEventHander. What could go wrong?

by SanyaWaffles » Sat May 23, 2020 8:01 pm

I honestly have never found any sort of issue using StaticEventHandlers for anything. It seems to work fine.

If initialization isn't meant to be used in OnRegister then where is it supposed to be done for a one-time thing? Especially since StaticEventHandlers are meant for overall games and not specific levels/maps.

I feel as what is being done isn't too intensive it shouldn't matter. However it seems there's no good consensus on what qualifies as good practice with EventHandlers.

If OnRegister is not intended for initialization... Then several projects have been doing it wrong, including my own... even though there's no obvious side effects observed. I don't get what the issue is.

Honestly it feels like it's one of those design things that's a chicken and the egg type thing. And it's frustrating to be limited when it doesn't seem to cause any issues.

Also not using StaticEventHandler? Then what's it there for? If it's something verboten then why is it able to be used? Why bother implementing a feature if it shouldn't be used?

EventHandler vs StaticEventHander. What could go wrong?

by Accensus » Fri May 22, 2020 11:01 am

Here's the situation: I have this override right here.

Recently, here, it was stated that OnRegister should only ever be used for setting the event handler order. Now, I could move the code to WorldLoaded in that case, but that would break the mod because WorldLoaded in EventHandler is only called when map starts and not when save games are loaded, which is the reason all that crap is jammed inside OnRegister right now.

I'd convert the event handler to StaticEventHandler, but here comes the million dollar question: what could go wrong? I remember that a while ago someone (I won't mention names but their post could be trusted) recommended to avoid using StaticEventHandler. No particular reason was specified, and at the time I didn't think to question that statement, but now I am facing the issue of using OnRegister for something it was never intended for, despite the wiki article saying initialization could be performed in it. Should StaticEventHandler be avoided? What are the complications? Will I commit third degree crimes if I use it with the same virtuals I am currently using in the file in the first link?

Top