Add IsKindOf support to EventHandler Find function

Moderator: GZDoom Developers

TheShadsy
Posts: 86
Joined: Sun Apr 15, 2012 2:58 pm

Add IsKindOf support to EventHandler Find function

Post by TheShadsy »

The Find function in event handlers can currently only find a handler by their specific class name. For event handlers that derive from another event handler, you cannot find them based on their parent class. (eg., if Handler_1 inherits from Handler_Base, you cannot find Handler_1 by searching for "Handler_Base").

Some search functions, like FindInventory, do support this type of search by using the function IsKindOf (see p_mobj.cpp, line 1105).

Similar to FindInventory, it would be great if EventHandler had a subclass argument (false by default) that could locate handlers the same way.

This is probably not accurate code, but it could look something like this. Modified from events.cpp, lines 1683-1706

Code: Select all

DEFINE_ACTION_FUNCTION(DEventHandler, Find)
{
	PARAM_PROLOGUE;
	PARAM_CLASS(t, DStaticEventHandler);
	PARAM_BOOL(subclass);
	for (DStaticEventHandler* handler = currentVMLevel->localEventManager->FirstEventHandler; handler; handler = handler->next)
		if (handler->GetClass() == t) // check precise class
			ACTION_RETURN_OBJECT(handler);
		else if (subclass && handler_>IsKindOf(t));
			ACTION_RETURN_OBJECT(handler);
	ACTION_RETURN_OBJECT(nullptr);
}

// we might later want to change this
DEFINE_ACTION_FUNCTION(DStaticEventHandler, Find)
{
	PARAM_PROLOGUE;
	PARAM_CLASS(t, DStaticEventHandler);
	PARAM_BOOL(subclass);
	for (DStaticEventHandler* handler = staticEventManager.FirstEventHandler; handler; handler = handler->next)
		if (handler->GetClass() == t) // check precise class
			ACTION_RETURN_OBJECT(handler);
		else if (subclass && handler_>IsKindOf(t));
			ACTION_RETURN_OBJECT(handler);
	ACTION_RETURN_OBJECT(nullptr);
}
For a real-life example of why this is useful: Reelism 2 uses different event handlers on each map to determine their game mode. They are all derived from a common event handler template (Splot_GameType_Global). If we want to find any active gametype handler, the Find function can't do that, so we have to do Find for each specific game mode event handler class until we find one. Adding IsKindOf to Find would make this much simpler and more efficient.
User avatar
Rachael
Posts: 13971
Joined: Tue Jan 13, 2004 1:31 pm
Preferred Pronouns: She/Her

Re: Add IsKindOf support to EventHandler Find function

Post by Rachael »

TheShadsy
Posts: 86
Joined: Sun Apr 15, 2012 2:58 pm

Re: Add IsKindOf support to EventHandler Find function

Post by TheShadsy »

Rachael wrote: Sun Aug 24, 2025 2:28 am added
Thank you! FYI there's still a little bit of implementation that still needs to be done. In events.zs, line 224 (Find for non-static event handlers) also needs to be updated to include "bool subclass = false".

EDIT: In a partially implemented state, this also seems to have broken the Find operation altogether for non-static event handlers in the current DRD Team build (g4.15pre-516-gabdacad8d). Eek!

EDIT 2: For the sake of documenting it: this has been addressed!

Return to “Closed Feature Suggestions [GZDoom]”