0
votes

I am actually trying to create a class in C++ to encapsulate calls of Lua, actually trying to encapsulate a function to load a Lua script on the constructor, but the main problem is when I try to make a function call of the Lua script I can't find how to store the multiple returns value and how to correctly push all the arguments.

I'm trying to find an idea of the implementaion of a function who call a lua fonction with any number of any type of parameter, (the function will push arguments and call the function, but i dont want dynamic cast for example to know if i have to "lua_pushnumber" or push string for example.

1
Posting your code is very helpful. - Pubby
The problem is that i don't know how to write this code, i know i'll have to use va_args to get all the parameters but i can't find a way to trait parameter to push them elegantly. ;-) - Loadex
Did you take a look at luabind? They provide a very good c++ style lua integration. Maybe you can find some inspiration there. - mkaes
I'm not very good in english but if i understannd well, that bring an object vision in lua ? If it is i'm not looking for this, I'm just looking for a elegant way to call fonctions in lua with a class. Thansk for your answer and your help ;-) - Loadex
First, it's "function", not "fonction". Second, what is your question? Are you asking for code on how to do this? Do you understand how to send parameters to Lua functions with the regular Lua API? Are you asking for advice on how to build a generic API for C++ that does this in a type-safe way? What are you looking for here? - Nicol Bolas

1 Answers

0
votes

You could try to accept an array of some type of variant class in your constructor, and use their type to determine how to pump them into Lua. On the other hand, there is really only two types that are interchangeable between C/C++ and Lua: string and number/double. A possible solution is to give pass in an array of strings (or a char** and an int, if you prefer), passing in your doubles as strings as well.

You could then do your loadstring() call by appending a string representation of your string or double after the string "return ". When you execute the function pushed to the stack by loadstring(), the lua engine will push your variable (string or double with appropriate type) to the lua stack. You would have the overhead of string-parsing your doubles, but if you were dying for speed I bet you would be coding purely in C++ anyways :) The advantage of this method is that you could actually pass in a function this way too: (i.e. "return function() print("hello"); end")