Storing function calls in tables

Is there any way I can store a call to a function in a table, and then call that function, like this:

local levels = { loadLevel1() } function M.load(levelNumber) --call something like levels[levelNumber] end function loadLevel1() --loads level 1 map and enemies end

How could I achieve something like this?

local function loadLevel1() --loads level 1 map and enemies end local function loadLevel2() --loads level 2 map and enemies end local levels = { loadLevel1, loadLevel2, } function M.load(levelNumber) levels[levelNumber]() -- UPDATED end

Hi sdktester15,

function name with parentheses on the end means execute function and return something (by the way nil is something too). Use only name of the function instead. 

@roaminggamer:  I just want write something similar. You was first :slight_smile: hehe

Have a nice day:)

ldurniat

Oh and then I just call something like levelslevelNumber ?

Correct. I updated my post.  Sorry to miss that.

Thanks!

local function loadLevel1() --loads level 1 map and enemies end local function loadLevel2() --loads level 2 map and enemies end local levels = { loadLevel1, loadLevel2, } function M.load(levelNumber) levels[levelNumber]() -- UPDATED end

Hi sdktester15,

function name with parentheses on the end means execute function and return something (by the way nil is something too). Use only name of the function instead. 

@roaminggamer:  I just want write something similar. You was first :slight_smile: hehe

Have a nice day:)

ldurniat

Oh and then I just call something like levelslevelNumber ?

Correct. I updated my post.  Sorry to miss that.

Thanks!