+4 votes

Hey,

I consider to change from unity to godot.
My next project needs a small dedicated server, which settles on a postgreSQL - database (it is my main-database, which I can manage central across all project (dumps, administration,...).
I searched for postgreSQL-Support but the last answer I find was a "no, not yet" from 2018. Is there any update of this answer?
Is it possible to include e.g. Npgsql in a godot-project?

I am sorry for such a "beginner"-question. I read a few comparison between unity and godot and with some experience in my mind (love2d, .NET) I want to switch - but with my favorite database for server :-)

Thanks for reading and I wish you a great sunday.

in Engine by (16 points)

From this i guess you can include c# libraries, so it should work, but i'll let someone who knows for sure answer.

First, I had to install the mono-version for c#-support.
My test-project throw out a build-error, that the mbuild couldn't find the .Net-Framework, but it was installed.
After installing the whole Visual-Studio-Package or editing the csproj I got the same error.
Before giving up for the day I decided to create a brand new godot-project. This one I could build.
So I created the test project with a moving player.
After that I installe VS-code and there I installed the package manager.
With STRG+P and add package I added Npgsql.
VS-Code added the lines:



to my csproj - File.
Now I run dotnet restore in VS-code.
Now I run nuget restore in VS-Code. Sadly, this didn't worked and I can't figure out, why.
I removed the lines in csproj-files.

Now I try to open a test-database and write a line in a table for test-reasons:

public override void _Ready(){
    GD.Print("test");
    _screenSize = GetViewport().Size;
    string cs = "Host=kandula.db.elephantsql.com;Username=hidden_user;Password=hidden_pw;Database=hidden_db";
    con = new NpgsqlConnection(cs);
    try
    {
        con.Open();
        NpgsqlCommand cmd = new NpgsqlCommand("INSERT INTO public.cars (name, price) VALUES (@n, @p)", con);
        cmd.Parameters.AddWithValue("n", "bla");
        cmd.Parameters.AddWithValue("p", 123);
        //cmd.Prepare();
        cmd.ExecuteNonQuery();
    }
    catch
    {
        GD.Print("catch");
        return;
    }}

First, I don't know why it doesn't work with VS-Studio. Also, I don't know how to handle exceptions in godot.
But: Awesome, it works, the values are in the database. I think I give godot and myself a try and look for some basic tutorials.
Thanks for your help!

2 Answers

+2 votes

It works!

I just added the Npgsql Nuget Package to my Project. Here an Code Example:

using System;
using Npgsql;
using Godot;

public class MainScene : Control

{
public override void _Ready()
{
try
{
NpgsqlConnection conn = new NpgsqlConnection("Host=localhost;Username=postgres;Password=postgres;Database=postgres");
conn.Open();
NpgsqlCommand command = new NpgsqlCommand("SELECT * FROM people", conn);
NpgsqlDataReader dataReader = command.ExecuteReader();

        while (dataReader.Read())
        {
            GD.Print(dataReader[0], " ", dataReader[1]);
        }
        conn.Close();
    }
    catch (Exception exception)
    {
        GD.Print(exception);
    }
}

}

by (710 points)
+2 votes
by (22 points)

I was pretty happy to have found this, but then I read the docs that say the client is unstable, and should not be used in production. It also occurred to me that it is likely to become abandonware, and likely incompatible with some future version of PostgreSQL.

I also don't want to go through the pain of trying to integrate Godot and C#, so integrating PostgreSQL into my game code has become a non-starter.

What I decided on was using JSON files as my lingua-franca in a structured file tree, and having a separate monitor program that reads and writes the database. This way, the files provide real-time data to and from the game servers, and the database lag is immaterial.

Hello, I am the developer of the repository, I am 18 years old and I am in my studies so I did not have time to finish the script. Despite this the project is still relevant and I will be able to resume its development.

The project is still only a draft but I did not know that there could be a real interest in the reference. I have the same need as you so I will continue to maintain it and make it cleaner in the future.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.