a GetInstance() for [Static]EventHandlers

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: a GetInstance() for [Static]EventHandlers

Re: a GetInstance() for [Static]EventHandlers

by Graf Zahl » Tue Jul 26, 2022 4:02 pm

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.

Re: a GetInstance() for [Static]EventHandlers

by Sir Robin » Tue Jul 26, 2022 3:36 pm

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()

Re: a GetInstance() for [Static]EventHandlers

by Player701 » Sun Jul 24, 2022 11:42 pm

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.

Re: a GetInstance() for [Static]EventHandlers

by Graf Zahl » Thu Jul 21, 2022 11:18 am

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?

Re: a GetInstance() for [Static]EventHandlers

by Sir Robin » Thu Jul 21, 2022 10:58 am

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

Re: a GetInstance() for [Static]EventHandlers

by Graf Zahl » Thu Jul 21, 2022 12:00 am

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.

a GetInstance() for [Static]EventHandlers

by Sir Robin » Wed Jul 20, 2022 9:25 pm

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?

Top