1

I am making a dynamic callback table for a helper function that handles input events. I want to make a(testString) execute when functionTable[1](testString) executes or allow a way for it to be run directly from a string.

functionTable = {}
testString = "atad atad atad"

function a(param)
    print(param)
end

functionTable[1] = "a"

exec(functionTable[1].."(testString)")

How should I be doing this?

(for Lua 5.1)

1 Answer 1

2

you can use the load() function to execute strings:

functionTable = {}
testString = "atad atad atad"

function a(param)
    print("hello " .. param)
end

functionTable[1] = "a"

load(functionTable[1].."(testString)")()
Sign up to request clarification or add additional context in comments.

5 Comments

This works, thank you! Why is the second pair of parentheses necessary?
@GrifyDev it is because load() is a function that just collects a string and transforms it into a real execution, however it is necessary that we call the function inside load() so that we can actually execute it and collect its return
I meant the () here
load(functionTable[1].."(testString)")__()__
yes exactly, load() will only convert the string to an executable, but we still need to call it by the function being called inside load()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.