Help with basic even handler

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!)
Eredhen
Posts: 1
Joined: Thu Mar 06, 2025 4:52 pm
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia (Modern GZDoom)

Help with basic even handler

Post by Eredhen »

I'm really new to Zscript and is really rotting my mind.
Can't figure to make a Monster give an item called "ExpPoints" to the player every time a monster is killed.
And I tried to take from a Youtube tutorial, the idea of giving more points to the player, for every 20HP a monster has.
This is probably been answered multiple times but I have searched the forums for more than 2 hours and can't find an answer (maybe is because is too basic)
Thank you beforehand :D

Code: Select all

class RewardStuff : EventHandler
{
	
    override void WorldThingDied (worldevent e) 
	{
	int maxHealth = e.Thing.GetMaxHealth();
	    
    if (!e.thing || !e.thing.bISMONSTER || !e.thing.target || !e.thing.target.player)
    return;
	for (int i = 0; i < maxHealth; i += 20)
			{
			if (e.thing.player && !e.thing.bISMONSTER)   //check the killed actor exists and is a monster
            e.thing.GiveInventory("ExpPoint",1);
			}
    }
Jarewill
 
 
Posts: 1853
Joined: Sun Jul 21, 2019 8:54 am

Re: Help with basic even handler

Post by Jarewill »

GetMaxHealth() is a player function, to get the max health of monsters you want to use SpawnHealth().
Now with the first check you have it set up correctly, the code will proceed only if the killed actor was a monster and the killer was a player.
However with the second check you have it reversed, it checks if the killed actor was a player and not a monster. The first check already has everything covered so you don't need to worry about even including a second one.
Now the for loop is correct, but it's not particularly needed, you can just use math in GiveInventory: maxHealth / 20.
Blue Shadow
Posts: 5039
Joined: Sun Nov 14, 2010 12:59 am

Re: Help with basic even handler

Post by Blue Shadow »

Jarewill wrote: Sat Mar 08, 2025 12:28 pm GetMaxHealth() is a player function, [...]
That's incorrect.
Jarewill
 
 
Posts: 1853
Joined: Sun Jul 21, 2019 8:54 am

Re: Help with basic even handler

Post by Jarewill »

Blue Shadow wrote: Mon Mar 17, 2025 11:46 am That's incorrect.
My bad.
Well it looks like it ultimately calls SpawnHealth() anyways, so the end result is still the same.
Thanks for clarifying though. :D

Return to “Scripting”