Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to assign a function to a variable?

Asked by 8 years ago

Basically I want a table of functions. Could I do something like

local functions={x,y,z}

x=function()
    print("x")
end)

y=function()
    print("y")
end)

z=function()
    print("z")
end)

z()

or would it be more like

local functions={

function()
print("x")
end,
--etc

How would I do this, and how would I call the function?

2 answers

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

You had the right idea with the first example, but you'd just be storing variables you haven't defined yet. You should be defining the functions first, then using them as elements to a table. Functions can be stored inside tables just as any other data type. Here's a few examples marking the similarities:

Creating a new index

local t = {}

t.x = 1 -- store a number with new index 'x'
t.y = "string" -- store string with index 'y'

-- store function with index 'z'
t.z = function()
    print("Hello world")
end

-- access function from table and call it
t.z()

Would also be the same as:

local t = {}

-- Define function outside of table
local function x()
    print("Function x")
end

-- Set new index of table to the function. You could use any new index you want at this point, since you have reference to the original function.
t.x = x 
t.y = x
t.z = x

-- All x,y,z keys above would point to the same function.

Creating a method

local t = {}

-- Using a colon to create the function as a method for object 't'. Methods are a topic for a different discussion, however. You should ask a question about them on here some time.
function t:method()
    print("Hello world")
end

-- Calling function as method on 't'
t:method()

Initial storage

-- Creating them inside the table
local t = {
    x = function()
        print("x")
    end;
    y = function()
        print("y")
    end;

    -- Just as you could initially store other data types
    x2 = "hello";
    y2 = 100;
}

-- Same with an array, except variables wouldn't be defined
local t = {
    function()
        print("Anonymous")
    end;

    -- Same rules apply
    "hello";
    100;
}

-- I discourage the idea of having random data types thrown about in your data structures, everything here is just for demonstration purposes to show it's stored the same way.

So as demonstrated, it's the same as storing any other ordinary data type (strings, numbers, bools, etc). The only exception is creating the function as a method, which is a discussion for a different topic. Anyway, hope this cleared things up. Let me know if you have any questions.

Ad
Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
8 years ago
Edited 8 years ago

Its really simple. You just create the function and put them in an array with key,value.

function a(args)
--do stuff
end

function b(args)
--do stuff
end

function c(args)
    --do stuff
end

local allFunctions = {func1=a,func2=b,func3=c}

--for module scripts:
return allFunctions
0
note, func1, func2 etc are just names. they can be anything, same goes for a,b,c. RubenKan 3615 — 8y

Answer this question