event handler - get x, y, z of health pickup when it's picked up

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!
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!)
cjdubreu
Posts: 4
Joined: Tue Mar 31, 2026 5:44 am
Operating System Version (Optional): windows

event handler - get x, y, z of health pickup when it's picked up

Post by cjdubreu »

I've created a custom pickup in ZScript called "HealthPlant." It's just a health item that gives the player 3 health upon pickup. I used ZScript because I’ve heard it’s the better scripting language to learn these days.

Anyway, since it’s a plant, I thought it would be cool to spawn some leaves that fall to the ground when picked up. I want to use an event handler for this — every time the "HealthPlant" is picked up, I want to spawn some of my custom leaf sprites, which will then fall to the ground.

However, I’m struggling with this; I can get my leaf sprites to spawn when a monster dies (I'll provide the ZScript for that below), but I can’t seem to get this to work with a health pickup. The monster death script uses - override void WorldThingDied(WorldEvent e) - but is there an equivalent event for item pickups? Specifically, can I make it so that when a specific item type (in this case, "HealthPlant") is picked up, I can get access to that object’s x, y, and z position?

Here’s the working code for spawning my "clippings" sprites when a monster dies:




class MonsterDeathEventHandler : EventHandler {


override void WorldThingDied(WorldEvent e) {

vector3 pos = e.Thing.pos;

double height = e.Thing.height;

int maxHealth = e.Thing.getMaxHealth();

for (int i = 0; i < maxHealth; i += 10) {

let spawnedActor = Actor.Spawn("Clipping", (pos.x, pos.y, pos.z + (height/2)));

spawnedActor.vel.X = frandom(-3, 3);

spawnedActor.vel.Y = frandom(-3, 3);

spawnedActor.vel.Z = frandom(5, 10);

}

}


}
User avatar
Average Raven Fan
Posts: 22
Joined: Sat Oct 05, 2024 12:00 am

Re: event handler - get x, y, z of health pickup when it's picked up

Post by Average Raven Fan »

Let's start off by cutting the Gordian Knot : do it on the plant's actor instead of using a handler.

Code: Select all

class ShatterBonus : HealthBonus replaces HealthBonus
{
	override bool CanPickup(Actor toucher)
	{
		if(Super.CanPickup(toucher))
		{
			for(int i = 0; i < 16; i++)
			{
				let spawnedActor = Spawn("IceChunk", pos);
				spawnedActor.AddZ(height/2);
				spawnedActor.vel.X = frandom(-3, 3);
				spawnedActor.vel.Y = frandom(-3, 3);
				spawnedActor.vel.Z = frandom(5, 10);
			}
			return true;
		}
		return false;
	}
}
This is a health bonus that shatters on pickup, because you stomp on it instead of drinking it, you see. Add that override to your plant actor and change it to your liking!

I tried to see if it could be done with handlers and I just could not figure out how. Technically pickups never die, they're still alive in your inventory.

EDIT : Of course, just as I say that, I figure out I forgot something Very Important in the handler's code I was testing out. Oops! Give this a go :

Code: Select all

	override void WorldThingDestroyed(WorldEvent e)
	{
		if(e.Thing is "HealthBonus")
		{
			for(int i = 0; i < 16; i++)
			{
				let spawnedActor = e.Thing.Spawn("IceChunk", e.Thing.pos);
				spawnedActor.AddZ(e.Thing.height/2);
				spawnedActor.vel.X = frandom(-3, 3);
				spawnedActor.vel.Y = frandom(-3, 3);
				spawnedActor.vel.Z = frandom(5, 10);
			}
		}
	}
Add to your handler. This should do exactly the same as said shattering health bonus above, modify as needed! That said, do consider putting it on the actor instead.
cjdubreu
Posts: 4
Joined: Tue Mar 31, 2026 5:44 am
Operating System Version (Optional): windows

Re: event handler - get x, y, z of health pickup when it's picked up

Post by cjdubreu »

Hello there!

I just wanted to say thanks; your script suggestion worked perfectly!

So far, I’ve used your first method (putting it on the actor). I’ll try the other version someday, but for now I’m just so happy to have this working.

Thanks again so much for your help!

Return to “Scripting”