Persistance Saving with SQL database in Unreal

Proposal

For my Specialist Project I am going to look into Multiplayer in Unreal and how players can interact with each other and how data is transferred from the database to a client. I would like by the end of this be able to allow players to trade with each other and trade some kind of ingame currency. Which is held on a database that the game references every time an exchange takes place

Listed in the slide After this is the areas that with some research are the areas that I would have to look into.

List of goals

  • Sending Data from unreal to server/Database
  • Data Replication SQL
  • Allow Players to interact with each other
  • Trade Currency with each other

First task

My First piece of research is how to get multiple characters into the same game so that they can see each other walking which will then open the door for allowing them to interact with each other.

In this case getting 2 players side by side was pretty easy as unreal handles most of the work for you, This was done locally and not online as I first wanted to understand how data replication works in unreal for example if an action is performed on the client side what happens if the data isn’t sent over to the server and vice versa, both actions led to an unplayable experience and the multiplayer aspect not functioning correctly.

However, the challenge from here is to make a variable called gold and store it individually so each player has their own amount of gold

MYSQL

I have been experimenting with MYSQL plugins in unreal to be able to make queries from unreal to MySQL but at the moment I am running into a few issues as I cannot get the data to send from Unreal to SQL. I found that Unreal cannot handle this with blueprints, so I had to look for some plugins to be able to send data from the engine to SQL.

I am currently using a custom plugin to try and send data from server to engine but cannot get it to work, I think that trying to use plugins is creating more areas of failure as I do not completely understand them and so cannot fix problems that arise. Since they are custom. I was able to import the plugin into my unreal and set it up very similar to the documentation and videos that I found online however it does not work, the logic is the same however the data won’t go from the engine to the server that I am hosting.

I am going to try other methods to see if I can find one that will work. My current goal after this is to send data from the game to the engine.

Setting Up My Own Sever

I Have downloaded MySQL and will be trying to store my database on there through hosting it rather than using third party Websites to host it. My current Assumption Going into this task is that I will have more control over the database since I will be able to open and close it when I want and not have to rely on websites.

I Have Successfully made my own database in MySQL and have data inside it

After doing some research I have found that in order to able to access this data in my database I will have to host it with an API so that I am able to fetch the data from the database.

After some trial and error I have managed to retrieve the data from the database and send it to the link that I am hosting it at. This is a small step but it means that the database is working which I was not able to do before, for example when I was using 3rd party websites to host this information. This type of control was not available to me and I wasn’t able to see what was going on behind the scenes i.e hosting sending etc and couldn’t figure out what was wrong. With this I ran into a few errors along the way for example small things such as the Z Coord not showing, looking at my code I saw that I had written the Variable for the Z coord wrong after fixing it worked fine. I was able to use the error codes and the find the faults to fix them and get it working.

Since I am still new to this I am unsure of how what the next step would be but I am assuming that I would need to send this data into unreal so that It is stored there and read in game.

What the Error Is?

The error in the first picture is what was being returned when I was trying to send the data to be stored in the database. the middle picture shows what the error was which said it was to do with my ID table. The fix was just auto indexing the ID value so that It would create a new ID for every entry added into the database

Can Now Send data from front end to back end to be stored however, it can tell data needs to be send however it cannot send the actual data which is what I am going to fox now

*How I Fixed*

What is a JSON? Json stands for JavaScript orientated Notation, all its used for it to send information from a database to a webpage, in my case I used it to show the characters information, such as the gold X, Y and Z coordinates along with the pid, rather than opening the database every time I want to look at the data, I can open a web page which is a lot more lightweight.

A Database is a store of set of data that is held on a computer, I made a database to hold information that I was going to be using inside my Game engine, such as gold, x, y and z.

SQL is used to manipulate data such as store and edit information in the database and is what I used to store my data.

An API stands for Application programming interface, and it allows applications to communicate with each other. I used an API to be able to allow other applications such as the game engine to edit my data. Not directly but sending data requests in gets and posts in order to tell the API how to handle the data, this then was passed into SQL where my data is being stored and the data is edited.

Have Successfully sent a post request from the game engine to the database meaning that there is a link established between the two of them. However there is a problem, although the game engine and the database have a link and know that there is data being sent over, it is unsure as to what data is actually being sent which is why on the picture you can see that it says 0 0 0 in the X y and Z slot. The data is not specific enough. So my next task will be to make the data more specific.

This print string is supposed to have the data that is being sent to the database inside it. Considering it is empty I realised that there was something wrong with the variables but just do not know what.

After some trial and error I found that It was because of the naming of the variables and them not being defined properly. For some reason the variables name was being uncapitalised which meant that it wasn’t matching up with the other naming properly. Once I changed it, it passed the data into the database just fine.

The Game engine now spawns the players at the location that is in the database.

Once the Player moves around a save function is used to store the location of the player and then input that back into the database which saves it and when the player loads back in it ill pick up from where the player left off.

I would like the user to be able to enter their PID and then it loads in their player data, with their gold and coordinates.

However, there are a few issues with this program right now, let us say I use a PID of 1235, if the PID of 1235 is found in the database everything will run perfectly fine. But if the pid is different for example 1234; it will not be found and fail, which shows that the pid hasn’t been found but it wont create a new entry in the database for the new player under the PID 1234 it will continue editing the PID of 1235s entry. This is the problem that I am working on right now.

For some reason the PID being printed out in the output log is -1, I know that this is wrong as the database updates the information after I have stopped running. Which means that the PID is correct.

I am currently trying to find out why the output is -1 and I am testing different methods to see if I can get the correct output and will be working from there. As I will be able to tell why the wrong output is being shown now. If can find out why the wrong output is shown I can get it to display the correct one and will be able to edit that value to send from and too the database.

I have got the correct PID to be printed out which shows what PID value I have to edit in order to get a different PID to be printed to the Database.

I have tried to change the PID to get a different entry into the database however the issue I was then having was that It wasn’t sending the data of the correct PID and was repeating data similar to what the issue was before which was the same data was being sent to the database because I was using the POST command and to overwrite data I have to use PUT. However the main problem im having isn’t with the code but with the logic of my code, since it can only do what I am wanted it to do, I want to data to be posted once with the new PID and after that I want it to use the PUT function which will overwrite data rather than send more entries.

I cannot find a solution to this problem as of yet. As I am currently trying to sort out two problems, one being data input for the user to enter their pid which checks it and then runs the necessary function.

User Input

I have made a new widget which allows the user to enter a PID before going onto the main level where the data is being handled. However, another problem that I have ran into is that the code that handles the data editing is being ran while the Login level is open. Which means im getting errors as I have set the PID to be set after the user input.

I have Set up 2 characters to be loaded in when the game starts similar to my first set of testing that I did, however due to the pid system only one character is being passed in, I am still working on different ways to get around this as I cannot stop the code running while the widget screen is open. I think that I will have to skip this step, as the problem seems to be too complex for me to solve as I have tried many different methods to try and get a custom PID passed into the system and loaded for each individual player to use, this is because of the complexity of C++, it requires a lot more knowledge to do simpler things, the main problem for me compared to other programming languages was the header files, since I had never used them before it would take a lot more learning than 3 months to become proficient in using them.

Conclusion

To conclude my research I think that It went well however there are a few areas that I would like to have improved upon but my lack of knowledge about certain things let me down.

What have I learnt?

I have learnt a great deal in this research, for example how to create an API, how to use SQL, how to link a game engine to a database and how to send data back and forth from the game engine to the database.

Firstly going into this project, I had never set up an API before to be able to handle data requests and link to the database. I had a few troubles along the way however there was a quite a lot of documentation on how to get an API up and running. I used Kotlin to run my API which was a

Secondly, I had to learn SQL which was another thing that I had never touched before, initially I was not comfortable in using this and was exploring alternative routes to host my data for example third party websites. This however proved to be very difficult as I didn’t have full control of my database and couldn’t start and stop it whenever I wanted, another reason I decided to host it myself was because I wanted to understand fully how it worked, I wasn’t able to do this with while using a third-party option. However, hosting it myself allowed to me to make custom functions inside MySQL which allowed me to manipulate the data while sending it to the database. such as parsing it before its sent and when its received if the PID is the same to update the info rather than making new entries.

I also learnt how the game engine pulls data, initially I had thought that the database was linked directly to the engine, and I wasn’t making much progress, this was because my thought process was wrong, rather than having engine link to the database you needed a middleman, something to handle the data and send it to the engine, which was the API. The API allowed me to make that link between the two and data to be sent back and forth.

What Went Wrong?

My limited knowledge of C++ held me back and stopped me from fully completing the tasks I had set out too. I had built the foundation of what I wanted to accomplish, which was my game engine being linked with my database. However, the additional steps of allowing different players to login and depending on the PID it would get that players data. In theory I understood how to do it, but this was difficult to translate into C++.

What Went Good?

This Leads me onto my next point, that I understood the fundamentals of Databases and Game engines. This was a topic that before I would try and stay away from as I thought it would be too difficult, now that I have completed it I am no longer afraid of going into databases and would be happy to start another project with this, I also learnt about data replication and how if a task is done on the client it must be replicated on the server so that it the change takes place globally and now only with on the client side.

What I would do differently next time?

I would try and experiment with other game engines, as I said my lack of C++ knowledge held me back and stopped me from completing all my tasks I would try and do this in a different game engine such as Unity, as it uses C# which isn’t as complex as C++ and I think that I would have a little more luck there. As I was still new to databases and APIs I decided to stick with one game engine to not overly confused myself.