How do I execute a function reference saves in a variable?

I'm trying to have an object save a function to a variable, that it should call when it's done processing something. Does anybody know how to call a reference to another function you saved to a variable?

If you're talking about Lua, my memory is rusty but I seem to recall functions can be passed around willy-nilly like in JavaScript and called as any method of an object normally would be.

If you're talking about C, the way this is done is something called a function pointer. A function pointer has the following syntax:

returnType (*)(argType1, argType2, ...)

For example, a function pointer for a two-operand operation on integers might look like this:

int (*)(int, int)

Unintuitively, if a name is being associated with the type, as in a typedef or variable declaration, the name appears next to the asterisk:

int (*funcVar)(int, int); // Declares a variable named funcVar

The function being associated with that type needs to have the same prototype, or else gloom and doom. Pointers are obtained with the & operator as usual:

int Multiply(int a, int b) {
     return a * b;
}

void main() {
    int (*funcVar)(int, int) = &Multiply;
    int c = funcVar(5, 3); // 15
}

For brevity, function pointers can also be used in typedef statements:

typedef int (*TwoOp)(int, int);

TwoOp funcVar = &Multiply;

Lastly, to use a function pointer in a cast:

void *funcVar = &Multiply;
TwoOp cast1 = (TwoOp) funcVar; // From a typedef
int (*cast2)(int, int) = (int (*)(int, int)) funcVar; // With the naked type
int c = cast1(5, 3); // 15
int d = cast2(2, 7); // 14
1 Like

Hi,

I'm talking about Lua.

local toCallWhenDone = someOtherFunction
-- how do I execute the stored reference toCallWhenDone?

Just call the variable like a function.

In Lua, functions don't actually have names. Every "function" is actually a reference variable to an anonymous function. The name in a function declaration is the name of the variable you're storing the initial reference into.

So in your example, you're copying the reference from the variable someOtherFunction into the variable toCallWhenDone.

This may be of help

Lua - Function Calls
https://www.lua.org/manual/5.4/manual.html#3.4.10

Can somebody please write a 2-line example? I really don't understand the above explanations.

local toCallWhenDone = someOtherFunction
toCallWhenDone()

Though I don't quite get what you're trying to do, or why you'd do this.

Thank you very much Matt!

I was trying to create a callback from an object (a video player) who can inform the main application when a video is ready. As far as I understand there's no built-in Event model in Lua, so the main app can't add an event listener to the videoPlayer to listen for a "videoReady" event.

(I made complex games in AS3 long ago, where most communications between objects were handled by weak event listeners to avoid storing hard references so the garbage collection would be easier)

1 Like

Understood! Keep going :slight_smile:

Also check this out A list of helpful libraries and code - #3 by dustin and this A list of helpful libraries and code - #4 by dustin