SpawnHealth taking into account WorldThingSpawned event

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

Moderator: GZDoom Developers

SpawnHealth taking into account WorldThingSpawned event

Postby Kzer-Za » Wed Nov 11, 2020 11:25 pm

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.
Kzer-Za
 
Joined: 20 Aug 2017

Re: SpawnHealth taking into account WorldThingSpawned event

Postby Player701 » Thu Nov 12, 2020 1:35 am

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.
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: SpawnHealth taking into account WorldThingSpawned event

Postby Kzer-Za » Thu Nov 12, 2020 1:54 am

No, it doesn't seem to help :(
Kzer-Za
 
Joined: 20 Aug 2017

Re: SpawnHealth taking into account WorldThingSpawned event

Postby SanyaWaffles » Thu Nov 12, 2020 1:56 am

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
SanyaWaffles
Wouldn't be an epic gamer if I didn't commit a few war crimes.
 
Joined: 25 Apr 2013
Location: Eastern Ohio
Discord: SanyaWaffles#5095
Twitch ID: sanyawaffles
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: SpawnHealth taking into account WorldThingSpawned event

Postby Player701 » Thu Nov 12, 2020 1:57 am

Kzer-Za wrote:No, it doesn't seem to help :(

It works for me though:

Code: Select allExpand view
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).
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: SpawnHealth taking into account WorldThingSpawned event

Postby Kzer-Za » Thu Nov 12, 2020 2:09 am

Ah, both Health and StartHealth need to be changed in one line! Thanks, now it's working :)
Kzer-Za
 
Joined: 20 Aug 2017

Re: SpawnHealth taking into account WorldThingSpawned event

Postby Player701 » Thu Nov 12, 2020 2:10 am

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 allExpand view
class TestHandler : EventHandler
{
    override void WorldThingSpawned(WorldEvent e)
    {
        if (e.Thing is 'ZombieMan')
        {
            e.Thing.Health = 10;
            e.Thing.StartHealth = 10;
        }
    }
}
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: SpawnHealth taking into account WorldThingSpawned event

Postby Kzer-Za » Thu Nov 12, 2020 2:25 am

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

Code: Select allExpand view
event.thing.Health = event.thing.StartHealth = event.thing.Health * 2;


The above works. The code below doesn't:

Code: Select allExpand view
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.
Kzer-Za
 
Joined: 20 Aug 2017

Re: SpawnHealth taking into account WorldThingSpawned event

Postby Player701 » Thu Nov 12, 2020 2:28 am

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

Code: Select allExpand view
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 allExpand view
event.thing.Health = event.thing.Health * 2;
event.thing.StartHealth = event.thing.Health;
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: SpawnHealth taking into account WorldThingSpawned event

Postby Kzer-Za » Thu Nov 12, 2020 2:29 am

Ah, of course! I'm so stupid! :)
Kzer-Za
 
Joined: 20 Aug 2017

Re: SpawnHealth taking into account WorldThingSpawned event

Postby Kzer-Za » Thu Nov 12, 2020 3:49 am

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.
Kzer-Za
 
Joined: 20 Aug 2017

Re: SpawnHealth taking into account WorldThingSpawned event

Postby Player701 » Thu Nov 12, 2020 4:16 am

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 allExpand view
e.Thing.Health = e.Thing.Health * 2;
e.Thing.StartHealth = e.Thing.Default.Health * 2;
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: SpawnHealth taking into account WorldThingSpawned event

Postby Kzer-Za » Thu Nov 12, 2020 4:32 am

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.
Kzer-Za
 
Joined: 20 Aug 2017

Re: SpawnHealth taking into account WorldThingSpawned event

Postby Player701 » Thu Nov 12, 2020 4:36 am

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.
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: SpawnHealth taking into account WorldThingSpawned event

Postby Kzer-Za » Thu Nov 12, 2020 5:08 am

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

Thanks a lot!
Kzer-Za
 
Joined: 20 Aug 2017


Return to Feature Suggestions

Who is online

Users browsing this forum: No registered users and 1 guest