Is there a way to check if the type is an array of an array

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By swordofbling

In my code, I am cycling through an array comparing the current one with the next one but sometimes there is an array within that array so it comes up with an error saying I can’t compare an integer with an array so I used this typeof(tile_positions[timeNo + 1]) != TYPE_ARRAY so it does not compare if it is not an array but this is apparently always true so it ends up never comparing, so is there a TYPE_ARRAY_ARRAY or something else I could use to achieve the same result.

Do you learn about Arrays this

MiltonVines | 2020-11-26 08:49

:bust_in_silhouette: Reply From: jgodfrey

While you don’t show much code, this works as expected for me:

func _ready():
	var abc = [1, 2, 3, 4, ["a", "b"]]
  • 1st print returns 19 for the entire array (TYPE_ARRAY)
  • 2nd print returns 2 for the array value 3 (TYPE_INT)
  • 3rd print returns 19 for the subarray value (TYPE_ARRAY)

Does that point you in the right direction?

If not, please post more code (including the array contents and the code that processes it).

Thanks alot I just replaced TYPE_ARRAY with 19 and that worked

swordofbling | 2020-11-25 17:32

That sounds really suspicious. TYPE_ARRAY is just an enum representation of the value 19. So, they should be absolutely interchangeable. And, with that in mind, I’d highly recommend that you use TYPE_ARRAY instead of the value 19.

So called magic numbers are never good to have floating around in code. If you look at that code 6 months from now, you’ll probably ask yourself what 19 means. However, if you instead see TYPE_ARRAY there, it’ll probably make sense immediately.

Really, I think you must have done more than changed the TYPE_ARRAY reference to 19. Whatever those other changes were must have been the real fix. Otherwise, it doesn’t make sense to me.

Regardless, I’m glad you have it working now.

jgodfrey | 2020-11-25 18:04

That’s odd because whenever I did print(typeof(tile_positions[timeNo]) == TYPE_ARRAY) it always returned true even on the integers sections of the array that’s why it didn’t work but when I switched it to 19 in the code it started to work so I’m thinking it got confused somehow in what it was trying to check.

swordofbling | 2020-11-26 09:44