Invalid get index 'emotion' (on base: 'Dictionary'). (Catching exception?)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Qws
:warning: Old Version Published before Godot 3 was released.

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?

:bust_in_silhouette: Reply From: volzhs
if character.has("emotion"):
    # do something with "emotion"
    # do else