Page 1 of 1

Accessing Lua Variables From C

PostPosted: Tue Apr 12, 2016 1:05 pm
by Kyle.Gilbertson
I have a C function that can access Lua variables in a Lua standalone environment that doesn't work in MarketScope (Indicore). The function tries to access arrays like this:

In Lua:
List1 = {68.9, 70.2}
List2 = {54.8, 52.1}

In C:
double array1[20];
int i =0, n=0;

lua_getglobal(L, "List1");
n = luaL_getn(L, 1);

for (int i=0; i < n; i++) {
lua_pushnumber(L, i + 1);
lua_rawgeti(L, -2, i + 1);
array1[i] = lua_tonumber(L, -1);
lua_pop(L, 2);
}

This code gets nothing. I've tried making the variables global, but that didn't work either. Is the only way to get an array (table) from Lua to C in Indicore through the parameter stack? Is there an easier way to make variables visible between the two environments? In any case, does anyone have sample code for getting arrays (tables) from Lua to C in the Indicore environment? [And vice versa]

Re: Accessing Lua Variables From C

PostPosted: Wed Apr 13, 2016 8:20 am
by Julia CJ
Hi Kyle,

Please read the following information from this article.

Thank you.

Re: Accessing Lua Variables From C

PostPosted: Wed Apr 13, 2016 5:46 pm
by Kyle.Gilbertson
The article was interesting, but I'm not sure it's applicable in this case. The variable access procedure I used works in a Lua standalone setting, but not in the Indicore implementation. The root of the issue is access to the variables. If the variables are declared local, for sure they are not available, and they are not available in the standalone environment either. However, when they are declared global, they still aren't available.

These two calls:
lua_getglobal(L, "List1");
n = luaL_getn(L, 1);

n comes back 0 no matter what I do with the arrays I want to access in Indicore. In the Lua standalone environment, as in not within Indicore, I can get those arrays no problem.

How do I get a hold of variables inside the Indicore Lua? Must they be passed through the stack as parameters to the function, or is there another way to get access to them?