Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How do I read an array argument from a LuaState
Started by meister.public May 13 2018 09:03 PM

1 reply to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

meister.public

[GLOBAL: userInfoPane.html]
meister.public
  • Observer

  • 9 posts
  • Corona SDK

I have a named function named "send" which in lua accepts a table array of integers for an argument.

 

On the java side, how do I fetch that data from the LuaState and convert it to an int[]?

 

private class send implements NamedJavaFunction {
@Override
public String getName() {
return "send";
}
@Override
public int invoke(LuaState L) {

// get array size from LuaState

int[] nums = new int[arraySize];

// get array values from LuaState

 

 

This is my current approach which doesn't seem to work:

 

 

int count = L.tableSize(1);
L.getTable(1);
int[] nums = new int[count];
for( int i = 0; i < count; i++ ) {
nums[i] = L.toInteger( i+1 );
}

 

Thanks



[TOPIC: post.html]
#2

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 697 posts
  • Corona SDK

In C / C++, I would do

size_t n = lua_objlen(L, 1); // assuming array is on slot 1

for (int i = 1; i <= n; ++i)
{
  lua_rawgeti(L, 1, i); // array, item #i

  lua_Integer integer = lua_tointeger(L, -1);

  DoSomething(integer);

  lua_pop(L, 1); // array
}

I've occasionally seen some of the Java code and it usually looks similar to the C API, so maybe it would here too? Do note that all the Lua indices will be 1-based, though.




[topic_controls]
[/topic_controls]