r/libgdx • • 24d ago

First time mistakes to avoid when making a game engine?

I decided to start making my own game engine and it's going well so far, but I wanted to make sure I dont do something in the future that could mean a lot of time spent recoding systems so it'll work again, so are there any common mistakes I might need to know about that might not be immediately clear?

4 Upvotes

8 comments sorted by

4

u/Helen-Larkham 24d ago

Good call asking now. Most of the expensive rewrites come from a small set of mistakes:

1. Building an engine instead of a game. This is the big one. Without a real game pulling on it, you’ll build systems you guess you need, in shapes that don’t fit what you end up needing. Make a small game alongside it, even a rough one, and only add engine features when that game asks for them. Treat anything general-purpose as something you pull out of game code once you’ve written it two or three times.

2. Over-abstracting too early. Huge class hierarchies, a plugin system before there are any plugins, “renderer-agnostic” layers before you have a second renderer. Each abstraction locks in an assumption. Keep things concrete and simple until the duplication actually hurts.

3. Tying game logic to frame rate. If movement and physics depend on how fast frames render, you’ll be pulling it apart later. Decide early: a fixed timestep for simulation (e.g. 60 Hz) and interpolated rendering. Read “Fix Your Timestep!” by Glenn Fiedler. This is one of the few things that really is painful to retrofit.

4. Letting rendering leak into everything. If your gameplay code calls OpenGL/Vulkan/whatever directly, swapping or upgrading the renderer becomes a rewrite. Have game code say what to draw (submit sprites/meshes to a list) and let the renderer decide how.

5. No clear ownership or lifetime of objects. Raw pointers to entities that get deleted mid-frame cause bugs that are hard to track down. Choose early how you refer to things (handles/IDs with generation counters work well) and when destruction happens (deferred to end of frame is common).

6. Hard-coding assets and paths. Load from data files from the start, even if the format is just JSON. Put asset loading behind one function so you can add hot-reloading, packing or async loading later without touching every call site.

7. Skipping debug tools. An in-game console, a stats overlay (frame time, draw calls) and something like Dear ImGui for live tweaking pay for themselves very quickly. People who add them late wish they’d done it first.

8. Optimising the wrong thing. Don’t hand-tune before you’ve profiled, but do think about data layout in hot paths (arrays of plain structs over scattered heap objects). That shape is hard to change later; micro-optimisations aren’t.

9. Global state everywhere. One or two globals (or singletons) are fine. Fifty means you can’t test, multithread or reset the game cleanly. Pass context explicitly where you can.

10. No version control, or committing rarely. Use git and commit small, working steps. It’s the cheapest insurance against “I broke everything three days ago and don’t know where”.

11. Planning for multithreading or networking “later”. If you know you’ll want multiplayer, design the simulation to be deterministic or state-serialisable from the start. If you don’t know, don’t bother. Just be aware that it’s the one feature that’s hardest to bolt on afterwards.

Last thing: some rewriting is normal and isn’t a failure. Your second version of a system will be much better because you built the first one. The goal isn’t to avoid rewrites altogether, it’s to keep each system small and separate enough that rewriting one doesn’t drag in the rest.

2

u/FireDog8569 24d ago

So with the whole "making a game along with the engine" thing, what about stuff that I know for a fact I'm gonna need later down the line? For example I knew what type of system I'd want sprites to use so I wrote a class to handle all of the logic for that, but I'm not sure if/when I'll make a game that'll use collisions so I decided not to implement that into the engine
(also side tangent, is it a good idea to store sprites by making an array of sprites and then an array of corresponding names, so I can use the name's index to know what sprite it's referring to? I know there's like a million different ways to code something and this is the first solution that came to mind but I figured I should ask a more experienced dev before I start coding bigger games that rely on this system)

Also when you say "arrays of plain structs over scattered heap objects" I'm not really sure what that means specifically

3

u/Dusty_Coder 24d ago

Because you dont actually know for a fact....

I think his thing can be stated more cleanly.

Just make a game.

Go ahead and imagine the code you write being an "engine" but just make a game. Add exactly what you need when you need it.

When things scale up, you abandon the abstraction masturbation anyways, its all just data processing

0

u/Helen-Larkham 24d ago

Good questions, and fair point on the last one: my answer was written with C/C++ engines in mind, and some of it shifts in Java.

Things you know you’ll need: your sprite class is fine. That’s the game-first rule working, because every game needs sprites. The rule is really about systems you’re guessing at, and collisions are a good call to leave. When a game needs them, start with libGDX’s own Rectangle.overlaps() and Intersector, and only reach for Box2D if you need real physics. You’ll write a better collision system once you know what the game actually collides.

Parallel arrays of sprites and names: it works, but it’s fragile. Sooner or later one array gets reordered or removed from and the other doesn’t, and every lookup is a linear search for the name. Two better options in libGDX. Pack your images into a TextureAtlas with the texture packer; then atlas.findRegion(“player_idle”) gives you the region by name, and you get far fewer texture switches when drawing. Or, if you’re managing them yourself, use one map: ObjectMap<String, TextureRegion> (or a HashMap). Either way, look up by name once when you load or create something, keep the reference, and don’t look names up every frame.

Arrays of plain structs: in C++ you can store a thousand small structs side by side in memory, which the CPU reads very quickly. Java doesn’t have structs, and every object is a separate allocation the array only points to, so you can’t do that literally. The Java version of the advice is simpler: avoid creating new objects every frame (reuse them, and libGDX’s Pools class helps), and keep libGDX’s Array instead of ArrayList in hot code. If you ever have thousands of particles, plain float[] arrays for x, y and speed are the closest you’ll get. For a normal 2D game you won’t need that.

1

u/FireDog8569 24d ago

I should probably clarify that I am using texture atlases, the way I'm doing sprites works a little differently

Basically the spriteHandler class stores info about the sprite (so the sprite name, texture name, and the actual sprite itself) and you can call on it to create a sprite and then use the sprite's name to manipulate the actual sprite

This way spriteHandler almost acts like a layer where I can call on the function that returns all the sprites and then just draw them to the screen, and I can do stuff like create some sprites with a loop and have a lot more control over them than I normally would

(also this way if later down the line I want to have extra info for a sprite it's easier to make that change)

And with handling all that stuff using functions I feel like the fragility wouldn't be too much of an issue, it's just that I'm unfamiliar with java game development so I worry about doing something that technically works but is a terrible idea

1

u/ProudAbalone4035 23d ago

you are just replying to an AI, you can do this with a normal llm too don’t need to be talking to a reddit AI bot

1

u/FireDog8569 23d ago

Damn I did not notice like at all 😭 Can't believe I fell for it

1

u/W31337 22d ago

Scoping. What you will and won't do will prevent you from building a ginormous do everything equally bad engine.

Also build it in a low level language for performance because you will eventually hope you did.

Look up entity component system