MySQL Integration for UE4

MySQL Integration is a plugin that lets you connect your UE4 project to your MySQL server, and store and retrieve data from the server via SQL queries, directly from the Blueprint. This plugin brings the power of C#.NET in Unreal Engine 4.

If you have worked with SQL, you must be very familiar to Select, Update, Insert and Delete SQL queries, joins and sub-queries. If you know C#.Net, and worked with ADO.Net, you must be knowing how to set Connection properties for your Server, and how to write and execute your SQL Queries within your .NET applications.

But now you can use Blueprints in Unreal Engine 4 to do your job. You are still writing the same queries that you are familiar with, but here you are writing it within Blueprints, which gives you flexibility to store and retrieve data as well as Images from your MySQL Server to your UE4 projects.

Get It Here

Features


Asynchronous Connection

Asynchronously connect your Server via MySQL Server Authentication System.

Easy Query Formation

Form your own query and pass as string input , directly within your Blueprint or C++ class.

Custom ConnectionString

Create custom connection string format with different protocols and other connection parameters, as required by your server.

Getting Started


To get started, create a C++ based project, download the plugin from the marketplace, and ensure that the plugin is installed, by visiting the Plugin Window, as shown below:

MySQLPluginWindow

If you are unsure regarding how to work with MySQL, you can visit this link to get yourself started with MySQL. https://dev.mysql.com/doc/mysql-getting-started/en/

Setting Up Database Connection in UE4

Before we begin writing our SQL queries and executing them in our database, we first need to set up a connection within UE4. This is similar to how we manually connect to our DB Server using Windows or MySQL Server Authentication.

Authentication Modes

To set up a DB Connection, we either need to use Windows Authentication mode or MySQL Authentication mode.

  • Windows Authentication mode authenticates users to use the Database server via Windows Login ID and Password.

    Function Name Description
    Get Current User This function returns the current Windows User Name.
    Get Current Domain This function returns the current PC Domain.
    Authenticate User This function takes the current user name, domain and password for your Windows account as input and returns the authentication result as a Boolean variable. Please note that this function will not work if the current Windows user does not require any password to login. So you may want to avoid using this function unless absolutely necessary.
  • MySQL Authentication mode uses logins that are created and stored in MySQL Server and are not based on Windows user accounts. Here you need to explicitly enter both your MySQL Server ID and password, while setting up the connection property. To learn about Authentication modes in detail, you can refer here : https://dev.mysql.com/doc/refman/5.5/en/authentication-plugins.html

MySQL Server Connection

Connection to the MySQL Server happens asynchronously. To connect to your server, first you need to call the Set Connection Properties function, and then add a custom tick event that you need to listen to at every tick to validate the current status of your connection

  • The function Set Connection Properties takes the necessary parameters that your server is going to need , and request for the connection to be established.

    Inputs Description
    Server This parameter takes your server address.
    DBName This parameter takes the name of your database.
    UserID This parameter takes your login ID.
    Password This parameter takes your login Password.
    Extra Param This parameter takes any extra setting that you might want to add. All individual settings should be seperated by semicolon and passed as a single string.
    Is Trusted If you are using Windows Authentication Mode , you can check this, and you do not need to enter your User ID and password in that case. Otherwise, uncheck this paramter.

    SetConnectionProperties

    A connection string typically looks like:

    Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

    So if your connectionstring consists of extra parameters , apart from the usual parameters included in the Set Connection Properties function(Server , DBName , UserID , Password , IsTrusted) , you can enter those extra parameters in the Extra Param input, seperated by a semicolon. For example , if your connectionstring is something like :

    Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;Protocol=pipe;PipeName=mypipename;

    Then you need to enter , in your Extra Param input :

    Protocol=pipe;PipeName=mypipename;

    It must be noted here that you can obviously enter the extra parameters in other inputs as well, like in your Server input , you can also write like this :

    myServerAddress;Protocol=pipe;PipeName=mypipename;

    But it is recommended to use Extra Param input for this purpose, just to avoid confusion.

    To learn about various types of Connection String and forming the one that best suits your requirement, you can refer here : https://www.connectionstrings.com/mysql/

  • Once your connection has been established, you need to periodically listen to the function Check Connection State to find out the current state of your connection. An useful way to do that in Blueprints is using Gates, which is called in Tick function , but is opened after Set Connection Properties is called.

    MySQLCheckConnectionState

    Connection State Description
    Broken The connection to the data source is broken.
    Closed The connection is closed.
    Connecting The connection object is connecting to the data source.
    Executing The connection object is executing a command.
    Fetching The connection object is retrieving data.
    Open The connection is open.

    As long as the current state shows Connecting, you can execute other operations, and let the connection happen in the background. When you get current state as Open, this means connection is successfully established , and now you can proceed with executing queries in your database.

Writing SQL queries within UE4 Blueprints

Once the connection property has been set, you can now start forming your SQL queries. Forming queries can be categorized in two types, queries that update data to the database (INSERT, UPDATE, DELETE), or the queries that fetch data from the database (SELECT). We will discuss these two categories one by one.

Update Queries

  • To asynchronously modify data in our table , we will use the function Update data from Query Async.

    MySQLUpdateQueryAsync

  • After calling the Update function , you need to periodically listen to the function Check Query Execution State to find out the current state of your function. Here also you can use Gate within the Tick function, which should be opened after Update data from Query Async is called.

    MySQLUpdateQueryExecution

    Query Execution State Description
    Executing The query is still being executed.
    Success The query has been successfully executed.
    Failed The query execution has failed. This generates error message as string output in the Check Query Execution State function.

If you would like to update data synchronously, you can call the function Update data from Query which takes a string input where we have to enter our query and executes the query in the database.

MySQLUpdateQuery

Select Queries

  • To select data asynchronously, you need to first call the function Select Data from Query Async which takes a string input where we have to enter our query and fetches the result from the database.

    MySQLSelectQueryAsync

  • Once you called Select Data from Query Async, you need to periodically listen to the function Check Query Execution State to find out the current state of your query execution.

  • To select the data once the query execution is successful, call the function Get Selected Table.

    MySQLSelectQueryAsync1

    Output Name Description
    Result By Column

    This will return an array of structured variable called MySQLDataTable. This represents a column in the table and contains two properties, ColumnName and ColumnData.

    ColumnName

    ColumnName contains the header name of the column of the current instance of our initial array that we get by executing our select query. If you want to display data of a specific column, you can use this variable to match the column name.

    ColumnData

    ColumnData is an array of string consisting of all the elements of a given column. So you can easily set up a for-loop here to get all the elements of a particular column iteratively.

    Result By Row

    This contains an array of structured variable called MySQLDataRow. This structure contains an array of string that represents values of an entire row. So the number of elements in this string array equals number of columns, and number of elements in the Result by Row array equals number of rows in the output of the Select query.

Below images display an example of how we can use Result By Row and Result By Column structures to store and display selected data retrieved from the server to our UE4 UI, as part of UMG widget.

ResultByColumnExample

ResultByRowExample

If you would like to select data synchronously, you can call the function Select data from Query which takes a string input where we have to enter our query , executes the query in the database. and returns the selected data as Result By Column and Result By Row format.

SelectDatafromQuery

The End


Thank you for your time. I hope you find this plugin useful. For technical difficulties, feedback, suggestions, queries, kindly drop me a mail at sameek4@gmail.com. I would love to hear from you.