Data type for variable in for loop

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

Several versions back you could declare a variable with a type then use in a -for- statement.

var prop_data:Dictionary;
for prop_data in get_property_list():
	# do stuff here
	pass;

At some point this stopped working and now reports the error Variable "prop_data" already defined in scope.

I’ve been working around it for awhile. Just wondering if this was intentional or a regression bug?

Thanks

I use Godot 3.2, but didn;t You just declare this variable in higher scope ? Or in parent class ?

It always worked like this and I don;t believe it is different in new Godot. What if You introduce it as

var prop_data = {}

?

Inces | 2021-12-21 14:04

Yes, at some point I think it did work. The purpose of declaring the variable outside the loop is to give it a type which is needed for intellisence. You can’t give the variable a type in the for statement itself. For example, the following is not allowed.

for prop_data:Dictionary in get_property_list():

The workaround is as follows (it works but is not very clean.

for prop_data in get_property_list():
    if (prop_data as Dictionary)["class_name"] == "":

tx350z | 2021-12-21 19:55

I see now what You wanted, sorry, but why do You need to impose this data_type ? All iterated items are going to be of the same type anyway ? If You are sure they are dicionaries , then what is the problem with syntax :

if prop_data["class_name"] == 

You would never get error in my version, if items are diciotnaries obviously

Inces | 2021-12-21 20:42