0 votes

Is there a way to print the content of a dictionary in plain text excluding the json format?

print(JSON.print(dictionary, "\t")) will return:

{
    "Item1": 4,
    "Item2": 3
}

print(dictionary) will return:

{Item1:4,Item2:3}

I'd like to have it formatted like this:

Item1 : 4
Item2 : 3

The result will be used in a label to list all the content of the dictionary.

This code works for the console, but not for the label, as it only shows the last key:value pair of the dictionary:

for i in dictionary:
    print("%s : %d" % [i, dictionary[i]]) 

So I'm struggling to translate this to a label

in Engine by (30 points)

1 Answer

+1 vote
Best answer

Here is the code:

func _ready():
    var test:Dictionary = {"Item1": 4, "Item2": 3}

    for i in test:
        $Label.text += "%s : %d" % [i, test[i]]
        if (i != test.keys().back()):
            $Label.text += "\n"
by (594 points)
selected by

That made the trick. Thanks!

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.