Accessing Locals/@counter_pos in a for loop

Godot Version



When using a for loop such as the following, I noticed in the Debugger there is a @counter_pos variable in Locals that seems to be automatically created and updated “behind the scenes”.

It would be nice to be able to use that as a “freebie” like so:

for value in array_of_values:
    var object = value                  # Access looks like this.
    var index = @counter_pos            # This would be nice
    print("This is value %s" % index)   # to use inside the loop like this --> 

without having to resort to something like:

for index in array_of_values.size():
    var object = array_of_values[index] # Access changes to this
    print("This is value %s" % index)   # just to have the index.

Direct object access above “feels nice”. Maybe this is possible and I just haven’t found how to access it?

Thanks for any hints,

I’m not aware of a way to do that. There’s a proposal for it Add support for looping over dictionaries' key and value (`for key, value in dict`) · Issue #3457 · godotengine/godot-proposals · GitHub but I’m not sure if it will be implemented or not.

1 Like

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