0 votes

I need to add a trade system in my game so that two players can be in a room and trade their cards. But I've searched everywhere and I can't find a simple way to do it, I've just seen a lot of 3D LAN tutorials...

What I would like :

  • both players have a button to join the trade room

  • server max peers = 2 so if a third player plays and two players are already in that trade room, he won't see a "go to trade room" button

  • when a trade is done the server shuts down and everyone can see the "go to trade room" button again

asked Jul 13 in Engine by ZacB (168 points)

1 Answer

+2 votes

You can use the high-level multiplayer API of Godot, which is explained here: http://docs.godotengine.org/en/3.0/tutorials/networking/high_level_multiplayer.html
I would suggest you get familiar with this first.

This will require one of your players to host the trade room, and the other to use the IP address to connect. But you can also make a separate trade server and have both players enter the IP of that server.

If you don't want them to enter any IP address, then you will have to either hardcode it and make sure the server always run under the same IP, or have an intermediate server (using HTTP for example) which provides the list of available trade servers.

In the case of not entering any IP in a LAN context, you could use network discovery to search for trade servers in your local network (aka an instance of your game that opened a trade peer). I don't know how that works in detail, but it was mentionned here: https://github.com/godotengine/godot/issues/13947

answered Jul 13 by Zylann (14,838 points)

Thx for your answer. I need to use a LAN system and the first player pressing the connect button would create the trade room. But I've two problems :
How can I check if a server is already running so the second player can join it ?
And is there a way to close the server when a trade is done ?

You know if a server runs if it responds to your connection/discovery broadcast. You can never assume a server is always running (even if it should ideally).

Closing a server when the trade is gone is relative. I'd say, your clients have to leave it first, because if the server closes first your clients won't be gracefully kicked out (depends how you code this, though). Also, if you use a dedicated server or that, it could still be running all the time and handle trades by "sessions". It's up to you to decide which flow you want.

I'm new to networking in Godot as well, so I mostly know theory, sorry if I can't share actual code^^

So what are the codes to :
- check if a server is running
- leave the server

?

(sry I'm lost with network stuff :))

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.