0 votes

Hi all,

I have JSON data in a dictionary called dict. I want to draw characters by calling the below fuction with something like drawCharacter(4).

func drawCharacter(id):
    get_node("LabelCharacterName").set_text(dict["4"]["name"])

The problem is I can't do the above as if I try and use id in the string like below:

func drawCharacter(id):
    get_node("LabelCharacterName").set_text(dict[id]["name"])

... it doesn't work and returns an error that it's an invalid index.

I can ONLY get it to work if it's wrapped in " like in the first example.

How can I make this work? I'm currently stuck. Any help very much appreciated...

in Engine by (810 points)

if you call it as drawCharacter(4) then the id is an integer and not a string... try replacing the 4 with "4" so it will be a string and not an integer

Hey there, thanks for your answer. Unfortunately the 4 in my example was just to show what might be sent. It really needs to be whatever is in id. Therefore it has to be a dynamically changing variable and not hard coded.

1 Answer

0 votes
Best answer

Use str() to convert id to string:

func drawCharacter(id):
  get_node("LabelCharacterName").set_text(dict[str(id)]["name"])
by (69 points)
selected by

OK I'm at a loss. First of all, thank you so much. I tried that last night you see. It failed. That was the main reason I asked here. "Why can't I convert to a string?!" I was thinking.

So then I try again this morning before replying and it ... it worked. I must have been too tired and had a typo. Thanks so much. You solved the issue.

Thank you so much dude!

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.

Categories