Question about networking

Hello everyone. I’m trying to write a MMO game, trying to write a connection to the MySQL server through a dedicated UE4, but apparently that is not possible, since I always new mistakes. So I decided to try to divide the logic on two servers: the first server (dedicated server UE4) will synchronize the movement, interaction, and so on, and the second server will interact with the database. Please tell me if I’m doing the right thing? Because of the two servers will not suffer optimization? Can someone tell me how to write the interaction with the database server on a dedicated UE4?

What you want to do instead is write your data access layer using an appropriate system (not UE4). A WebAPI would be a good choice. If you are using mySQL as your database then you may want to look into using PHP to write your WebAPI. If you search on Google you can find thousands of examples showing how to use PHP to build an API to interact with MySQL. Then you can access this WebAPI easily from blueprints in UE4 using the free VaREST plugin. Personally I prefer Microsoft SQL server so I wrote my WebAPI with Microsoft .NET and MVC.

Can I use the PHP server instead of C ++?

PHP is a different language than C++. It is a higher level language so it will be much easier than C++. What you want to download to get started is a combo of Apache, PHP, and mySQL. That is everything you need to get started and you can download it all in one bundle for a wide variety of operating systems.

If you aren’t set on using MySQL and you are developing on a Windows PC you might find IIS, Microsoft. NET, SQL Express to be easier to learn and they are also free. I have developed with both and much prefer the Microsoft toolset. Also, you can use the same Visual Studio Cummunity 2015 that you use for UE4 to build the WebAPI using Microsoft. NET. So you can build all of your games components with one tool.

I just do not want for this to learn PHP and do not want to have PHP server proxy. It turns out I will have such a scheme: client-> dedicated server (UE4) → PHP-> MySQL.
I wanted to use a dedicated server as the core, ie to have such a scheme:
client-> dedicated server (UE4) → MySQL.

You can do that then. It isn’t as flexible, but with C++ anything is possible. When I first started with my database project I directly integrated UE4 with Microsoft SQL before I realized that one dedicated server wasn’t going to be enough and I would need dozens of dedicated UE4 servers.

What you will have to find is an unmanaged C++ library that can interface with MySQL. Then you add that library to your C++ project and then you can connect MySQL directly to UE4.

There are probably many libraries to choose from, but here is an example of one that looks promising:

https://dev.mysql.com/doc/connector-cpp/en/

Keep in mind this will take a pretty good knowledge of C++ programming to get this up and running.

You are correct. PHP is an interpreted language and is not compiled. It will never match the speed of C++. However, C++ is going require significantly more code to create the same interface to MySQL, so there are always trade-offs.

Thank you, I have attached a library to the C ++ project. My question is this: they say that C ++ - the fastest language of OOP. Is PHP equal speed with C ++? After all, he is not a compiled language. I apologize for such silly questions. I am familiar only with C ++ :slight_smile:

Well, I knew what I wanted, thank you :slight_smile: Additional question for off-topic: Tell me please, how can I automatically run what ever function as a server and to run this logic is performed only on the server? GameMode how strange works. It runs only on the server or on the client too?

Yes the GameMode only runs on the server. However, that doesn’t limit you to only using the GameMode for your server side code. In blueprints you can us the Is Auth node to make sure code only runs on the server and in C++ you can use the following:

if (Role == ROLE_Authority)

What you may find is that you make calls to the database from various objects like the Character or the Player Controller. Just make sure you always have a check to be sure database accesses only run from the server side.

My GameMode reason invoked 2 times: when the editor is started and at the beginning of the game. I do not like it, I want to GameMode summoned 1 time. I want to start with the Dedicated Server is attached to my database one time, and the result is 2-3, and maybe more.

Yeah, I had the same issue, that is why I decided not to use the GameMode. Instead I make all my database calls from the Character, PlayerController, and for centrally located database calls, I just placed a custom Actor in the level to function the same as you are using the GameMode.

Yes, I had such an idea, but it is necessary to create in C ++, but not in the editor, otherwise a dedicated server will not know about the existence of this actor. Thank you very much. P.S. I’m sorry for my English, it’s Google translator to blame! : D

I found a solution to the problem: I was trying to establish a connection through the class constructor, as needed through BeginPlay function!

It should still check whether it is a dedicated server. Otherwise, the function will be called when the client is started, if it is not connected to the server.