How would I go about making a multiplayer game?

I’m trying the Defold engine and want to make a multiplayer survival game in it, but so far I have found no existing support for peer to peer network communication. What would be the best way to communicate between two clients in Defold?

1 Like

Defold has support for LuaSocket. The socket “object” can be used to bind a socket, send and receive data:

local s = socket.bind(host, port)
local con, err = s.accept()
local data, err = con:receive()
con:send("foobar")

There’s more to it than the above. By default the accept() and receive() calls are blocking. You want to use the settimeout(0) on both and poll once per frame for connections and data. Check the LuaSocket documentation for details.

Just be aware that without LuaSec/OpenSSL, the LuaSocket network traffic is not secure and doesn’t support TLS.

Might not be an issue for your game, however it is a show stopper for me.

This doesn’t seem very accessible for someone who isn’t an experienced coder. Are you planning on expanding your builtin libraries with support for this?

I don’t see how to make this much easier without making a lot of design decisions that some may agree with and others might now. Doing online games is complex. Doing peer to peer multiplayer games is complex. I think it’s important to have full control over the implementation, but I guess it might be possible to wrap the socket code into something a little bit easier to work with. What are your suggestion for making this more accessible to a novice coder?

1 Like

A thought would be wrapping the socket communication in something similar to the current messaging system so you could do msg.post and receive messages to and from other devices

5 Likes

I think you both said it. Wrapping it in something that seems intuitive and in line with the current systems would be nice. And of course, documentation and examples for setting up a connection between two clients as simple as possible.

I’m convinced that there are ways of making this accessible without compromising on the amount of control you have over your implementation. After all, any decision concerning the framework of the engine is a design decision that people may or may not agree with, and so far you have succeeded in striking a good balance between accessibility and user control.

I feel I might be confused as to how much of an experienced programmer you have to be to use the engine because of how accessible and easy to understand it is in other areas. I would understand if you don’t want to cater to people who aren’t experienced network coders, but that would alienate many of those that the engine seems to be directed towards, including myself and Fluffy.

2 Likes

I think this is something that I would like to give as a broader feedback for the engine.
I’m a designer making games on my own, and Defold is by far the best engine I’ve worked with when it comes to making 2D-games. Having developed very different kinds of games, I feel it’s flexible enough to almost anything you can imagine, but some things that are essential in many games, seem hopelessly out of reach.

Coming from a design perspective, the biggest strength with the engine in my opinion is the balance it strikes between control and accessibility. The message passing system is easy to understand and provides a good framework without compromising on control.

From that same perspective, the biggest weakness is that it feels uneven in what it expects from you. After a couple of years of using it, I have at times felt like development seems hopeless, usually when pointed towards some monolithic external documentation like GLSL or LuaSocket.

Even though a subject is very complicated - and it can be seen as the users responsibility to acquire the knowledge before even approaching your engine - I feel there are many things that can be done to make it accessible to people who would otherwise just give up making their game.

Simple examples and Defold-side documentation helps a lot, especially when paired with built-in functions. You don’t even necessarily have to simplify the mechanics of a thing, you just have to de-obscurify it.
Network is something that a lot of people want to attempt, and if you can make it accessible to scripter-level developers, I think you have tons to gain.

2 Likes

I spent some time yesterday working with the socket module and I might be able to share a Lua module that simplifies working with sockets. I just need to check with some people internally that I can share the code.

1 Like

That sounds great! I’d be happy to take a look, if you can share it that is:)

To be honest I haven’t looked any deeper into coding network with LuaSocket. My perspective stems mostly from engines like Unreal or Unity where the engine and tools documentation + tutorials cover all that stuff and they have function libraries for common-use stuff if not only to give newcomers an idea of how it is supposed to be approached, never leading users into external documentation. Coming from a gamplay-scripter’s perspective that is immensely empowering and I would love for Defold to get to that level of accessibility.

Sorry for the wall-of-texts. I guess I really care about where Defold is going because it is, as I said previously, the best engine I’ve used and I have high hopes for its future. You guys are doing a great job!

Thanks for being so responsive here in the forums!

3 Likes

Thanks for the kind word, and for the feedback. Our goal is to make this engine great in all respects so we need definitely take a closer look at this.

2 Likes

Maybe a REST API layer and a websockets layer?

1 Like

hi guys,

I’m trying to make a multiplayer game with defold.

LuaSocket works well with nodejs websocket server?
What about the performance of defold sockets?

3 Likes

Yay! We need more of those!

LuaSocket can do TCP and UDP. WebSockets is using TCP with some additional handshakes etc. When you bundle for HTML5 the underlying transpiled engine code (using Emscripten) will emulate sockets using WebSockets. If you want to communicate with a nodejs websocket server you need to add a layer on top of LuaSocket to take care of the websocket handshake. This is doable but not trivial. I’ve tried to wrap this up into something reusable here.

The socket code is written in C so it’s fast. The actual job of reading and writing on a socket is done in Lua though, but it’s not really an issue. I know that @andreas.strangequest is working on a multiplayer game in Defold and he has as far as I know no problems with performance.

4 Likes

I can confirm that we have had no problem with any of the steps involved in multiplayer networking so far. As for now, I’ve created a network solution that is working stable with very little performance hit or lag. This includes packaging, serializing messages, sending via udp, ftp and our own custom order-reliable protocol. I havent fiddled with nodejs websocket though.

Our tech is both server and client built and run with defold (headless dmengine serverside). If you have any question, feel free to ask.

5 Likes