Im trying to check if something in an array is there or not

Godot Version

4.3

Question

So im trying to figure out if the object in my instance exists or not and than im setting a boolean to true so uh can someone help?

if is_instance_valid(NoteData[i][“k”]):
var KInstance = NoteData[i][“k”]
if KInstance == “Char Sing”:
CharIsAllowed = true

Whats the problem you are experiencing? Are there error messages?

if NoteData[i][“k”]: CharIsAllowed = true

if returns false if null

Would you elaborate a little more? If I understand your code correctly, you are checking if the text “Char Sing” is in NoteData[i][“k”]. Maybe the problem you are facing is that once CharIsAllowed is true it never changes to false? You can solve that (and simplify your code) like this:

if is_instance_valid(NoteData[i][“k”]):
    var KInstance = NoteData[i][“k”]
    CharIsAllowed = KInstance == “Char Sing”

BTW, to properly format your code you can follow these instructions: Make it easier for new users to format and preview code in their posts - #4 by gertkeno

I think there is some logical issue in your code.

You are checking to see if NoteData[i][“k”] is a valid instance, and then if it is, you are then comparing it to a string? A string is not the same as an instance, which is an object.

If you describe in more detail what you are trying to achieve, perhaps we could be more helpful.

1 Like

well if you’re looking just for check if any value in array, this construction works well:

if “Char Sing” in NoteData.values(): CharIsAllowed=true

if you have a limited amount (up to 32) of bool properties check out of @export_flags() and bit flags.

NoteData[i][“k”] is not an array, it’s a dictionary. Also Arrays and Dictionaries are not considered objects.

You probably have a dictionary within an array, something like this:

var NoteData=[{"k":"Char Sing"}]

so you need to use the Dictionary has() method: NoteData[0].has("k"))

So here is example:

	var NoteData=[{"k":"Char Sing","l":"Stop Sing"}]
	print(typeof(NoteData[0]))  # 27 is Dictionary
	print(typeof(NoteData))  # 28 is Array
	print(NoteData[0].has("k"))  # Should be true
	print(NoteData[0].has("l"))  # Should be true
	print(NoteData[0].has("j"))  # should be false