0 votes

I have a problem with handling/reading unexisting JSON object key(s).

  "emotion": "angry"

Godot script to get the emotion:

var emotion = character["emotion"]

My problem is, some character have this:


It has no emotion, just a name. Obviously, I can't get the emotion from that. But my game crash and outputs this:
"Invalid get index 'emotion' (on base: 'Dictionary').".

I would like to try and catch the error and just ignore it.

Or atleast do this:

if(character["emotion"] == null):
  var emotion = null #or "noEmotion"
  print('character["emotion"] is empty!')

How can I do this? I've found out that Godot Script doesn't support try catch?

in Engine by (101 points)
edited by

1 Answer

+5 votes
Best answer
if character.has("emotion"):
    # do something with "emotion"
    # do else
by (9,786 points)
selected by
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 Frequently asked questions and 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 [email protected] with your username.