[ZScript] How Do EventHandlers ¯\(°_o)/¯ ?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

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!)
User avatar
Nash
 
 
Posts: 17450
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

[ZScript] How Do EventHandlers ¯\(°_o)/¯ ?

Post by Nash »

Requesting ZZYZX to post some really basic examples on how to use the new system... if no one knows how to use it, the feature won't be used... and the community will turn into another QuakeC "source code hoarder" mentality where only the Elite Few(tm) know how to do things. :mrgreen:
Last edited by Nash on Mon Feb 20, 2017 12:28 am, edited 1 time in total.
User avatar
ZZYZX
 
 
Posts: 1384
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

Re: Event Handler barebones examples

Post by ZZYZX »

Sadly the system is half-functional as of now and you can't do UI on it due to this thread, but the most basic example is here:

MAPINFO:

Code: Select all

GameInfo
{
  EventHandlers = "MyHandler"
}
ZScript:

Code: Select all

class MyHandler : StaticEventHandler
{
  override void WorldLoaded(WorldEvent e)
  {
    // this event will be fired every time a map is loaded. details in WorldEvent object
  }

  override void WorldUnloaded(WorldEvent e)
  {
    // this event will be fired every time a map is about to unload. details in WorldEvent object
  }
}
WorldEvent (and other event types) are well-commented here:
https://github.com/coelckers/gzdoom/blo ... events.txt

Note that you can do it two ways.

If you inherit MyHandler from StaticEventHandler (like I did), your handler will be created DURING ENGINE STARTUP and persist between map changes with all it's variables.
StaticEventHandler won't get saved to the savegames ever, and all references to it will be nulled when loading a savegame (like a transient field).

If you inherit MyHandler from EventHandler, your handler will be created during the start of the map and deleted after you exit it, but it will also save to savegames and restore it's state after loading the game.
EventHandlers are playsim-safe and savegame-transparent.

WorldLoaded and WorldUnloaded will work differently for Static and non-Static event handlers: Static will receive every load/unload, including when you are unloading to load a save game or just loaded from a save game (use e.IsSaveGame to check for these), while non-Static will receive only first load and level change (the same as OPEN and UNLOADING scripts in ACS).
User avatar
Nash
 
 
Posts: 17450
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

Re: Event Handler barebones examples

Post by Nash »

I am so lost. What's registering and unregistering and why do I have to or not have to do those? How do you use InputProcess because I made one and the player can't move (but I can print the current keyboard keys being pressed). How do I make an event that fires something off when a key bound to "sprint" is pressed? Also after a while InputProcess just randomly stops working (doesn't receive my keyboard presses anymore) for no apparent reason.

Confused and clueless...
User avatar
Major Cooke
Posts: 8182
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: Event Handler barebones examples

Post by Major Cooke »

As far as I can tell, it's easiest to assume nothing posted here will stick for the time being until confirmed/denied otherwise, due to the massive rework ZZYZX is currently putting into it.
User avatar
ZZYZX
 
 
Posts: 1384
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

Re: Event Handler barebones examples

Post by ZZYZX »

Nash wrote:Also after a while InputProcess just randomly stops working (doesn't receive my keyboard presses anymore) for no apparent reason.
Probably got garbage collected. Graf still hasnt looked into it.
As for InputEvent, return false if you want the event to pass through (i.e. allow the player to move), otherwise return true to eat the event.

Anyway, that's exactly why Graf said that you shouldn't base mods on devbuilds anymore. Especially on new features in the devbuilds.
User avatar
Nash
 
 
Posts: 17450
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

Re: Event Handler barebones examples

Post by Nash »

Re: GC, well this thing can't even be tested properly if no one knows how to use any of these... :V

I propose example scripts for every event and how to use them, whenever you have time. Like that ZSDemo thing you put together in that other thread (that one only covers world events, which are obvious to use)

Also what do every of the functions mean... like what even is registering and unregistering.
User avatar
ZZYZX
 
 
Posts: 1384
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

Re: Event Handler barebones examples

Post by ZZYZX »

Registering is for creating new event handlers during the game, not defined in MAPINFO.
i.e. you can make an event handler by EventHandler.Create(type), then use the received object with EventHandler.Register() and it will receive events as well.
User avatar
Nash
 
 
Posts: 17450
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

Re: Event Handler barebones examples

Post by Nash »

Okay. Next question:

If StaticEventHandler is transient and persistent, and EventHandler is save-game transparent... what should I do if I want EventHandler to be global (like an ACS global variable)?

Am I supposed to copy the EventHandler's variables temporarily to a StaticEventHandler between map changes? Is that the only way, or is there A Better Way(tm)?
User avatar
ZZYZX
 
 
Posts: 1384
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

Re: Event Handler barebones examples

Post by ZZYZX »

Nash wrote:what should I do if I want EventHandler to be global (like an ACS global variable)?
You currently cannot. Not sure if copying would work, but it probably should for now (until the proper way is done).
Make sure you detect new game and don't copy the values back in that case. (use level.time==level.totaltime or whatever that variable is called).
User avatar
Nash
 
 
Posts: 17450
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

Re: [ZScript] How Do EventHandlers ¯\(°_o)/¯ ?

Post by Nash »

Sounds like something Graf Zahl will yell at me for. :mrgreen: Maybe I'll wait for an official way to have clean, persistent variables that can live between maps for that particular game session.

EDIT: Yeaaaa definitely giving up on even trying anymore, it's too freaking complicated and prone to mistakes... if I wanted to play micromanagement I'd rather play StarCraft 2 instead. Will wait for official global variable support.
User avatar
Major Cooke
Posts: 8182
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: [ZScript] How Do EventHandlers ¯\(°_o)/¯ ?

Post by Major Cooke »

There won't be official global variable support outside of what we have now, last I checked.
User avatar
Nash
 
 
Posts: 17450
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

Re: [ZScript] How Do EventHandlers ¯\(°_o)/¯ ?

Post by Nash »

Code: Select all

bind Shift netevent SprintHandler

Code: Select all

class SprintHandler : EventHandler
{
    override void ConsoleProcess(ConsoleEvent e)
    {
        Console.Printf("%s: activated by player %d [%s]", e.Name, e.Player, players[e.Player].mo.GetClassName());
        //players[e.Player].mo.A_Die();
    }
}
 
It only works in singleplayer. In multiplayer, the event doesn't fire off. Tried both event and netevent CCMDs, both do not work in multiplayer. Am I doing it wrong, or bug?
User avatar
ZZYZX
 
 
Posts: 1384
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

Re: [ZScript] How Do EventHandlers ¯\(°_o)/¯ ?

Post by ZZYZX »

Bug.
User avatar
Nash
 
 
Posts: 17450
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

Re: [ZScript] How Do EventHandlers ¯\(°_o)/¯ ?

Post by Nash »

Code: Select all

class MenuKeyHandler : StaticEventHandler
{
    override void ConsoleProcess(ConsoleEvent e)
    {
        Console.Printf("%s: activated by player %d [%s]", e.Name, e.Player, players[e.Player].mo.GetClassName());
    }
}

class SprintHandler : StaticEventHandler
{
    override void ConsoleProcess(ConsoleEvent e)
    {
        Console.Printf("%s: activated by player %d [%s]", e.Name, e.Player, players[e.Player].mo.GetClassName());
        let p = players[e.Player].mo;
        Z_Player(p).bSprinting = true;
    }
}
 
I have Tab bound to "netevent MenuKeyHandler" and Shift bound to "netevent SprintKeyHandler".

When I press either one of those keys, both events get fired off. Meaning, I can sprint even if I hit the menu key, and vice versa when press Shift to spring, the MenuKeyHandler gets fired.

Bug?
User avatar
ZZYZX
 
 
Posts: 1384
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

Re: [ZScript] How Do EventHandlers ¯\(°_o)/¯ ?

Post by ZZYZX »

Does it work correctly with "event"? Perhaps it's something about networking that's broken.
Otherwise if you mean that both handlers receive it, that's how it should be. You are supposed to look at e.Name and only handle events that you need to handle.

Return to “Scripting”