Would this be a bad practice?

So basically i’m wondering if passing an entire function as an argument would be a bad idea, It seems to be a common practice in python, So I just want to make sure lua would handle it 99% of the time.

local ExternalModule = require(game.ExternalModule);

local function RunAtEndLerp()
    print("Completed");
end;

local function OnItemTouch()
    if condition then
         ExternalModule:FireThingy(true, RunAtEndLerp);
    end;
end;

ExternalModule:

local module = {};
    function module:FireThingy(value1, DoFunction);
        if value1 then 
            --<Does The 'Lerping' Yield;
            DoFunction(); --<Then calls the passed function
    end
    end;
return module;

For anyone who sees this thread, it apparently is well supported by lua. Not a bad practice at all.

1 Like

There is no reason for this to be a bad practice. Lua’s functions are first-class citizens, meaning they can be returned, passed as an argument, set to a variable ect. It’s supposed to be a lua feature.

The function in this case is a “callback”, a callback function is a function ran directly after something has happened (functions passed to events for example). These functions are usually passed as a last argument and called when the function did what it wanted to do correctly.

2 Likes