How to best call performance intensive game logic functions?

Hello,

I’m programming a grid based game. One of my game logic functions goes through every single element of that grid to do things.

private void GoThroughMap(Action<int,int,int> action)
    {
        for (x = 0; x < mapX; x++)
        {
            for (y = 0; y < mapY; y++)
            {
                for (z = 0; z < mapZ; z++)
                {
                    action(x, y, z);
                }
            }
        }
    }

Now I had this in void FixedUpdate initially and it worked well enough. Until I decided to actually test this with a 1000 by 1000 grid (x and z, y was 1) instead of the 100 by 100 grid I used before. Game performance crashed.

I only need that function every second or so, which I assume would massively reduce the load on my game. But I’m not sure what’s the best way to implement this. Keep it in void update with a float timer to check against? Use InvokeRepeating(string methodName, float time, float repeatRate); ? Some coroutine? Or something else?

That’s a start. What you can also do is differ it so you calculate 10% every 0.1 seconds so it’s not doing one massive recalc every second or so.

Is it neccesary that you check everything every frame? Wouldn’t it better to use some kind of EventManager?
I guess it depends on the game, but probably an EventManager should do the trick in most of cases. I made one here: https://github.com/MonoFlauta/Framework-Goat/tree/master/Framework%20Goat/Assets/Framework%20Goat/EventManager
And if not, did you consider trying out ECS? Unity Connect