a GetInstance() for [Static]EventHandlers

Moderator: GZDoom Developers

Post Reply
User avatar
Sir Robin
Posts: 537
Joined: Wed Dec 22, 2021 7:02 pm
Graphics Processor: Intel (Modern GZDoom)
Location: Medellin, Colombia

a GetInstance() for [Static]EventHandlers

Post by Sir Robin »

Just about every time I write an event handler and need to check variables or call non-static functions I have to write something like this:

Code: Select all

    static MyEventHandler GetInstance()
    {
        let h = MyEventHandler(EventHandler.Find('MyEventHandler'));

        if (h == null)
        {
            ThrowAbortException("Could not find the instance of MyEventHandler.");
        }

        return h;
    }
 
Can we get something like that built-in?
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49067
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: a GetInstance() for [Static]EventHandlers

Post by Graf Zahl »

What do you want here? The function you call is essentially that already and baking in the error handling is a very, very bad idea.
User avatar
Sir Robin
Posts: 537
Joined: Wed Dec 22, 2021 7:02 pm
Graphics Processor: Intel (Modern GZDoom)
Location: Medellin, Colombia

Re: a GetInstance() for [Static]EventHandlers

Post by Sir Robin »

The error handling isn't needed, I just copy/pasted my existing code as an example.
I thought that if this were converted to a native function it would give:
Convenience: Don't have to re-write this function in zscript for every event handler coded
Performance: I'm assuming that if written natively it wouldn't need to search on a string and cast the result back to a class
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49067
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: a GetInstance() for [Static]EventHandlers

Post by Graf Zahl »

You are mistaken here. There's not much to be saved here. Unless the info was stored elsewhere it still needs to traverse the list and dynamically check the type.
There's also another gotcha: What magic is supposed to generate these static methods for your classes?
User avatar
Player701
 
 
Posts: 1640
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support
Contact:

Re: a GetInstance() for [Static]EventHandlers

Post by Player701 »

Why not have just one method instead? E.g. in addition to EventHandler.Find, add an EventHandler.MustFind or MustGet or whatever. Though it might not actually be necessary to put it into the engine itself:

Code: Select all

class EventHandlerHelper
{
    static StaticEventHandler MustFind(class<StaticEventHandler> type)
    {
        let h = StaticEventHandler.Find(type);

        if (h == null)
        {
            ThrowAbortException("Could not find the instance of %s.", type.GetClassName());
        }

        return h;
    }
}
I use similar code in my own mod, it's really nothing more than a safety net for easier debugging. But considering that there is no debugger for ZScript, anything that improves error handling makes it less painful to work with. Suppose I accidentally forgot to register my handler, then I'd receive a meaningful error message instead of a generic "tried to read from address zero" one.
User avatar
Sir Robin
Posts: 537
Joined: Wed Dec 22, 2021 7:02 pm
Graphics Processor: Intel (Modern GZDoom)
Location: Medellin, Colombia

Re: a GetInstance() for [Static]EventHandlers

Post by Sir Robin »

The error handling wasn't important, it was just an example. What I was asking for was just an engine-provided implementation of that GetInstance() function, witout the error handling. I figured if the engine was handling that instead of ZScript it would be faster because internally it could use a dictionary to find the instance instead of having to convert it to a string and search that way. If I keep a global variable or even a function in an eventhandler and have a bunch of actors that keep calling that Find in their tick every time they need it I just thought that would add up.

And it would be easy enough to wrap it up in an error handler. I think I'd call mine GetInstanceOrDieTrying()
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49067
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: a GetInstance() for [Static]EventHandlers

Post by Graf Zahl »

There's one big problem with that: There is no object that could store the instance pointer. And the GetInstance method cannot be instantiated automatically for each derived class.
Post Reply

Return to “Closed Feature Suggestions [GZDoom]”