Help Did you try to write a game server in Lua?
Hello. Recently, I've been thinking about writing my own game server in Lua. However, I didn't find many server examples in the internet, not to mention how incomplete they are. I'd like to ask the following things:
- Possibility of writing a game server in Lua.
- Libraries, which are used to write a game server in Lua
- The difficulties that may occur when writing a game server in Lua.
Thank you for your answer.
9
Upvotes
12
u/qwyss Aug 11 '26
I mean, you open some sockets, squirt messages back and forth and booom, U dun.
Really it depends on the game, you can get away with basic TCP streams as long as you design around it.
If you are wanting local state prediction/rewinds with UDP updates then it all gets very very complicated very fast but lua isn't the problem :)
One thing I do think you always need is some task/threading. I was using lanes but I had some issues and ended up writing my own. Really lanes seems more designed towards firing off lots of small one shot tasks and I needed the opposite.
2
u/SleepyGuyy Aug 11 '26
I wonder if a game server needs multi-threading.
im not familiar with Lua Im just starting, I know it does have some multithreading stuff I think.
5
u/sock_dgram Aug 11 '26
- Possible
- There are probably some networking libraries out there.
- The same as with every other language.
The language isn't really the problem, probably not even performance. It's keeping everything in sync, even with different pings and connection problems.
3
u/GloriusSilver Aug 11 '26
The Luvit framework is a good one. It's basically what Node is to JavaScript but for Lua.
I made a MUD game using it, so I don't see any issues with building a simple game server with it. Multithreading is also supported.
I think there is also server framework called "Swampy" or something like that. It's for Defold game engine though.
2
u/ibisum Aug 11 '26
The way to do a server backend with Lua, is to have it embedded in a high-speed application tuned for networking.
turbo.Lua is one such thing - I have used it quite successfully for backends for a few projects over the years. You might find it intriguing:
https://turbo.readthedocs.io/en/latest/
2
u/jkulczyski Aug 11 '26
Could write it in c with lua bindings
2
u/Lonely-Restaurant986 Aug 11 '26
Could you? Yeah*. But like you could also build a neural network in lua. The question is scope.
A small server is fine, but lua can’t do multithreading natively, and multithreading libraries asfaik are not real multithreading, and it’s just another lua state on another thread, so they can’t and don’t share memory or variables, and require copying and serialization. But really coroutines are probably good enough
Like 99% of cases ur fine, but building a game server that handles thousands of ai’s physics, and concurrent connections with over 500 connections, will probably struggle.
If you are in luajit land, you could use ffi pointers and create a thread and share ffi between threads. This would be the fastest lua option, But at that point ur so close to C, and wrangling ffi to the point where you might as well write C, or a better language.
So like yes, but no if your application is performance sensitive, but even then probably yes.
So..it depends.
Idk any libraries which let you easily interact with sockets, but I bet you luarocks has at least like 20.
Or you could, like I said, use ffi and just use actual C libraries. But it would mean basically writing c.