Page 1 of 4

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

PostPosted: Tue Feb 14, 2017 10:12 pm
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:

Re: Event Handler barebones examples

PostPosted: Wed Feb 15, 2017 9:04 am
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 allExpand view
GameInfo
{
  EventHandlers = "MyHandler"
}


ZScript:
Code: Select allExpand view
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).

Re: Event Handler barebones examples

PostPosted: Sun Feb 19, 2017 10:42 am
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...

Re: Event Handler barebones examples

PostPosted: Sun Feb 19, 2017 11:07 am
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.

Re: Event Handler barebones examples

PostPosted: Sun Feb 19, 2017 2:12 pm
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.

Re: Event Handler barebones examples

PostPosted: Sun Feb 19, 2017 8:02 pm
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.

Re: Event Handler barebones examples

PostPosted: Sun Feb 19, 2017 8:31 pm
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.

Re: Event Handler barebones examples

PostPosted: Mon Feb 20, 2017 12:27 am
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)?

Re: Event Handler barebones examples

PostPosted: Mon Feb 20, 2017 1:24 am
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).

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

PostPosted: Mon Feb 20, 2017 2:04 am
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.

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

PostPosted: Mon Feb 20, 2017 6:59 am
by Major Cooke
There won't be official global variable support outside of what we have now, last I checked.

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

PostPosted: Mon Feb 20, 2017 7:49 am
by Nash
Code: Select allExpand view

bind Shift netevent SprintHandler


Code: Select allExpand view
class SprintHandler EventHandler
{
    
override void ConsoleProcess(ConsoleEvent e)
    {
        
Console.Printf("%s: activated by player %d [%s]"e.Namee.Playerplayers[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?

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

PostPosted: Mon Feb 20, 2017 8:12 am
by ZZYZX
Bug.

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

PostPosted: Thu Feb 23, 2017 12:34 am
by Nash
Code: Select allExpand view

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?

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

PostPosted: Thu Feb 23, 2017 12:53 am
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.