Multiply monster Health based on amount of players

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
Mini--Joe
Posts: 97
Joined: Sun Jul 27, 2014 10:25 am

Multiply monster Health based on amount of players

Post by Mini--Joe »

I need to do other things similar to this, but this is the most important.
How would I do this? I need it to happen to all monsters, hopefully also including monsters spawned in through scripts.

*EDIT: Specifically, I want it to affect any thing with the ISMONSTER flag.
Blue Shadow
Posts: 5043
Joined: Sun Nov 14, 2010 12:59 am

Re: Multiply monster Health based on amount of players

Post by Blue Shadow »

One way to do it is with a [wiki=Events_and_handlers#WorldThingSpawned]WorldThingSpawned[/wiki] event (yes, this is ZScript):

Code: Select all

class AdjustHealthEvent : EventHandler
{
    // Returns how many players are currently in the game.
    private int CountPlayers ()
    {
        int count = 0;

        for (int i = 0; i < MAXPLAYERS; i++)
        {
            if (playeringame[i])
            {
                count++;
            }
        }

        return count;
    }

    override void WorldThingSpawned (WorldEvent e)
    {
        // On spawn, adjust a monster's health depending on the number of players.
        if (e.Thing.bIsMonster)
        {
            e.Thing.health *= CountPlayers();
        }
    }
}
Here's a demo:
https://www.dropbox.com/s/2y2szjshpya8g ... p.pk3?dl=1
User avatar
Mini--Joe
Posts: 97
Joined: Sun Jul 27, 2014 10:25 am

Re: Multiply monster Health based on amount of players

Post by Mini--Joe »

Does the file with that code in it have to be named zscript.txt? The page for Zscript on the zdoom wiki says I should rename it to avoid conflict.

Also, what it looks like it is doing is multiplying the monster health by the number of players. How would I make it be a smaller multiplier, like only 1.2x per player?
Blue Shadow
Posts: 5043
Joined: Sun Nov 14, 2010 12:59 am

Re: Multiply monster Health based on amount of players

Post by Blue Shadow »

Mini--Joe wrote:Does the file with that code in it have to be named zscript.txt? The page for Zscript on the zdoom wiki says I should rename it to avoid conflict.
Just like DECORATE, ZScript is cumulative, which means you can have multiple ZScript lumps without them conflicting with each other.
Also, what it looks like it is doing is multiplying the monster health by the number of players. How would I make it be a smaller multiplier, like only 1.2x per player?
Would this do?

Code: Select all

    override void WorldThingSpawned (WorldEvent e)
    {
        if (e.Thing.bIsMonster)
        {
            int pcount = CountPlayers();

            // For every additional player in the game, add 20% to the monster's health.
            if (--pcount > 0)
            {
                int adjusted_hp = e.Thing.health + int(e.Thing.health * pcount * 0.2);

                // Make sure the adjusted health is greater than zero before setting it.
                if (adjusted_hp > 0)
                {
                    e.Thing.health = adjusted_hp;
                }
            }
        }
    }
User avatar
Mini--Joe
Posts: 97
Joined: Sun Jul 27, 2014 10:25 am

Re: Multiply monster Health based on amount of players

Post by Mini--Joe »

Where do I place that?

If i put it after the AdjustHealthEvent, I just get the error "Unexpected 'override' Expecting end of file or 'include' or 'extend' or 'class' or 'struct' or 'const' or 'enum'".
Blue Shadow
Posts: 5043
Joined: Sun Nov 14, 2010 12:59 am

Re: Multiply monster Health based on amount of players

Post by Blue Shadow »

Check the demo file. It should replace the same function in there.
User avatar
Mini--Joe
Posts: 97
Joined: Sun Jul 27, 2014 10:25 am

Re: Multiply monster Health based on amount of players

Post by Mini--Joe »

Ok I figured out the problem, I placed it in the wrong spot; I am not knowledgeable with scripting at all. Thanks for your help, it makes this experience so much better.
Post Reply

Return to “Scripting”