SpawnHealth taking into account WorldThingSpawned event

Remember, just because you request it, that doesn't mean you'll get it.

Moderator: GZDoom Developers

Kzer-Za
Posts: 521
Joined: Sat Aug 19, 2017 11:52 pm
Graphics Processor: nVidia (Modern GZDoom)

SpawnHealth taking into account WorldThingSpawned event

Post by Kzer-Za »

I don't know if it's possible, so please disregard it if I'm saying a stupid thing, but it would be nice if SpawnHealth function would take into account changes to actors Health made during WorldThingSpawned event. Currently this change is not taken into account.

Why it matters: some mods displaying an enemy healthbar rely on this function to determine the max amount of the actor's health. So if the actor's health is changed at WorldThingSpawned event, the healthbar displays incorrect information.
User avatar
Player701
 
 
Posts: 1697
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support

Re: SpawnHealth taking into account WorldThingSpawned event

Post by Player701 »

The Actor class has a field called StartHealth and it is not read-only. Try modifying it as well in your WorldThingSpawned override and see if it fixes your problem.
Kzer-Za
Posts: 521
Joined: Sat Aug 19, 2017 11:52 pm
Graphics Processor: nVidia (Modern GZDoom)

Re: SpawnHealth taking into account WorldThingSpawned event

Post by Kzer-Za »

No, it doesn't seem to help :(
SanyaWaffles
Posts: 822
Joined: Thu Apr 25, 2013 12:21 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Windows 11 for the Motorola Powerstack II
Graphics Processor: nVidia with Vulkan support
Location: The Corn Fields

Re: SpawnHealth taking into account WorldThingSpawned event

Post by SanyaWaffles »

Player701 wrote:The Actor class has a field called StartHealth and it is not read-only.
Hm, this makes me wonder if projects have been doing it wrong then. I wasn't aware that this field existed.
User avatar
Player701
 
 
Posts: 1697
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support

Re: SpawnHealth taking into account WorldThingSpawned event

Post by Player701 »

Kzer-Za wrote:No, it doesn't seem to help :(
It works for me though:

Code: Select all

class TestHandler : EventHandler
{
    override void WorldThingSpawned(WorldEvent e)
    {
        if (e.Thing is 'ZombieMan')
        {
            e.Thing.Health = e.Thing.StartHealth = 10;
        }
    }
}


upd
SanyaWaffles wrote:Hm, this makes me wonder if projects have been doing it wrong then. I wasn't aware that this field existed.
I think this field exists primarily to be modified, not to be read. For that, there is the SpawnHealth method (which I wasn't using in my own mod code for some reason, but I have fixed it now).
Kzer-Za
Posts: 521
Joined: Sat Aug 19, 2017 11:52 pm
Graphics Processor: nVidia (Modern GZDoom)

Re: SpawnHealth taking into account WorldThingSpawned event

Post by Kzer-Za »

Ah, both Health and StartHealth need to be changed in one line! Thanks, now it's working :)
User avatar
Player701
 
 
Posts: 1697
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support

Re: SpawnHealth taking into account WorldThingSpawned event

Post by Player701 »

Kzer-Za wrote:Ah, both Health and StartHealth need to be changed in one line! Thanks, now it's working :)
It doesn't really matter though, it's just to make code shorter. This also works for me just fine:

Code: Select all

class TestHandler : EventHandler
{
    override void WorldThingSpawned(WorldEvent e)
    {
        if (e.Thing is 'ZombieMan')
        {
            e.Thing.Health = 10;
            e.Thing.StartHealth = 10;
        }
    }
}
Kzer-Za
Posts: 521
Joined: Sat Aug 19, 2017 11:52 pm
Graphics Processor: nVidia (Modern GZDoom)

Re: SpawnHealth taking into account WorldThingSpawned event

Post by Kzer-Za »

For me it matters because I defined them relative to Health, like this:

Code: Select all

event.thing.Health = event.thing.StartHealth = event.thing.Health * 2;
The above works. The code below doesn't:

Code: Select all

event.thing.Health = event.thing.Health * 2;
event.thing.StartHealth = event.thing.Health * 2;
I'm using this because if I give an absolute figure in this event, then the spawned monster's health is the same regardless of the option "MonsterHealth = ..." in Skill definition, while I want it to increase/decrease proportionately with the health of all other monsters, which is governed by this option.
User avatar
Player701
 
 
Posts: 1697
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support

Re: SpawnHealth taking into account WorldThingSpawned event

Post by Player701 »

Kzer-Za wrote:The above works. The code below doesn't:

Code: Select all

event.thing.Health = event.thing.Health * 2;
event.thing.StartHealth = event.thing.Health * 2;
Well, of course it doesn't. The first line multiplies the health by 2. The second line sets StartHealth to a value of health * 2, but you shouldn't multiply it again because it has already been changed at this point. This is how your code should look like to work properly:

Code: Select all

event.thing.Health = event.thing.Health * 2;
event.thing.StartHealth = event.thing.Health;
Kzer-Za
Posts: 521
Joined: Sat Aug 19, 2017 11:52 pm
Graphics Processor: nVidia (Modern GZDoom)

Re: SpawnHealth taking into account WorldThingSpawned event

Post by Kzer-Za »

Ah, of course! I'm so stupid! :)
Kzer-Za
Posts: 521
Joined: Sat Aug 19, 2017 11:52 pm
Graphics Processor: nVidia (Modern GZDoom)

Re: SpawnHealth taking into account WorldThingSpawned event

Post by Kzer-Za »

Hm, I tested this code with MonsterHealth different from 1, and it seems to throw off the SpawnHealth function (even if I use absolute figures, not relative!). The bar displays the max Health as increased proportinately to MonsterHealth to a greater figure than it should be.

Example: I'm applying it to IronLich.
With "MonsterHealth = 1" its Health and StartHealth both become 1400, as they should be and the bar displays it correctly.
With "MonsterHealth = 0.5" its Health and StartHealth both are 700, as they should be, but the bar "thinks" that that max Health is 350.
With "MonsterHealth = 2" its Health and StartHealth become 2800, as they should be, but the bar "thinks" that that max Health is 5600.

I'm talking about Simple HUD Add-ons mod. Probably I should just contact the author and ask him to use "target.StartHealth" instead of "target.spawnhealth()" (I have changed it in the mod copy on my PC and it fixed the problem), but still it's strange that the function is thrown off by this skill setting.
User avatar
Player701
 
 
Posts: 1697
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support

Re: SpawnHealth taking into account WorldThingSpawned event

Post by Player701 »

Kzer-Za wrote:Probably I should just contact the author and ask him to use "target.StartHealth" instead of "target.spawnhealth()" (I have changed it in the mod copy on my PC and it fixed the problem), but still it's strange that the function is thrown off by this skill setting.
No. SpawnHealth() is the right way to go, because it accounts for skill factors. StartHealth is what you use to modify the base value.

The problem here is that you derive the value of StartHealth from Health, which at this point has already been adjusted by the skill settings. Therefore, the output value of SpawnHealth() is getting adjusted twice. Derive StartHealth from Default.Health instead, and it should work right this time:

Code: Select all

e.Thing.Health = e.Thing.Health * 2;
e.Thing.StartHealth = e.Thing.Default.Health * 2;
Kzer-Za
Posts: 521
Joined: Sat Aug 19, 2017 11:52 pm
Graphics Processor: nVidia (Modern GZDoom)

Re: SpawnHealth taking into account WorldThingSpawned event

Post by Kzer-Za »

Okay, I have just tested it and it leads to StartHealth having always the same value regardless of the MonsterHealth value. Maybe I could set it as "= e.Thing.Default.Health * 2 * x.MonsterHealth"? I suppose it should be stored somewhere, but I don't know where, i.e. what X is.
User avatar
Player701
 
 
Posts: 1697
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support

Re: SpawnHealth taking into account WorldThingSpawned event

Post by Player701 »

Kzer-Za wrote:Okay, I have just tested it and it leads to StartHealth having always the same value regardless of the MonsterHealth value.
Yes, that's exactly how it's supposed to be. When you call SpawnHealth() to get the initial health value, it will get adjusted by the skill factor, and you'll get the correct initial health as a result. You don't have to account for the skill yourself, the engine will do it for you.
Kzer-Za
Posts: 521
Joined: Sat Aug 19, 2017 11:52 pm
Graphics Processor: nVidia (Modern GZDoom)

Re: SpawnHealth taking into account WorldThingSpawned event

Post by Kzer-Za »

Ah, indeed, I just looked at what the console said, bat the bar sees the correct figure for max health.

Thanks a lot!

Return to “Feature Suggestions [GZDoom]”