Pools
Monkey Forums/Monkey Programming/Pools
| ||
I am maintaining platforms using a list in a fifo manner.Class Level Field platforms:List<Platform> ' using as a queue to hold platforms (fifo) Method CreatePlatform:Void() Local randomX:Int = Rnd(0, 400) Local randomY:Int = Rnd(1, 400) ' add the platform to the queue platforms.AddLast(New Platform(randomX, randomY)) End Method Method RemovePlatform:Void() platforms.RemoveFirst() ' remove the first platform End Method End Class CreatePlatform is called every x seconds from my game loop. I then call RemovePlatform continuously to remove the first in platform when there are more than MAX_PLATFORMS. I have read that calling new in the main loop can degrade performance. So, I am hoping to convert my above code to use a pool instead of new. Any help with this? I am trying to maintain my fifo logic, so is a List still okay to use? Thank you |
| ||
Once you got a full list, why not just iterate through it and modify the oldest instance with the values you want for the new platform? I wouldn't worry about it too much unless it's being called intensively. |
| ||
CreatePlatform is being called continuously throughout my game, a new platform is spawned every second. |
| ||
Every second is actually 'rare' in terms of a game. Don't worry about it until it becomes a problem (meaning you have identified it using your profiler). So you should be fine using New. |
| ||
I have identified it as a problem, which is why I'm asking. |
| ||
I also am surprised that this is a problem, unless creating platforms is very heavyweight. You are not reloading graphics or anything when you construct a platform? Anyway, the above is the way to use Pool. Set MAXPLATFORMS to as many as you think you will need at once. If other classes will be creating platforms too, or if Level is not persistent, make the pool global. |
| ||
I have identified it as a problem, which is why I'm asking. If one alloc/dealloc per second is causing an issue, then there is something weird going on. How did you identify the problem? |