How To: ZSCRIPT MENUS?

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: 17282
Joined: Mon Oct 27, 2003 12:07 am
Twitch ID: nashmuhandes
Github ID: nashmuhandes
Location: Kuala Lumpur, Malaysia

Re: How To: ZSCRIPT MENUS?

Post by Nash »

Cool tip!

Code: Select all


    override void Ticker
(void)
    {
        menuactive = Menu.OnNoPause;
    }
 


Put that in your Menu's ticker and the menu won't pause the whole game when it's open. Perfect for your RPG menus and what-not.
User avatar
Nash
 
 
Posts: 17282
Joined: Mon Oct 27, 2003 12:07 am
Twitch ID: nashmuhandes
Github ID: nashmuhandes
Location: Kuala Lumpur, Malaysia

Re: How To: ZSCRIPT MENUS?

Post by Nash »

Code: Select all

class StatsMenu MyOptionMenu
{
    
KeyBindings mBindings;

    
override bool MenuEvent (int mkeybool fromcontroller)
    {
        
int key mBindings.GetKeysForCommand("openmenu StatsMenu");

        if (
mkey == key)
        {
            
Close();
            return 
true;
        }

        return 
Super.MenuEvent(mkeyfromcontroller);
    }
}
 


QUESTION: How do I retrieve a specifically bound key? I want to make it so that if the user presses the same button that was bound to the command that opened this menu, it will just close the menu.

My code snippet above crashes the game as soon as there's some user input.
User avatar
Major Cooke
Posts: 8055
Joined: Sun Jan 28, 2007 3:55 pm

Re: How To: ZSCRIPT MENUS?

Post by Major Cooke »

Try using Destroy() instead of Close().

I have a couple alternatives in mind in case that doesn't work, but try that first.
User avatar
Nash
 
 
Posts: 17282
Joined: Mon Oct 27, 2003 12:07 am
Twitch ID: nashmuhandes
Github ID: nashmuhandes
Location: Kuala Lumpur, Malaysia

Re: How To: ZSCRIPT MENUS?

Post by Nash »

Nope, still crashes the game. It doesn't seem to like int key = mBindings.GetKeysForCommand("openmenu StatsMenu");
hence my question, how/if it's possible to even retrieve bounded keys.
User avatar
Major Cooke
Posts: 8055
Joined: Sun Jan 28, 2007 3:55 pm

Re: How To: ZSCRIPT MENUS?

Post by Major Cooke »

Do a check and see if mBindings is null -- call Console.Printf if any step of the way it is null.

Code: Select all

if (mBindings == null)    Console.Printf("Ruh roh!");


Also, GetKeysForCommand returns two ints because two keybinds can be assigned at any time.

Which means you might need to do this:

Code: Select all

int key1 = 0, key2 = 0;
[key1, key2] = mBindings.GetKeysForCommand(...);


Or something like that, I don't remember the exact schematics for it.
User avatar
Major Cooke
Posts: 8055
Joined: Sun Jan 28, 2007 3:55 pm

Re: How To: ZSCRIPT MENUS?

Post by Major Cooke »

Okay, here's a menu demonstrating how to make aesthetic(ally pleasing?) effects occur. Right now this only affects the main menu, but I plan on expanding it to also chain-affect every other menu if possible.

Try opening the different list items. You'll see that New/Load/Save game move the menu off to the right, Options moves it down and Quit moves it up. You can also cancel the in-transit effect with the escape key, and the menu will move right back to the starting position.

Small bonus, the list's text items (I replaced the patches with actual text, bear that in mind) now flash between two colors, set by the Font keyword in Menudef. Reminiscent of the Doomsday engine which I happened to really like.
You do not have the required permissions to view the files attached to this post.
User avatar
Nash
 
 
Posts: 17282
Joined: Mon Oct 27, 2003 12:07 am
Twitch ID: nashmuhandes
Github ID: nashmuhandes
Location: Kuala Lumpur, Malaysia

Re: How To: ZSCRIPT MENUS?

Post by Nash »

I almost solved my previous problem... the responder is now receiving the keyboard input, but I can't seem to make the input event match the keybind...

Code: Select all

class StatsMenu MyOptionMenu
{
    
KeyBindings mBindings;

    
override bool Responder (InputEventData ev)
    {
        if (
ev.type == InputEventData.GUI_Event)
        {
            if (
ev.subtype == InputEventData.GUI_KeyDown)
            {
                
int ch ev.data1;

                
int key1key2;
                
mBindings bindings;

                [
key1key2] = mBindings.GetKeysForCommand("openmenu StatsMenu");

                
Console.Printf("%d - %d"chkey1); // they're not the same thing!!!

                
if (ch == key1 || ch == key2)
                {
                    
Close();
                    return 
true;
                }
            }
        }
        return 
Super.Responder(ev);
    }
}
 
User avatar
Major Cooke
Posts: 8055
Joined: Sun Jan 28, 2007 3:55 pm

Re: How To: ZSCRIPT MENUS?

Post by Major Cooke »

Can't you just do "closemenu; openmenu StatsMenu", where it only has time to do one but not the other without the wait command?

Also, I wouldn't rely on input data if I were you. That whole system is being changed as we speak, and chances are will break your menu here wide open.
User avatar
Nash
 
 
Posts: 17282
Joined: Mon Oct 27, 2003 12:07 am
Twitch ID: nashmuhandes
Github ID: nashmuhandes
Location: Kuala Lumpur, Malaysia

Re: How To: ZSCRIPT MENUS?

Post by Nash »

That input event code is copy/pasted directly from whatever Graf Zahl wrote from the internal MenuDef. I assume it's safe to use.
User avatar
Major Cooke
Posts: 8055
Joined: Sun Jan 28, 2007 3:55 pm

Re: How To: ZSCRIPT MENUS?

Post by Major Cooke »

It doesn't mean ZZYZX won't change something about it, like the name going from InputEventData to UiEvent. Guess we'll find out when that comes around. :P

My previous question still stands, though.
Last edited by Major Cooke on Sun Feb 19, 2017 1:02 pm, edited 1 time in total.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 47970
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: How To: ZSCRIPT MENUS?

Post by Graf Zahl »

The key binder uses raw key codes (what the play code needs), the GUI codes are different. The menu normally uses GUI codes in its event processing. What you are trying to do there cannot possibly work,.
User avatar
Nash
 
 
Posts: 17282
Joined: Mon Oct 27, 2003 12:07 am
Twitch ID: nashmuhandes
Github ID: nashmuhandes
Location: Kuala Lumpur, Malaysia

Re: How To: ZSCRIPT MENUS?

Post by Nash »

Ok, thanks for clarifying. No problem, it's not important. :)
User avatar
Nash
 
 
Posts: 17282
Joined: Mon Oct 27, 2003 12:07 am
Twitch ID: nashmuhandes
Github ID: nashmuhandes
Location: Kuala Lumpur, Malaysia

Re: How To: ZSCRIPT MENUS?

Post by Nash »

How do I add a new parameter to a custom widget I inherit from? Let's say I wanted to make a custom version of OptionMenuItem that takes an extra parameter to set the "help/tooltip text" for it.
User avatar
Major Cooke
Posts: 8055
Joined: Sun Jan 28, 2007 3:55 pm

Re: How To: ZSCRIPT MENUS?

Post by Major Cooke »

It's all based on the Init function.

Code: Select all

OptionMenuItemCreditDisplay Init (Name command, CVar graycheck = null)
{
   Super.Init("Credits", command, graycheck);
   mCredit = CVar.FindCVar("D4DCash");
   return self;
}


Expand the parameters to how you see fit. In my case I actually removed some.
User avatar
Nash
 
 
Posts: 17282
Joined: Mon Oct 27, 2003 12:07 am
Twitch ID: nashmuhandes
Github ID: nashmuhandes
Location: Kuala Lumpur, Malaysia

Re: How To: ZSCRIPT MENUS?

Post by Nash »

Major Cooke, if you have time - can you post a minimal example on how to safely manipulate the player with the recent systems and restrictions.

Perhaps just a simple menu that changes the player's health when you click on a button?

Thanks!

Return to “Scripting”