How to detect crouching?

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!)
Zantonlan
Posts: 2
Joined: Tue May 14, 2024 4:55 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support

How to detect crouching?

Post by Zantonlan »

I can't figure out how to detect if the player is crouching. I'm using ZScript on an enemy to detect if the player is crouching for a stealth system. I've tried checking the "Crouching" variable under the player, getting the button press, and I can't figure out what the function "GetCrouchFactor" wants for the pointer.

My current code is the following:

Code: Select all

virtual bool IsCrouching()
    {
        int buttons = GetPlayerInput(0, INPUT_BUTTONS);

        if (buttons & BT_CROUCH)
        {
            return true;
        }
        //if (PlayerInGame[0] && players[0].mo)
        //{
            if (players[0].Crouching == -1)
            {
                return true;
            }
        //}
        return false;
    }

    States
    {
        Spawn:
            GEOD AKA 5;
            GEOD A 0 A_JumpIf(IsCrouching(), 2);
            GEOD A 0 A_Look;
            loop;
        See:
            GEOD ABAC 4;
            GEOD C 0 A_JumpIf(IsCrouching(), "Spawn");
            GEOD C 0 A_Chase;
    }
Any help would be appreciated, thanks!
peewee_RotA
Posts: 382
Joined: Fri Feb 07, 2014 6:45 am

Re: How to detect crouching?

Post by peewee_RotA »

Since GetCrouchFactor is a decorate function the pointers are weird, but basically you have to use special flags to tell it what to check.

If you are calling this from inside one of the player functions, what you're looking for is this:

Code: Select all

GetCrouchFactor(AAPTR_DEFAULT)
The AAPTR_DEFAULT tells it to point to the calling actor.

Here's more info on this:
https://zdoom.org/wiki/Actor_pointer#DECORATE_.26_ACS


EDIT:
GetCrouchFactor returns 0.5 for a player who is crouching, and 1.0 for a player that is not crouching or any actor that is not a player (since only players can crouch)

So inside your IsCrouching function you'll check if that's == 0.5
Jarewill
 
 
Posts: 1783
Joined: Sun Jul 21, 2019 8:54 am

Re: How to detect crouching?

Post by Jarewill »

With ZScript you can also directly get the crouchoffset or crouchfactor PlayerInfo variables.
For crouchoffset you can compare if it's not zero: player.crouchoffset != 0
And for crouchfactor you can compare it to 0.5 like peewee said: player.crouchfactor == 0.5
Zantonlan
Posts: 2
Joined: Tue May 14, 2024 4:55 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support

Re: How to detect crouching?

Post by Zantonlan »

It works now, thanks a ton!

Return to “Scripting”