Download

Client / Server connections

Hi, I’m trying to have my client connect to my dedicated server from the main menu and exchange messages (for example list of available characters in an MMO). I tried many ways but it seems I cannot find an easy way to do so.

What I understood is that the client first of all needs to be connected to the server, and to do this the open command executes some code like this:

	WorldContext.PendingNetGame = NewObject<UPendingNetGame>();
	WorldContext.PendingNetGame->Initialize(URL);
	WorldContext.PendingNetGame->InitNetDriver();

taken from UnrealEngine.cpp:8160

I tried to recreate this into my GameMode, when the player clicks on the “login” button, but seems I cannot find a way to provide a proper worldcontext from there.

I used GEngine->GetWorldContexts() but this is returning a const set of objects, I cannot modify, so I cannot apply to that the code above. Does anyone know the way to have the client connect to the server by code? I would suggest to put this in an example, as I think it’s a pretty common question.

(my next question will be how to send a message)

Thanks.

User console command to connect to server:



if(APlayerController* PC = GetWorld()->GetFirstPlayerController()) //You can obtain player controller in other way
{
      PC->ConsoleCommand("open 127.0.0.1"); //Specify the address of the server here
}


Hi Ogniok, thanks for your answer. Using the “open” command works, but doesn’t give me control of what’s happening. In particular the open command is making the player travel to the server map, which is something I don’t want to do in the main menu of the game. In the main menu the player should only select the character he wants to play with. So either there is a way to avoid “open” command make the player travel, or I need another way to connect to the server. That’s why I started the investigations above. The code you see above it’s actually part of the open command, as I want to get only the connection part.

You should have another sever for storing player data, connect to it and then after he wants to enter the game connect to the game server. But if you want a workaround(very bad :D) then you can in example create additional level(let’s call it “Menu”). After the player starts the game you connect him to the server with Menu map that handles logging in, character selection etc. After everything is set you can connect him to a game server. But this is a real bad idea and what you want to achieve should be done differently.

In a MMO the player data is on the main server, I cannot spread it over multiple ones, as I will need this data all the time, and will be hard to replicate it/synchronize it. There must be a way to just connect to the server and exchange messages by code by using the InitNetDriver() function. I just need someone to help me to figure that out or provide an example. Anyone with ideas on this? Thanks.

I’m working on a game that uses some back-end social server features that I am developing. Included with that is that I am only using my own dedicated servers to run the multiplayer games. With this, I am using a custom system where both the clients and the dedicated servers will be connected to a back-end Java server that I also wrote. I’m using TCP Packets, which may be of use to you. You get control on how information is sent through this.

Because the amount of information that I am sending in this is pretty minimal, I’m just sending strings and then having my C++ code in the Unreal Engine parse these strings to get the commands out of them. Things like “login username password” or “chat sender This is the message.” and then I am taking these strings and making events that I fire in Blueprint. It’s not the most elegant system, but it’s working really well for what I need. Something similar may suit you as well.

On the Unreal Engine side, I have based my code on a heavily modified version of Rama’s TCP Socket tutorial: https://wiki.unrealengine.com/TCP_Socket_Listener,Receive_Binary_Data_From_an_IP/Port_Into_UE4,%28Full_Code_Sample%29

Hi Epsilia, thanks for your tips. Developing a separate protocol to communicate is surely a possibility, and in this case we could probably just decide to port the code we have in our old project, but to me seems a pity to develop a parallel networking system, when there is already one in place in Unreal, with UDP, capable of reliable and unreliable messages. I would like to see if someone managed to use the UE networking instead of writing a new one. I hope some UE developer can shed some light on this. Example having a modified version of the “open” command which does not force the player to travel to a specific map will already be a good starting point to achieve a simple connection, then extending the messaging system will allow custom communication. If anyone has examples will be good. I will dedicate more time to this during the weekend and see if I can come up with a solution.

Has anyone additional tips/examples on this topic? We are still struggling to put to a good use the Unreal network layer without examples.

If you don’t want to travel to another map but do some data exchange, you can setup a Server & Client Beacon (it’s a feature of UE).

By doing this, you can get information from the server without traveling to it. It is use by example in UT4 for getting the current match status and who is playing etc…

here is the link

I found a variant of GetWorldContext function, UEngine::GetWorldContextFromWorld http://api.unrealengine.com/INT/API/Runtime/Engine/Engine/UEngine/GetWorldContextFromWorld/1/index.html