After discussing some problems with users on Discord about Nakama and some of its uses, I wanted to convert my old warbattlesmp which works fine with my Swampy server, to work with the Nakama server.
I did try this about three years ago, but there were various problem trying to achieve certain capibilites for realtime mp networking.
I have currently converted a fair portion of the code across, and its taken alot longer than I originally thought it would, and mostly because I think Nakama is more suited to event styled networking and not really ideal for realtime mp (note: this is based on the free github server version).
Some of the key problems encountered:
- Connecting to named matches directly (without matchmaker). This seems like a simple thing, but people have asked on the Nakama forums it, but it isnt implemented. So I made a rpc that does it.
- The match_loop is heavy if you have too many ticks, and you will get issues (timing, sync etc) if you run it too hard (this is mentioned in the docs). This limits what you can do in this loop, but its really the only place you can do realtime updates on client input (I got around this too).
- Modules are not really modules in Nakama. It looks like if you init a match which uses a module, that module cannot be loaded into another RPC and the state used. These are not the same env. I understand this, but it limits lua’s use heavily. However there are undocumented feature that can help (detail below)
- Documentation. Is very inconsistent. And there are not enough sample to really show how to use the Server and Client side with something like a realtime game.
While the above are not completely disastrous, if you dont know how to get around them you will get frustrated.
Some helpers:
nk.localcache_put( "MyCacheObject", MyCacheObject, TTL or 0)
This can store a table in the local cache for a certain amount of time (0 is infinite)
local mycacheobj = nk.localcache_get( "MyCacheObject" )
This fetches the table from the local cache.
Use RPC where you need to get around things, in combo with the above methods. You can do quite alot with this. It is more management of your own state, but imho, thats kind of better anyway.
The current repo Im working on is here:
I will attempt to document things as much as time allows. Its extremely prototype atm, but it works in terms of client sending realtime data to the server and the server state updating and handling it.
I hope to have this working decently by this weekend, but I said that last week. I have gotten past the main problems with the modules (which was driving me nuts) and I really hope this will be able to work as a demo for future reference or just something to point and laugh at the code
NOTE/WARNING: Make sure you remove the last line in main.lua which authenticates a fixed ID for a device - Im using this in a ‘hacky’ way to auth devices manually.
For those wanting to look at SWAMPY its here (currently going through some upgrades):