Open vinniefalco opened 7 years ago
So far i'm doing this :
state("a={}");
for(std::vector<uint32_t>::iterator i = a->begin();i!= a->end();i++) {
std::string cmd = "table.insert(a,"+std::to_string(*i)+")";
state(cmd.c_str());
}
But I'ld love a better way, indeed :)
Oh my...that's awful! lol
I was experimenting the following, although I'm unsure whether or not I'm going to use this code in my project:
int counter = 0;
// ...
state(std::string("x" + std::to_string(++counter) + " = Object:new()").c_str());
sel::Selector tableReference = state[std::string("x" + std::to_string(counter)).c_str()];
Here I call a user-defined new method from a Lua do_string that contains the x char and concatenate it into a counter that tells how many tables have been instantiated in the program so that they may have unique identifiers. I don't care about the identifier since at least in my project I'm never going to call them directly. I only use this identifer so that lua may own the table. Ideally I would want to store it directly in C++ and have lua only reference it, but I don't know how I would accomplish that. Then when it's time to free the memory, set the table to nil.
My first solution was creating a Lua VM for each instance of my table since each VM maintains its own state. I was not sure if creating many VMs is going to have a significant impact on performance so I played it safe by looking for other ways. The big advantages here were that the states were self-contained, there was no need for manual memory handling and the code just looked better. Oh well.
Its not clear to me how to create a table in the Lua environment from C++...