Executing functions stored in a table [SOLVED]

Given Lua’s lack of a C-style switch statement, I would like to store a bunch of function names in a table and execute them with an index (number). I can’t work out the syntax though - has anyone managed to do this?

1 Like

Should be as easy as this!

    local funcs = {}
	
    funcs[1] = function(boop)
		print(boop)
	end

	funcs[1]("beep")

    --beep
8 Likes

Thanks! I went for this slightly different arrangement in the end.

function func1(self)
end

function func2(self)
end

function callfunc(self, n)
	local func = {func1, func2}
	func[n](self)
end
4 Likes