How to count the number of tiles (of a given name) that currently exist inside the room?

I would like to initialize a counter with the manually drawn total tiles in a room. Like this:

holesLeft  = ???

Then when the player steps on them, I would swap the tile to say, the default white, and decrease the count by 1.

I need this to know when the player reaches zero.

Thank you very much

So, you have an emit function, all "holes" will, on that event, increment a counter, like this:

holes_left += 1

Now, you will have in the game script:

on enter do
emit "hole_count"
end

The hole script:

on hole_count do
holes_left += 1
end

on collect do
holes_left -= 1
play "white"
end

(The hole tile needs to be an item)

Thanks for your kind reply.

Either I don't understand your proposed solution correctly or I didn't explained myself correctly at the beginning.

screenshot
In the example image, you can see there are 10 holes in the room.

How can I count (programmatically) the number of said holes, so that I can store that number in the variable: holes_left, when this room loads.

I did this in a prototype and it worked:

In the player script:

on draw do
label "{holes_left}" at 1,1
end

on enter do
emit "hole_count"
end

In the script of the item tile named hole:

on collect do
holes_left -= 1
play "white"
end

on hole_count do
holes_left += 1
end

Thanks, this works. Although I don't know why! hahaha!

I can't seem to wrap my head around which part makes the hole-counting happen.

I'll read over and over your two replies and try to figure it out.

Just in case it helps -

The "emit" command calls the same event on each of the tiles in the room being entered. Most of those tiles aren't holes, so the event is simply ignored, but the hole tiles all have "on hole_count do", and so each of the hole tiles on screen handles the event separately. In turn, they all increase the "holes_left" variable (shared between them as all variables in pulp are global) by 1 to give a total number.

When a play walks into the hole item, the "on collect do" part is triggered for that particular hole, which decreases the holes_left variable by 1, and changes the tile to a "white" tile instead of a hole.

As the "enter" event is applied to all tiles in the room automatically (according to the docs), I think you could change the hole's "on hole_count do" to "on enter do", and remove the "on enter" block in the player script, but haven't tried it.

Wow. Super clear explanation. Now I better understand what is happening.

Thank you very much for taking time to put this in simple terms.

1 Like

No problem - I find the more I understand how pulp works "under the hood", the easier it is to figure out how to tie all the events together.