Can a function execute over more than one tick to save performance?

If you keep a list or array of all of your characters, then you could keep a variable that contains the last index looked at (let’s call it N, and set it to 0 at game start) and another variable for max characters per tick (let’s call it X). Start at index N, and execute your function X times, then store the index number you stopped at + 1 in N. Next tick, just run the same code again.

You’ll need to do some array bounds checking and such so you don’t run beyond the end of the array, and when you get to the end, reset N to 0 again. Miscellaneous housekeeping like that.

It’s a pretty naive algorithm, but it might work in a pinch.