How to Optimize my game


Im working on optimizing my 2D side-scrolling fighting game.

It seems that MonoGame uses only one thread, how can I use more? For example, is it possible to put drawing/rendering on another thread so my main thread can focus on calculations? The game is kinda heavy because the animation system and collision detection are complicated, but I’d like the game to run on older laptops too.

Any other tips for optimizing games? Good practices?

Now that I’m here, how can I use Source/DestinationRectangle for smooth tiling parallax? Sometimes background is visibly “shaking” because Rectangles use ints and not floats.

Thank you

  • Mikosama

I’d say take a look at optimizing your animation and collision systems. If done properly these shouldn’t be too heavy for a 2D game. Try profiling to find bottlenecks, VS has great tools.

It’s generally a good idea to avoid producing garbage, the GC can cause lag spikes. Though if you have a consistent low framerate try profiling and optimizing first. Other than that there aren’t any general optimization tips I can think of. Feel free to post some code if you’re unsure how to optimize and people here can help you out.

If your system wasn’t built with multithreading in mind, it will most likely be a very difficult task to refactor it to work in a multithreaded environment. There is a variant of the famous quote

Some people, when confronted with a problem, think, “I know, I’ll use regular expressions.” Now they have two problems.

to do with multithreading

Some people, when confronted with a problem, think “I know, I’ll use multithreading”. Nothhw tpe yawrve o oblems.

or the old “Knock knock” joke

Knock, knock
Race condition
Who’s there?

It’s funny, because it’s true.

Keep the rendering on the same thread, but farm the calculations off to other threads. Break up the animation and collision into separate jobs that can be calculated on their own without referencing any other data. Submit those jobs to a thread pool. Don’t render using the same objects that are being updated by the thread pool. Submit enough data from each object to a “draw queue”. Now the rendering can render from that draw queue and the object can be updated from the thread pool. Synchronize the rendering with the completion of the draw queue.

It’s not a simple task, and there are many traps along the way.

1 Like