I have an array containing nodes as well as strings. While passing it through a loop, I want to call certain functions depending on the type of object being read. I tried to do the following to test the type -
if to_fry_array[fry_looper].is_class("Node2D"):
to_fry_array[fry_looper].show()
else:
print (to_fry_array[fry_looper])
The code throws an error when a string is read in the array to_fry_array
. Error -
Invalid call. Nonexistent function 'is_class' in base 'String'.
How do I modify this code so that it behaves universal to all types of objects/variables?
Not every type has method is_class
, but fortunately you can use operator typeof
to achieve what you need.
if typeof(obj) == TYPE_OBJECT and obj.is_class("Node2D"):
print("Node2D: ", obj)
elif typeof(x) == TYPE_STRING:
print("String: ", obj)
and in your particular case:
if typeof(to_fry_array[fry_looper]) == TYPE_OBJECT and \
to_fry_array[fry_looper].is_class("Node2D"):
to_fry_array[fry_looper].show()
else:
print (to_fry_array[fry_looper])