Will this lag my game? I’ve got a function that’s triggered every 0.8 seconds. This function generates a random number between 1-2. If, 1 is selected, it performs a spawn enemy action, which in the beginning goes through approx 10 if-statements, to check my powerups, my level and so on, to know which enemy to spawn. After those are done, it spawns the enemy. Then it goes through another 15 if statements to perform some other stuff. All inside that one function… Will this lag my game and is there any wya to get around this? [import]uid: 30185 topic_id: 6225 reply_id: 306225[/import]
Yeah I keep spawning new enemies all the time, but I also destroy them after they leave screen on collision with a rectangle… Thought that would solve the memory issue? since there is maximum 10-20 enemies on screen at once, maximum [import]uid: 30185 topic_id: 6225 reply_id: 21440[/import]
I never said anything about a memory issue, I said there can be a performance hit. Memory and performance are indirectly related, but don’t get the two confused. “Memory” is how much space the data takes up, while “performance” is how quickly the program runs.
In terms of spawning for example, every time you spawn an enemy some memory is taken up to store the data for that enemy. Meanwhile, the computer takes some time to load up that data and initialize everything. I wasn’t talking about how much data is loaded; I was talking about how long it takes to load that data. [import]uid: 12108 topic_id: 6225 reply_id: 21449[/import]
Too many “if” statements could certainly have a performance impact, but 25 is nothing. Too many is more like thousands. Which doesn’t mean you literally have to write 1000 “if” statements; if the check happens in a loop (eg. loop through every enemy and there are lots) then those checks pile up.
Anyway, the real performance hit in the situation you describe is the spawning. You could do hundreds of conditional checks every frame with no problem, but if your spawn involves loading up a lot of new graphics or sounds or whatever that could be expensive. Worrying about “if” statements is a distraction from real performance killers. [import]uid: 12108 topic_id: 6225 reply_id: 21416[/import]