0 votes

Is it possible to create an array of commands and excute said commands by index?

for example,

var array = [print("1"),get_node("node"),set_process(true)]
array[1] #prints 1
in Engine by (40 points)
edited by

1 Answer

+2 votes
Best answer

Currently you can't do it with that kind of syntax, but it's possible to do it like this:

func action1():
    print(1)


func action2():
    get_node("node").set_process(true)


var array = ["action1", "action2"]


func do_actions():
    # Print 1
    call(array[0])

However in a future version it will be possible to use lambdas:

func _ready():
    array = [

        func():
            print(1),

        func():
            get_node("node").set_process(true)
    ]

    # Print 1
    array[0]()

Note: array indexes start at 0 ;)

by (28,788 points)
selected by
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 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 webmaster@godotengine.org with your username.