how do i check the player health?(zscript)

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!)
Post Reply
User avatar
OddYokai
Posts: 15
Joined: Mon Jul 04, 2022 11:01 am
Preferred Pronouns: He/Him
Graphics Processor: ATI/AMD (Modern GZDoom)
Location: Hungary
Contact:

how do i check the player health?(zscript)

Post by OddYokai »

Hello! I'm very new to zscript. I found an item magnet on the forum that works great. as practice, I tried to rewrite it so that the player only attracts the item if his health is less than or equal to 90. But unfortunately it didn't work, I didn't really know what to do, I tried "if(player.health <= 90)" everywhere. I know zscript doesn't really work that way, but I tried, please help


The code:

Code: Select all

Override void Tick()
    { 
        Super.Tick();
        GlideToPlayer(); //Call the function
    }
    void GlideToPlayer()
    {
        For(let i = BlockThingsIterator.Create(self,128); i.Next();)
        { 
            Actor other = i.thing;
            If(other==self)
            {Continue;} 
            double distance = Distance3D(other);
            If(distance>128)
            {Continue;} 
            If(other is "PlayerPawn")
            { 
                A_Face(other); //Face the player
                VelFromAngle(10.0);
            }
        }
    }
} 
Jarewill
 
 
Posts: 1853
Joined: Sun Jul 21, 2019 8:54 am

Re: how do i check the player health?(zscript)

Post by Jarewill »

To check for the player's health you need to use a pointer to the player, in your case that pointer is other, so it would look like this:
If(other is "PlayerPawn" && other.health <= 90)

By the way, if you want the magnet to also work vertically, you can replace A_Face and VelFromAngle with this:
Vel3DFromAngle(10,AngleTo(other),PitchTo(other,0,other.height/2));
User avatar
OddYokai
Posts: 15
Joined: Mon Jul 04, 2022 11:01 am
Preferred Pronouns: He/Him
Graphics Processor: ATI/AMD (Modern GZDoom)
Location: Hungary
Contact:

Re: how do i check the player health?(zscript)

Post by OddYokai »

Thanks! it works great. now I clearly understand what a "pointer" is and how it works.
User avatar
OddYokai
Posts: 15
Joined: Mon Jul 04, 2022 11:01 am
Preferred Pronouns: He/Him
Graphics Processor: ATI/AMD (Modern GZDoom)
Location: Hungary
Contact:

Re: how do i check the player health?(zscript)

Post by OddYokai »

I forgot to ask that if I want to achieve the same effect with just armor instead of health, is it enough to rewrite the code like this?

Code: Select all

 If(other is "PlayerPawn" && other.BasicArmor <= 90)
 
as far as I know, armor and health work differently. Unfortunately I'm at work now and can't check. But curiosity doesn't let me rest
Jarewill
 
 
Posts: 1853
Joined: Sun Jul 21, 2019 8:54 am

Re: how do i check the player health?(zscript)

Post by Jarewill »

Armor does indeed work differently, in such it's actually stored in an inventory item rather than a variable like health.
So to check how much armor the player has, you have to check for the item:

Code: Select all

If(other is "PlayerPawn")
{
	If(other.FindInventory("BasicArmor").Amount <= 90) //This will return a pointer to the BasicArmor item, which it will find because players always have it no matter what, and check for its amount
	{
		//Rest of the code
User avatar
Marrub
 
 
Posts: 1202
Joined: Tue Feb 26, 2013 2:48 pm
Preferred Pronouns: No Preference
Operating System Version (Optional): Arch Linux
Graphics Processor: ATI/AMD with Vulkan/Metal Support
Contact:

Re: how do i check the player health?(zscript)

Post by Marrub »

FindInventory might return null (probably not on BasicArmor but certainly for other classes) so you might want to use CountInv, which has a built-in null check in it:

Code: Select all

other.CountInv("BasicArmor") <= 90)
User avatar
OddYokai
Posts: 15
Joined: Mon Jul 04, 2022 11:01 am
Preferred Pronouns: He/Him
Graphics Processor: ATI/AMD (Modern GZDoom)
Location: Hungary
Contact:

Re: how do i check the player health?(zscript)

Post by OddYokai »

Thank you both for your replies, it works great! I think this topic is completely solved.
Post Reply

Return to “Scripting”