Introduction to the ARMA 3 extDB3

in #arma37 years ago

sxtDB3.png

This post combines two hobbies of mine: Gaming and Programming. I am going to give an introduction to the ARMA 3 Addon extDB3.

What is extDB3?

It is a server side addon, that enables ARMA to connect to a Database (currently MariaDB/MYSQL). The main purpose is for persistent missions to be able to save and load data from an external source. Just to give some examples, the database can simply serve as an persistent data storage between restarts of the ARMA server or the database is altered from the outside and the game can interact with the Internet in this way (you could read/update information via a website or Android App).

How to install?

First you need a database server to connect to. It can be installed at the same computer but doesn´t need to. Just make sure ARMA can actually reach the database server. Now create a new database and configure it as you want. Next you need to download extDB3 from its developer homepage. You need to unzip the downloaded file, unpack it and move it to the main game folder. Then start the ARMA server with the Mod (simply pass it an additional parameter "-mod=@extDB3"). Inside the ARMA/@extDB3 folder you find a extdb3-conf.ini which contains the settings.

...
[Database]
IP = 127.0.0.1
Port = 3306
Username = changeme
Password =  changeme
Database = changeme

You have to edit this file and set at least: the username, password and Database. If you the database is not at the same computer as the ARMA 3 server, put also in the IP and port. I recommend to not use the root login for security reasons. If you can, you should create an own ARMA user in the database server.

How to use it?

I am not going to explain how to create a mission. I am going to focus on the part with the database. In the mission init.sqf file, which is running at the ARMA server start, you need to put in some lines to connect to the database and configure it for later use. In SQF, the ARMA scripting language, you use

param1 callExtension param2;

to interact with any addon. param1 is a string containing the name of the addon: "extDB3" and param2 contains some parameter, that gets passed to the addon. First you need to introduce a new database with:

_result = "extDB3" callExtension "9:ADD_DATABASE:Database";

_result can be used to check for an error:

if(!(_result isEqualTo "[1]")) exitWith {diag_log "extDB3: Error with Database Connection";};

Now let´s have a closer look at the second argument of callExtension. It consists of three parts separated by colon. The first part is simply a 9, representing system mode (more explanation later). The second part is ADD_DATABSE, which is a command telling extDB3 to connect to a new database. The last part is Database and relates to the section in the extdb3-conf.ini where you inserted your database login. It tells extBD3 to open a connection to the server specified in that section. You could add more such sections in extdb3-conf.ini and switch between them in the SQF code. Only make sure not to use the same name multiple times (example).
The next step is to define the SQL Protocol. In order to request information from or insert data into the database you need to know at least some basic SQL. In order to define such a protocol use (and catch any potential errors):

_result = "extDB3" callExtension "9:ADD_DATABASE_PROTOCOL:Database:SQL:<PROTOCAL_NAME> ";
if(!(_result isEqualTo "[1]")) exitWith {diag_log "extDB3: Error with Database Connection";};

The argument is, like before, separated by colon. The first part is 9 again, referring to system mode. ADD_DATABASE_PROTOCOL is also pretty self explanatory. Database specifies the database you want to use the protocol for - exactly in the same way as before. SQL means you are going to send SQL statements (the more advanced alternative is SQL_CUSTOM). And the last part can be chosen freely. You will need it later to send requests to the database. Additionally extDB3 offers some features for simpler communication. You can add another section to the end to define how Text Datatypes and NULL objects are handled. There are three different possibilities:

  1. :TEXT   wraps all text datatypes in double quotes "
  2. :TEXT2   wraps all text datatypes in single quotes '
  3. :NULL   Converts NULL value to objNull

You can also combine 1. or 2. with 3. in this way:
:TEXT-NULL

If you don´t need this feature or if you have no idea what I am talking about, don´t worry and just continue.
Now the setup is complete and you can use the database anywhere in your server mission file. There are two different use cases. First if you want to update/insert or alter data:

_ret = "extDB3" callExtension "1:<PROTOCAL_NAME>:<SQL_STATEMENT>";

Now let´s talk again about the system mode. It basically tells extDB3 what you want to do. The 1 at the beginning of the argument means, extDB3 is not going to save the answer, this ** <SQL_STATEMENT>** is going to return. If you use this mode for data requests, you are not going to get an answer. The <PROTOCOL_NAME> is the name you defined previously and the <SQL_STATEMENT> should be a valid SQL query. If you want to retrieve data use mode 0 like this:

_ret = "extDB3" callExtension "0:<PROTOCAL_NAME>:<SQL_STATEMENT>";

The arguments are just like before except this time the first number is a 0. That means extDB3 is going to send the SQL query to the database and sends the answer back to. The _ret variable contains an array, with one or two elements. The first element is always an integer value and the second a string. If there was an error you will get 0. If everything was fine, you get 1 and the string comprises the requested data like this:

[1,"message_data"]
[0, "Generic Msg"]

Is that all?

No, extDB3 offers much more. If you are interested take a look at their homepage or leave a comment below. If anyone is interested I can do second post about more advanced stuff. There are so many possibilities you can use databases for, please let me know what you build with it.

Sort:  

Great write up. I always notice extDB3 on the logins for Arma3 RP servers and wondered what it was for