Page 1 of 1

Help with basic even handler

Posted: Thu Mar 06, 2025 6:03 pm
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);
			}
    }

Re: Help with basic even handler

Posted: Sat Mar 08, 2025 12:28 pm
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.

Re: Help with basic even handler

Posted: Mon Mar 17, 2025 11:46 am
by Blue Shadow
Jarewill wrote: Sat Mar 08, 2025 12:28 pm GetMaxHealth() is a player function, [...]
That's incorrect.

Re: Help with basic even handler

Posted: Mon Mar 31, 2025 11:27 am
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