How to send table using TCP?

Hello,
I’m triyng to make multiplayer game and I need to send tables from client to server during game.
I’m using this code:


It’s working great with normal variable, but with tables no.
I just need the name of the object in the first place, then the name of the data (position, rotation) and then the data itself.
Does anybody know how to do that?
Thank you for every answer.

I think you need to encode the table into a JSON string and send it, and decode when received

3 Likes

Exactly. You send bytes over a socket and it is up to you to decide how you want to convert/encode your table to bytes. Using JSON is a quick solution, but for high volumes of data you need a more compact format.

3 Likes

Thank you very much, in the end I will probably do it by sending the variables in one sequence.
But I have one more problem, when I run the tcp server and client from DefNet on one computer, everything works great, but when I try to do the same with a computer on a remote network, it doesn’t work, you don’t know when the problem is?
Thank you so much for any help.


And would anyone know where to find this tutorial? It could help me a lot, but I can’t find it anywhere and the link is broken.

You can’t just connect to another computer.
There are some difficulties with public IP and blocked ports.
The solution might be to use a dedicated server and connect to it
You can also try port forwarding and find out the public IP address to try to connect. But I don’t think it will be a good solution for anyone who wants to create their own room in the game. Also, it may not always be available (for example, from my provider)

It would be cool if you could write what kind of multiplayer in your game. Do players create separate rooms and play them, or are there dedicated servers that players connect to?
Do you need local and global multiplayer in the game? I mean, perhaps, in your case, Nakama or Coliseus may be more suitable

1 Like

Thank you very much for the answer @Denis_Makhortov, in the game all you have to do is create your own rooms and connect to them, for example using code.

One more thing comes to mind, I have the option to take my own server from someone, is there any way I can easily configure it and connect it to Defold to make global multiplayer?

Defnet does not support creating rooms. You can write this yourself, but I advise:

  1. Use Nakama or Coliseus
  2. Check the work of finding rooms, their connection and much more on the local computer
  3. Rent a dedicated server (or VPS, I don’t know what the server load will be. Try some cheap one for a test) and run the server on it. Next, players will connect to the server IP address and will create their own rooms, play, etc.
2 Likes

Thank you very much, and would it be possible to use your own server instead of the leased one?

If you can connect and interact, then there is no difference. The rented ones work 24/7 and you can connect to them 100% without any problems.
You can try it on your PC.
The main thing is that the server works and you can connect to it.

1 Like

Thank you very much, I see that you are really good at it, unfortunately I do not have the opportunity to rent the server, I am only 15 years old and I have no way to pay for it. My own server is my only option, at least for starters, I could just let it run for a few hours a day. Don’t just know where to start? Would I be enough with the Nakama library, for example? And is it even possible to handle it when I’m an absolute beginner when working with servers (as a programmer I’m already quite proficient)?
And thank you very much again for your answers.

Test everything on your computer, and when everything is ready, then rent a cheap VPS
If you want the server to be on your PC, I think you will need set up the network so that others can connect to you outside the local network (I wrote about the difficulties above)

I have never worked with Nakama. I am afraid that I cannot answer this question in detail. But I think yes, that’s enough
I think you need to start by reading the documentation :slight_smile:

I believe that there are cool developers here who have worked with this library and will be able to write in more detail
You can create a new topic with a title associated with Nakama for them to notice and help. Or if any of them read this, please respond :slight_smile:

1 Like

Thank you all again, I’m trying to work with Colyseus, so I’m installing npm, but when I try to enter npm start in the console, it writes this error:


So I should probably create a script called start, but where should I place it so that I can run the Tic Tac Toe project with its help, for example?
Thank you very much in advance for every answer.

Hi! I haven’t used the Colyseus package, but in Node.js world the npm start command runs the .js script you’ve specified as the start script in your package.json file. If you haven’t specified a start script in package.json then npm start is the equivalent of running the command node server.js

So, if you don’t have a file named server.js or package.json in C:/Users/karel then that’s probably why this is erroring :slight_smile:

(Also, npm install doesn’t actually install npm – it downloads and installs dependency packages that you can specify in your package.json file or at the command prompt. It seems like you might have initialized a npm project in the root of your user directory, which probably isn’t ideal. It does look like you’ve installed the npm tool successfully though!)

It sounds like you might need to install/initialize Colyseus now that you’ve got npm installed. The Quickstart section on the Colyseus GitHub repo might give an idea about this. I’d recommend following a couple Node.js tutorials on YouTube if you haven’t yet to get familiar with how node projects are structured.

3 Likes