[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.

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

Postby Nash » Tue Feb 14, 2017 10:12 pm

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
Nash
Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

Re: Event Handler barebones examples

Postby ZZYZX » Wed Feb 15, 2017 9:04 am

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).
User avatar
ZZYZX
le chat du rabbin
 
 
 
Joined: 14 Oct 2012
Location: Ukraine
Discord: ZZYZX#1394
Github ID: jewalky

Re: Event Handler barebones examples

Postby Nash » Sun Feb 19, 2017 10:42 am

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
Nash
Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

Re: Event Handler barebones examples

Postby Major Cooke » Sun Feb 19, 2017 11:07 am

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
Major Cooke
QZDoom Maintenance Team
 
Joined: 28 Jan 2007

Re: Event Handler barebones examples

Postby ZZYZX » Sun Feb 19, 2017 2:12 pm

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
ZZYZX
le chat du rabbin
 
 
 
Joined: 14 Oct 2012
Location: Ukraine
Discord: ZZYZX#1394
Github ID: jewalky

Re: Event Handler barebones examples

Postby Nash » Sun Feb 19, 2017 8:02 pm

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
Nash
Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

Re: Event Handler barebones examples

Postby ZZYZX » Sun Feb 19, 2017 8:31 pm

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
ZZYZX
le chat du rabbin
 
 
 
Joined: 14 Oct 2012
Location: Ukraine
Discord: ZZYZX#1394
Github ID: jewalky

Re: Event Handler barebones examples

Postby Nash » Mon Feb 20, 2017 12:27 am

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
Nash
Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

Re: Event Handler barebones examples

Postby ZZYZX » Mon Feb 20, 2017 1:24 am

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
ZZYZX
le chat du rabbin
 
 
 
Joined: 14 Oct 2012
Location: Ukraine
Discord: ZZYZX#1394
Github ID: jewalky

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

Postby Nash » Mon Feb 20, 2017 2:04 am

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
Nash
Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

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

Postby Major Cooke » Mon Feb 20, 2017 6:59 am

There won't be official global variable support outside of what we have now, last I checked.
User avatar
Major Cooke
QZDoom Maintenance Team
 
Joined: 28 Jan 2007

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

Postby Nash » Mon Feb 20, 2017 7:49 am

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?
User avatar
Nash
Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

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

Postby ZZYZX » Mon Feb 20, 2017 8:12 am

Bug.
User avatar
ZZYZX
le chat du rabbin
 
 
 
Joined: 14 Oct 2012
Location: Ukraine
Discord: ZZYZX#1394
Github ID: jewalky

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

Postby Nash » Thu Feb 23, 2017 12:34 am

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?
User avatar
Nash
Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

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

Postby ZZYZX » Thu Feb 23, 2017 12:53 am

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.
User avatar
ZZYZX
le chat du rabbin
 
 
 
Joined: 14 Oct 2012
Location: Ukraine
Discord: ZZYZX#1394
Github ID: jewalky

Next

Return to Scripting

Who is online

Users browsing this forum: zedernt and 2 guests