I would like to extend the Lua so that when a user writes a script, a variable is already available to him.
That is certainly possible. Once the Lua state exists you can create global variables (ie. place key/value pairs in the global environment table).
Is it allowed to create object instances before running a LUA script ?
Yes it is. In my code I preload all sorts of stuff. Tables of useful things for the end-user. Extra libraries they can call.
Example code:
typedef struct { const char* key; int val; } flags_pair;
...
static flags_pair trigger_flags[] =
{
{ "Enabled", 1 }, // enable trigger
{ "OmitFromLog", 2 }, // omit from log file
{ "OmitFromOutput", 4 }, // omit trigger from output
{ "KeepEvaluating", 8 }, // keep evaluating
{ "IgnoreCase", 16 }, // ignore case when matching
{ "RegularExpression", 32 }, // trigger uses regular expression
{ "ExpandVariables", 512 }, // expand variables like @direction
{ "Replace", 1024 }, // replace existing trigger of same name
{ "LowercaseWildcard", 2048 }, // wildcards forced to lower-case
{ "Temporary", 16384 }, // temporary - do not save to world file
{ "OneShot", 32768 }, // if set, trigger only fires once
{ NULL, 0 }
};
...
static int MakeFlagsTable (lua_State *L,
const char *name,
const flags_pair *arr)
{
const flags_pair *p;
lua_newtable(L);
for(p=arr; p->key != NULL; p++) {
lua_pushstring(L, p->key);
lua_pushnumber(L, p->val);
lua_rawset(L, -3);
}
lua_setglobal (L, name);
return 1;
}
...
MakeFlagsTable (L, "trigger_flag", trigger_flags)
lua_Statebefore adding your variable?lua_load), and then you call that function. Lua execution is driven by the host. There's no "running machine" in way that you might expect from other languages.