[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: 17454
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

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

Post by Nash »

UGH. I cannot make a minimal WAD example because the bug doesn't happen.

I'm sending you a link to my entire project. Bind something to "netevent SprintKeyHandler". You'll get the bug very quickly with my package.
User avatar
Nash
 
 
Posts: 17454
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

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

Post by Nash »

And minimal WAD example if you still insist. Bug will not happen with this one (the SprintKeyHandler can be fired as many times as you want, it just works)
You do not have the required permissions to view the files attached to this post.
User avatar
ZZYZX
 
 
Posts: 1384
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

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

Post by ZZYZX »

Doesn't reproduce with your full PK3 using the SprintKeyHandler. The message displays reliably 1 minute into the game, no matter how often I press the bound key.
I'm using gzdoom-x64-g2.4pre-620-g10c6b7a.
User avatar
Nash
 
 
Posts: 17454
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

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

Post by Nash »

I'm using the same executable as you. Here's what I found out:

If I QUICKLY start a new game as soon as the program launches, and IMMEDIATELY hit the Sprint key; the sprinting will work and I can trigger it repeatedly.

If I take my time at the title screen, then press new game, then wait a few more seconds and try to Sprint... the sprint key won't work.

I was able to reproduce this repeatedly.
User avatar
ZZYZX
 
 
Posts: 1384
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

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

Post by ZZYZX »

SprintKeyHandler is static, and if you put it in MAPINFO, it's supposed to be loaded at the start of the engine and persist.
Yet, for whatever reason it gets removed at exactly tic 40 in titlemap. Tested with WorldTick callback that outputs level.time.
No idea what causes this really, will build latest GZDoom later and test, if Graf can't help sooner.
I really think this might be related to the recent GC edit which made static handlers non-fixed, but as well might not be that. Looks too deterministic to be a problem with GC (it's always 40 tics...).

Can you send that archive to Graf as well pls :)
User avatar
ZZYZX
 
 
Posts: 1384
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

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

Post by ZZYZX »

Made an example on proper routing of input events through the network for interaction with playsim, thought I'd share here as well: http://pastebin.com/2gvzhcUu
NetworkProcess is ok to alter the world state from.
User avatar
Major Cooke
Posts: 8193
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 »

If you want a more specific version...

Code: Select all

class TwilightPlayerHandler : EventHandler
{
    override bool InputProcess(InputEvent ev)
    {
        if (ev.Type == InputEvent.Type_KeyDown && ev.KeyChar >= 0x30 && ev.KeyChar <= 0x39) // 0x30, 0x39 are '0' and '9'
        {
            // Make sure the player exists.
            let plr = players[consoleplayer].mo;
            
            // Why run away? It's JUST a giant nuclear purple warhorse.
            if (!plr || !plr.CountInv("PowerTwilightSparkleMorph"))
                return false;
            
            // ev.KeyChar smells like magic.
            EventHandler.SendNetworkEvent("Pwnie_NumericKeyPressed", ev.KeyChar-0x30, 0, 0);
        }
        return false;
    }

    override void NetworkProcess(ConsoleEvent ev)
    {
        if (ev.Name == "Pwnie_NumericKeyPressed")
        {
            let plr = players[ev.Player].mo;
            
            // player ev.Player pressed key ev.Args[0]
            // where ev.Args[0] is the key 0 through 9, apparently.
            if (plr && plr.CountInv("PowerTwilightSparkleMorph"))
            {
                plr.A_SetInventory("WeaponModeToken",ev.Args[0]);
            }
        }
    }
}
Thus I am able to have different attack modes with a morph on. Thank you so much, ZZYZX. :mrgreen:

Even if it, quite literally, runs on rocket science so powerful that it's magical. SOMEHOW.

'cuz I don't get how ev.KeyChar - 0x30 works and shit, nor what each of the three args contain.
User avatar
AFADoomer
Posts: 1337
Joined: Tue Jul 15, 2003 4:18 pm

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

Post by AFADoomer »

Major Cooke wrote:If you want a more specific version...

[snip]

Thus I am able to have different attack modes with a morph on. Thank you so much, ZZYZX. :mrgreen:

Even if it, quite literally, runs on rocket science so powerful that it's magical. SOMEHOW.

'cuz I don't get how ev.KeyChar - 0x30 works and shit, nor what each of the three args contain.
ev.KeyChar is literally just returning the ASCII code for the pressed key...

0x30 in ASCII encoding is '0'. You know that your inputs are between 0x30 and 0x39 (0-9) becasue of the first "if" statement there, so a quick way to convert your ASCII code to the equivalent integer value is to subtract 0x30 from the ASCII value.

So if the player pushes the '7' key, the game is sent ASCII code 0x37, then you subtract 0x30 from 0x37 and get 7.
User avatar
Nash
 
 
Posts: 17454
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

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

Post by Nash »

Posting because this was apparently never documented or even mentioned anywhere:

MAPINFO "AddEventHandlers" can be used to add to existing event handlers without overwriting anything. Useful for mod compatibility.
User avatar
Major Cooke
Posts: 8193
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 »

Yeah, ZZYZX only recently told me about it himself.
User avatar
Ed the Bat
Posts: 3060
Joined: Thu May 03, 2012 1:18 pm
Graphics Processor: nVidia with Vulkan support
Location: Maryland, US

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

Post by Ed the Bat »

I had no issue with multiple instances of EventHandlers among different MAPINFO lumps. But I just tried changing them to AddEventHandlers, and now the event handlers never activate. Is there something special I need to do?
User avatar
ZZYZX
 
 
Posts: 1384
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

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

Post by ZZYZX »

Probably a bug or something.
User avatar
Nash
 
 
Posts: 17454
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 LADgameInputHandler : EventHandler
{
    override void ConsoleProcess(ConsoleEvent e)
    {
        if (e.Name == 'OpenCharacterMenu')
        {
            EventHandler.SendNetworkEvent("OpenCharacterMenu", 0, 0, 0);
        }
    }

    override void NetworkProcess(ConsoleEvent e)
    {
        if (e.Name == 'OpenCharacterMenu')
        {
            let p = players[e.Player].mo;

            if (p)
            {
                Menu.SetMenu("ItemDebugMenu");
            }
        }
    }
}
 
Script error, ":zscript/base/ladgameinput/ladgameinput.zc" line 76:
Can't call ui function SetMenu from play context (not readable)

Help?
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49130
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

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

Post by Graf Zahl »

You are trying to open the menu from inside the play simulation. Any reason you are seinding that through the network?
User avatar
Major Cooke
Posts: 8193
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 »

I think he was trying to make a system where he could open and close the menu with the same keybind? (Correct me if I'm wrong.)

Return to “Scripting”