2
votes

Ok so, this is more a sanity check than anything else.

Lets asume we have a struct called lua_State, now I need to create a uncertain amount of unique lua_State's. To make sure I don't use the same variable name twice I would need to have some sort of way to get an unique variable every time i make a new state.

However there is only one way (I think?) to create a new state, and that is as follows:

lua_State *S = lewL_newstate();

Now I would need some way to dynamically change that "S" to.. whatever.

For example: If I had 4 lua files, and I wanted to load each into their own lua_State, I would call: lua_State *A = lewL_newstate(); for the first, lua_State *B = lewL_newstate(); for the second, and so on. Keep in mind the number of lua files varies so creating a fixed number of states probably won't go over well.

How would I go about doing this?

clarification:

.h

struct lua_State

.cpp

createNewState(Lua_State* something){
 lua_State* something = luaL_newstate();
}

I thought about creating a

std::map<int, lua_State*> luaMap;

but then I would still have the problem of actually generating (for lack of better words) a variable name for every int-index.

So, have I been drinking too much coffee and is there a glaringly obvious simply solution to what I am trying to do, or should I just stop coding untill the crazy blows over?

Thanks in advance.

4
Is there something wrong with using an array/vector of (pointers to) lua_state s? - Jerry Coffin
Why do you need unique variable names? Is a vector not good enough? - schot
The problem is not in storing the states, the problem is making up State variable names depending on the number of lua files to be loaded. so 2 files = how do I get 2 unique variable names to call these new states? Its kinda the point that I dont know how many variable names (states) I need. So I can't create them beforehand. - Karrok
By the time your program is running, the code has already been compiled, so it's too late to add new C++ variables to your program, and therefore it's pointless to worry about those variables' names. You can certainly associate names with the files you're loading (as in Reko's answer), but they cannot be variables. - Rob Kennedy

4 Answers

3
votes

Use a std::vector to both store the created states and generate sequential identifiers (i.e. array indices). Unless I'm missing something, then you are grossly over-complicating your requirements.

std::vector<lua_State *> stateList;

// create a new Lua state and return it's ID number
int newLuaState()
{
    stateList.push_back(luaL_newstate());
    return stateList.size() - 1;
}

// retrieve a Lua state by its ID number
lua_State * getLuaState(int id)
{
    assert(0 <= id && stateList.size() > id);
    return stateList[id];
}
2
votes

Can't you use std::map<std::string, lua_State*> and use the script name as an index to the state?

1
votes

Why do you need a variable name for every index? Why is it not good enough to refer to, for example, luaMap[0] and luaMap[1]? I don't think there's really any way to do what you want. You need some sort of dynamic array, like a std::vector.

0
votes

GiNaC does this, but the name has to be explicitly given to a variable