I have an Array that may eventually contain a reference to itself. The simplest case would be something like this:
var cyclic = [1, 2, 3]
cyclic.append(cyclic)
Now I'd like to recursively process such structures without ending in an infinite loop, e.g. something like that:
func do_something(structure, already_visited):
if typeof(structure) == TYPE_INT:
print("do something with it")
elif typeof(structure) == TYPE_ARRAY:
if already_visited.has(structure):
return
already_visited[structure] = true
for item in structure:
do_something(item, already_visited)
do_something(cyclic, {})
Unfortunately this doesn't work because
already_visited[structure] = true
seems to send GDScript into an infinite loop (it is probably trying to build a hash value for the Array and ends up in the cycle...). So I can't put a reference to the cyclic array itself into already_visited - but what else can I put in there that will uniquely identify the Array? Is there something like Python's id() function that would yield, for example, the memory address of the Array?