How to show data from SQLite in my 2D game

Godot Version

4.2

Question

So far everything worked as intended using the great guide from FinePointCGI:

But instead of printing out the values from a database to the console with “print”, I want to show it in my GUI. Can someone please give me advice for this? (sounds kinda simple but I’m unable)

This is the command to print it to console:

print(database.select_rows(“players”,“score > 6”, [“*”]))

So I guess instead of “print” I need to use something else? Thanks in advance!

If you have a Label added, you can simply get a reference to your label, then set it’s text property, like so:

label.text = YourTextFromSQLite

I recommend checking out the first 2d game tutorial, since it teaches you a lot of the basics such as this.

4 Likes

Thanks but instructions unclear :smiley:

I already started with the 2D game tutorial but it doesn’t suit my needs at all. My game is purely text-based (football simulation) and everything works so far. I just need to find out how to show values from the DB on the game screen instead of the console.

But I’ll look into the documentation again, however I am not sure if there is a database involved at some point…

You are overthinking this. Assigning a value to a UI element has nothing to do with databases. The database is just the place where you get the value from.

You know what this code does, right?

label.text = "my text"

Now your label shows “my text”.

It’s the same as this:

var label_text = "my text"
label.text = label_text

Now what you have to figure out is how to get the text from your database into the variable label_text. That’s basically it.

3 Likes

Ah so the first time I told you the solution it’s “unclear” but as soon as someone else repeats it it’s suddenly the solution? Right..

3 Likes

If you are talking about me: I did not repeat what you said and just assigning something to a property was clearly not the issue the OP had.

I’d recommend taking the time to do the tutorial even if you’re just making a text game because it teaches you how to use Godot as an editor. You wouldn’t have needed to ask this question at all if you’d actually spent an hour completing it.

I only say this to try and help you, because based on your question, and requiring two people to explain the same thing to you, this is not going to be the last thing you struggle with that would have been explained if you’d done the tutorial.

2 Likes

I wasn’t talking about you, but I’m sorry nevertheless.

Wow I heard about how “great” the godot community is for newbies and I am grateful for the answers, but looking at how you communicate to eachother I don’t know where that was coming from. As I said, I am back to the documentation / “your first 2D game”.

One last thing: @tibaverus : I guess you had a bad day. While you may have showed the solution, the post from Toxe was (for me) in more detail and easier to understand and unfortunately I cannot mark two posts as a “solution”. Perhaps I shouldn’t mark any posts as solution in the future to avoid hurt pride?

Then lemme show you more info on displaying text in labels.

1.you can use str() to convert any int or float to string! soo displaying scores will be much more easy!

var score:int = 67

score_label.text = str(score)

and I think @Toxe didn’t explained well for your case.

you have to do it like this:

Label.text = SqlData.PlayerScore #or whatever

you have to get the property of the object or class.

I think it should be easy if you look into the doc of that SQL plugin : D

You can check this video out for more clear understanding:

Have a nice day : )

2 Likes