Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

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
This topic has been archived. This means that you cannot reply to this topic.
[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • 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 {
public String getName() {
return "send";
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);
int[] nums = new int[count];
for( int i = 0; i < count; i++ ) {
nums[i] = L.toInteger( i+1 );



[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Contributor

  • 778 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);


  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.