SpawnHealth taking into account WorldThingSpawned event
Moderator: GZDoom Developers
-
- Posts: 521
- Joined: Sat Aug 19, 2017 11:52 pm
- Graphics Processor: nVidia (Modern GZDoom)
SpawnHealth taking into account WorldThingSpawned event
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.
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.
-
-
- Posts: 1697
- Joined: Wed May 13, 2009 3:15 am
- Graphics Processor: nVidia with Vulkan support
Re: SpawnHealth taking into account WorldThingSpawned event
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.
-
- Posts: 521
- Joined: Sat Aug 19, 2017 11:52 pm
- Graphics Processor: nVidia (Modern GZDoom)
Re: SpawnHealth taking into account WorldThingSpawned event
No, it doesn't seem to help 

-
- 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
Hm, this makes me wonder if projects have been doing it wrong then. I wasn't aware that this field existed.Player701 wrote:The Actor class has a field called StartHealth and it is not read-only.
-
-
- Posts: 1697
- Joined: Wed May 13, 2009 3:15 am
- Graphics Processor: nVidia with Vulkan support
Re: SpawnHealth taking into account WorldThingSpawned event
It works for me though:Kzer-Za wrote:No, it doesn't seem to help
Code: Select all
class TestHandler : EventHandler
{
override void WorldThingSpawned(WorldEvent e)
{
if (e.Thing is 'ZombieMan')
{
e.Thing.Health = e.Thing.StartHealth = 10;
}
}
}

upd
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).SanyaWaffles wrote:Hm, this makes me wonder if projects have been doing it wrong then. I wasn't aware that this field existed.
-
- Posts: 521
- Joined: Sat Aug 19, 2017 11:52 pm
- Graphics Processor: nVidia (Modern GZDoom)
Re: SpawnHealth taking into account WorldThingSpawned event
Ah, both Health and StartHealth need to be changed in one line! Thanks, now it's working 

-
-
- Posts: 1697
- Joined: Wed May 13, 2009 3:15 am
- Graphics Processor: nVidia with Vulkan support
Re: SpawnHealth taking into account WorldThingSpawned event
It doesn't really matter though, it's just to make code shorter. This also works for me just fine:Kzer-Za wrote:Ah, both Health and StartHealth need to be changed in one line! Thanks, now it's working
Code: Select all
class TestHandler : EventHandler
{
override void WorldThingSpawned(WorldEvent e)
{
if (e.Thing is 'ZombieMan')
{
e.Thing.Health = 10;
e.Thing.StartHealth = 10;
}
}
}
-
- Posts: 521
- Joined: Sat Aug 19, 2017 11:52 pm
- Graphics Processor: nVidia (Modern GZDoom)
Re: SpawnHealth taking into account WorldThingSpawned event
For me it matters because I defined them relative to Health, like this:
The above works. The code below doesn't:
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.
Code: Select all
event.thing.Health = event.thing.StartHealth = event.thing.Health * 2;
Code: Select all
event.thing.Health = event.thing.Health * 2;
event.thing.StartHealth = event.thing.Health * 2;
-
-
- Posts: 1697
- Joined: Wed May 13, 2009 3:15 am
- Graphics Processor: nVidia with Vulkan support
Re: SpawnHealth taking into account WorldThingSpawned event
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: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;
Code: Select all
event.thing.Health = event.thing.Health * 2;
event.thing.StartHealth = event.thing.Health;
-
- Posts: 521
- Joined: Sat Aug 19, 2017 11:52 pm
- Graphics Processor: nVidia (Modern GZDoom)
Re: SpawnHealth taking into account WorldThingSpawned event
Ah, of course! I'm so stupid! 

-
- Posts: 521
- Joined: Sat Aug 19, 2017 11:52 pm
- Graphics Processor: nVidia (Modern GZDoom)
Re: SpawnHealth taking into account WorldThingSpawned event
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.
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.
-
-
- Posts: 1697
- Joined: Wed May 13, 2009 3:15 am
- Graphics Processor: nVidia with Vulkan support
Re: SpawnHealth taking into account WorldThingSpawned event
No. SpawnHealth() is the right way to go, because it accounts for skill factors. StartHealth is what you use to modify the base value.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.
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;
-
- Posts: 521
- Joined: Sat Aug 19, 2017 11:52 pm
- Graphics Processor: nVidia (Modern GZDoom)
Re: SpawnHealth taking into account WorldThingSpawned event
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.
-
-
- Posts: 1697
- Joined: Wed May 13, 2009 3:15 am
- Graphics Processor: nVidia with Vulkan support
Re: SpawnHealth taking into account WorldThingSpawned event
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 wrote:Okay, I have just tested it and it leads to StartHealth having always the same value regardless of the MonsterHealth value.
-
- Posts: 521
- Joined: Sat Aug 19, 2017 11:52 pm
- Graphics Processor: nVidia (Modern GZDoom)
Re: SpawnHealth taking into account WorldThingSpawned event
Ah, indeed, I just looked at what the console said, bat the bar sees the correct figure for max health.
Thanks a lot!
Thanks a lot!