The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

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#

in Engine by (25 points)

1 Answer

0 votes

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():


by (62 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.