Get Value in Dictionary

Godot Version

Godot 4

Question

Hello, i have this function for returning the int from dictionary

var venue_list : Dictionary = {}

func get_venue_state(venue_id: int) -> int:
	# Check if the id exists in the dictionary directly
	print("Checking venue_id:", venue_id)
	print("Current venue_list keys:", venue_list.keys())
	if venue_id in venue_list:
		print("id exists in the dictionary")
		return venue_list[venue_id]
	# If not, set the venue state to LOCKED
	return STATE.LOCKED

When I called it with venue_id 10, it literally has a value of 10 in the venue_list. But why doesn’t it print “ID exists in the dictionary”?

This is the Screenshot of my debug
DIKKPO

Try if venue_id in venue_list.keys()

Still the same

Show us definition of dictionary. Wondering if datatype issues. E.g. floating point like 10.0 in key key.

This would fail for 10.

var venue_list : Dictionary = {10.0:1,20:2}
func _ready():
	get_venue_state(10)
	get_venue_state(20)
	pass
1 Like

Maybe the “has” would work.

func Check_List(value):
	if venue_list.has(value):
		print("This Key exists.")
	else:
		print("This Key doesn't exist.")

It works for me. How do you define STATE.LOCKED? And how do you fill the dictionary?

.has() is same as in as per docs… Will not work if you have incompatible key types.
As far as how you fill out Dictionaries, there are many ways. Study the Dictionary class.

STATE.LOCKED is probably an enum. Where did you find it?

its an enum

const STATE = {
	"LOCKED": 1,
	"FIRST_TIME_UNLOCKED": 2,
	"UNLOCKED": 3,
	"MISSING": 4
}

This is how my state

func set_venue_state(venue_id: int, venue_state: int) -> void:
	venue_list[venue_id] = venue_state

and this is how i fill the dict

I found that the key in the dictionary was a float, and I was comparing it with an int. I changed the data type of venue_id to float, it worked.

Thanks for the insight man

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.