Enemies taking turns?

Hi all! I'm still slowly making progress on my traditional (turn-based) roguelike in Pulp, and I'm in need of some help again. This time I'm struggling with making the enemies wait for their turns.

When I started out, I had the player take their action, then emitted "enemyTurn" and had all enemies take their action based on that. Worked fine in a room with only one enemy, but with multiple enemies in the room it becomes difficult to keep track of the action.

My next solution was to use two nested while loops to loop through all the tiles in the room, calling their "enemyTurn" function. If an "enemyTurn" function would be hit, that'd mean the tile was an enemy, the loops would be aborted and the enemy would do its thing. Afterwards, the enemy would call for the loop to resume, going to the next enemy.

In practice this broke performance on the machine, caused things to get out of sync and allowed enemies whose turn consisted in "moving to the right" or "moving to the bottom" to get multiple turns in a row.

Do you have any advice on how to make enemies wait for their turn in an orderly fashion?
Thanks in advance!

I managed to find a relatively efficiënt and elegant solution myself.

In case somebody with the same issue comes across this, here's how I solved it:

At the end of the player turn:
enemyCounter = 0
emit "enemyTurn"

And on every enemy (or on one central parent via mimic):
on enemyTurn do
timeToWait = enemyCounter
timeToWait *= 0.4
wait timeToWait then
// Whatever the enemy does on their turn

This way, every enemy gets 0.4 seconds to do their thing before the next one goes. Adjust as needed.

Using "emit" instead of looping through all tiles turned out to be much more performant in this particular case.

Hi there, glad you found a solution for this!

I had a similar use case, where I was generating tiles in random places, and using emit to update them. TBH, I wasn't hitting performance issues, but I do kind of have an unhealthy urge for optimisation, so wanted to try something out.

The code at First go at a way to store x/y positions in a pseudo, 10-element-max "array" in pulpscript · GitHub has functions that wrap a bunch of variables, so that I can basically just use a bunch of if/else statements repeatedly without having to think too hard. It's working for me so far, but would need extending if you need to keep track of more than 10 tiles at a time.

(On a sidenote, I think it also means you could fairly easily store all the variables involved, and restore them later, if you wanted to put the enemy positions back as they were after a save/reload.)

Might not be useful, but was fun to do anyway :wink:

1 Like