Teeter477
Teeter477

Reputation: 11

Passing Partial Function Name and Concatenating to Call Original Function

Alright guys, my first time posting here (after many years of reading). I'm pretty new to LUA, and I agree that it's incredibly useful and simple, however I still have a hard time for some reason.

Heads up, I'm basically reinventing the wheel here, but I want to make it as easy as possible for testers to create debuggers for a simulation engine I work with. I could easily just let them do it the way that exists, but at this point, it's personal. I need to understand what the issue is with my logic.

The existing method for creating a debugger in the engine would be something like:

print_message_to_user(string.format("Engine RPM: %d", sensor_data:getEngineRPM()))

However I'm trying to create a function, debugger(), which shortens and simplifies the process:

function debugger(helptext, sensor)
    print_message_to_user(string.format("%s: %d", helptext, sensor_data:..sensor..()))
end

Which is then called like:

debugger("Engine RPM", getEngineLeftRPM)

...and of course doesn't work. I'm sure it has everything to do with the way I'm trying to concatenate the supplied handle with symbology to hopefully call the actual function.

Upvotes: 0

Views: 178

Answers (1)

Egor Skriptunoff
Egor Skriptunoff

Reputation: 23737

function debugger(helptext, sensor)
    print_message_to_user(
       string.format("%s: %d", helptext, sensor_data[sensor](sensor_data))
    )
end

debugger("Engine RPM", "getEngineLeftRPM")

Upvotes: 3

Related Questions