can we make "custom array" like the "custom iterator" thing ?

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

i know there is custom iterator:

but is there an easy way to have something like this but with “custom array” ?

i want to do something like this:

var myclass =
print( myclass[0] ) → should print “hello”

no problem if its not possible, but i remember doing this in c#

:bust_in_silhouette: Reply From: Luck_437

I wa asking myself the same question, and as it could be useful, I will answer for anyone wo need it. It’s not possible to fast access keys of an object like that, not with numbers. With strings, we still get that complex virtual _get() method, but it is another area.

For purpose a basic example of an object class :

extends Object
var inventory = ["A","B","C","D"]
var i = 0

func _iter_init(arg):
	i = 0
	return i < inventory.size()

func _iter_next(arg):
	i += 1
	return i < inventory.size()

func _iter_get(arg):
	return inventory[i]

func key(n:int):
	return inventory[n]

And here the working code access inside a scene :

extends Node
var object = preload("").new()

func _ready():
	for each in object : prints(each)


An alternative, which use only the key function :

extends Node
var object = preload("").new()

func _ready():