Using CVAR slider to set default health for actor

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!)

Using CVAR slider to set default health for actor

Postby Edwin Grimbanks » Wed May 18, 2022 8:39 am

Hello, I am in a bit of a pickle, as I am currently struggling to set a custom health slider via options to a certain monster, but I have no idea of how to put it in it's ZScript.
Is there any way to do it ?
Thank you for the help !
Edwin Grimbanks
 

Re: Using CVAR slider to set default health for actor

Postby Player701 » Fri Jun 03, 2022 6:27 am

CVARs can be referenced directly from ZScript code. Suppose your CVAR is called mymonster_health, then you could adjust the health in the BeginPlay override of your monster class:

Code: Select allExpand view
class MyMonster : Actor
{
    ...
   
    override void BeginPlay()
    {
        Super.BeginPlay();
        Health = StartHealth = Max(1, mymonster_health);
    }
}

Note that we adjust both Health and StartHealth since the latter is required to make sure the monster's health is correctly reset upon resurrection. The latter is also necessary to correctly calculate the level of health when the "extreme" (i.e. gibbing) death animation is played.

NB: The Max(...) call is there to prevent setting non-positive health values (generally a bad idea for monsters: try it out and see what I mean). If you also want to take into account the health factor for the current skill, multiply the value by G_SkillPropertyFloat(SKILLP_HealthFactor) (cast result to int to avoid float truncation warning):

Code: Select allExpand view
class MyMonster : Actor
{
    ...
   
    override void BeginPlay()
    {
        Super.BeginPlay();
        Health = StartHealth = int(Max(1, mymonster_health * G_SkillPropertyFloat(SKILLP_HealthFactor)));
    }
}
User avatar
Player701
 
 
 
Joined: 13 May 2009
Location: Russia
Discord: Player701#8214
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support

Re: Using CVAR slider to set default health for actor

Postby 22alpha22 » Fri Jun 03, 2022 9:03 am

I know this isn't my thread but the way you are showing the op to use CVars in ZScript is confusing to me.
Player701 wrote:CVARs can be referenced directly from ZScript code

I thought you had to use either GetCVar for a CVar attached to a player or FindCVar for a global or server CVar.
Code: Select allExpand view
CVar.GetCVar(mymonster_health,Player).GetInt(); //Player specific CVar
CVar.FindCVar(mymonster_health).GetInt(); //Global or server CVar

Has something changed, is the above no longer the correct way to use CVars in ZScript?
User avatar
22alpha22
So lonely...
 
Joined: 21 Feb 2014
Location: Montana, USA
Operating System: Windows 10/8.1/8/201x 64-bit
Graphics Processor: nVidia with Vulkan support

Re: Using CVAR slider to set default health for actor

Postby Player701 » Fri Jun 03, 2022 9:19 am

22alpha22 wrote:I know this isn't my thread but the way you are showing the op to use CVars in ZScript is confusing to me.
Player701 wrote:CVARs can be referenced directly from ZScript code

I thought you had to use either GetCVar for a CVar attached to a player or FindCVar for a global or server CVar.
Code: Select allExpand view
CVar.GetCVar(mymonster_health,Player).GetInt(); //Player specific CVar
CVar.FindCVar(mymonster_health).GetInt(); //Global or server CVar

Has something changed, is the above no longer the correct way to use CVars in ZScript?

The code above assumes that the CVAR is server-scope, otherwise it will error out upon compilation. It is basically a shortcut for CVar.FindCVar(...).GetXXX() depending on its type. Both ways are correct.
User avatar
Player701
 
 
 
Joined: 13 May 2009
Location: Russia
Discord: Player701#8214
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support


Return to Scripting

Who is online

Users browsing this forum: No registered users and 1 guest