2shady4u / godot-sqlite

GDExtension wrapper for SQLite (Godot 4.x+)
MIT License
850 stars 76 forks source link
gamedev godot godot-addon godot-plugin godotengine sqlite

NOTE: This branch is compatible with Godot 4.x. Older versions of Godot (3.x) are supported on the godot-3.x-branch as found here.

Godot SQLite banner

godot-sqlite

This GDNative script aims to serve as a custom wrapper that makes SQLite3 available in Godot 4.0+. Additionally, it does not require any additional compilation or mucking about with build scripts.

Supported operating systems:

How to install?

Re-building Godot from scratch is NOT required, the proper way of installing this plugin is to either install it through the Asset Library or to just manually download the build files yourself.

Godot Asset Library

Godot-SQLite is available through the official Godot Asset Library, and can be installed in the following way:

Manually

It's also possible to manually download the build files found in the releases tab, extract them on your system and run the supplied demo-project. Make sure that Godot is correctly loading the gdsqlite.gdextension-resource and that it is available in the res://-environment.

An example project, named "demo", can also be downloaded from the releases tab.

How to use?

Examples of possible usage can be found in the supplied demo-project as downloadable in the releases tab or the source code can be directly inspected here.

Additionally, a video tutorial by Mitch McCollum (finepointcgi) is available here:

Variables

Functions

Frequently Asked Questions (FAQ)

1. My query fails and returns syntax errors, what should I do?

There are a couple of things you can do before panicking, namely:

After exhausting these options, please open an issue that describes the error in proper detail.

2. When should I create function bindings to augment SQLite's set of native functions?

Preferably never.

Creating function should only be seen as a measure of last resort and only be used when you perfectly know what you are doing. Be sure to first check out the available native list of scalar SQL applications that is already available in SQLite3.

3. My Android/iOS/HTML5 application cannot access the database!

Android does not allow modification of files in the res://-folder, thus blocking the plugin from acquiring a read and write lock on the database-file.

In the case of read-only databases, the database files have to be packaged/exported in the *.pck-file by manually adding the file entry to the include_filter of your export_presets.cfg-file. Additionally the connection has to be opened by explicitly setting the read_only-variable of your connection to True before opening your database.

In the case of a read and write database connection, the most painless solution is to copy the entire database to the user://-folder instead as apps have explicit writing privileges there. If there is a better solution, one that does not involve copying the database to a new location, please do enlighten me.

4. Is this plugin compatible with a Godot Server binary? How to set it up?

Follow the steps described in the Godot documentation as found here.

NOTE: If you are using an older version of Linux on your server machine (with glibc version < 2.31), the plugin crashes due to the compiled version of glibc being too recent. In that case you will have to recompile the Linux plugin binary yourself as Github Actions does not support Ubuntu versions lower than Ubuntu 20.04 LTS.

NOTE: The currently installed version of glibc can be obtained with following command:

ldd --version

How to export?

The exporting strategy is dependent on the nature of your database.

Read-only databases

If your database serves as static data storage i.e. there's no persistent dynamic data that needs to be saved, then your database is a read-only database. For example, a database that contains a table of monster descriptions and data (experience gained on kill, health points) probably never changes during normal gameplay.

In this case, the database can be packaged in the *.pck-file together with all other assets without any hassle.
To enable this behaviour following conditions need to be met:

You can also open databases in read-only mode that are not packaged, albeit under some restrictions such as the fact that the database files have to copied manually to user://-folder on mobile platforms (Android & iOS) and for web builds.

One important additional constraint for read-only databases is that Godot's implementation of file handling does not allow files to opened in a shareable manner. Basically this means that opening a database connection fails whenever other programs have a read lock on the database file e.g. having the file open in SQLiteStudio for editing purposes. However, multiple simultaneous read-only database connections are allowed.

NOTE: The contents of your PCK file can be verified by using externally available tools as found here.

Read and write databases

NOTE: On mobile platforms (Android & iOS) and for web builds, the method discussed here is not possible and the contents of the res://data/-folder have to be copied to the user://-folder in its entirety instead (see FAQ above).

If your database serves as dynamic data storage i.e. there's persistent dynamic data that needs to be saved during gameplay, then your database is a read and write database. For example, a database that contains a table of character levels and experience dynamically changes whenever the player levels up and/or gains experience.

In this case, the database cannot be packaged in the *.pck-file as the contents of this file are static and cannot be dynamically modified during normal operation.

All *.db-files (and *.json-files if you choose not to include them in the *.pck) should preferably be part of the same folder. For example, in the case of the demo-project this is the data/-folder. During export this folder has to be copied in its entirety and placed alongside the executable that is created by Godot's export command line utilities.
To simplify and automate this process, a script with the name export_data.gd can be found in the demo-project and allows to automatically copy the data-folder's contents to the export folder.

The commands to succesfully export a working executable for the demo-project are:

mkdir build
godot -s export_data.gd
godot --export-debug "Windows Desktop" "build/SQLite Demo.exe"

("Windows Desktop" should be replaced by whatever the name of your relevant build template is.)

For the "Windows Desktop" export target this results in following folder/file-structure:

data/test_backup_base64_old.json
data/test_backup_old.json
libgdsqlite.windows.template_release.x86_64.dll
'SQLite Demo.console.exe'
'SQLite Demo.exe'
'SQlite Demo.pck'

How to contribute?

Using GitHub Actions CI/CD

Fork the repository to your GitHub own account by following the recommended procedure.

Afterwards you can push your own code to the master-branch of this forked repository and GitHub Actions will automatically build new versions for all the supported platforms. These binaries then have to be downloaded and placed inside of the addons/godot-sqlite/bin/-folder, as found in your Godot project, alongside any existing binaries.

NOTE: Compiled binaries for the latest development version of godot-sqlite are available here.

Using your own device

First clone the project and initialize the godot-cpp-submodule as such:

git clone https://github.com/2shady4u/godot-sqlite.git
git submodule update --init

Secondly, SCons, Python and a viable C++ compiler have to be installed on your device. On Windows, both Visual Studio Community (2017+) and/or MinGW-w64 can be used to compile the plugin from scratch. More detailed instructions for compiling the plugin on Windows, Linux and other supported platforms can be found here in the Official Godot documentation.

Depending on the wanted target platform, some additional dependencies have to be installed:

Afterwards, the SContruct file found in this repository should be sufficient to build this project's C++ source code for Linux, Mac OS X, Windows, iOS and android with the help of following command:

scons platform=<platform> target_path=<target_path> target_name=libgdsqlite

In the case of both Android and iOS, an additional arch-parameter has to be supplied to specify the CPU architecture. In the case of android, valid values for the architecture are 'arm64' (= default), 'arm32', 'x86_64' and/or 'x86_32', and in the case of iOS, the valid values are 'universal' (= default), 'arm64' and/or 'x86_64'.

Additionally, in the case of Android, the ANDROID_NDK_ROOT-parameter has to be set to the location of the Android NDK installed previously.

For uncertainties regarding compilation & building specifics, please do check out the .github\workflows\*.yml-scripts and the SConstruct-file as found in this repository.

Tutorials for making and extending GDExtension scripts are available here in the Official Godot Documentation.