Insert multivalue into lua table

Hi all,

Trying to convert a JavaScript game into lua, and found this command:

_aTilesMc.push(new CTile(i,aPos[i],szRandValue,aLeftBlocks[i],aRightBlocks[i],aUpBlocks[i],aBlockingList[i],aTileHeight[i]));

 _aTilesMc is an array (table in lua) and push is like insert an element into it.

Dunno what CTile is, but how do you add a new multi value element into a table like this?

Also:

aTilesMc[i].init(pPos);

Can’t find an ‘init’ command in JavaScript for arrays, so not sure what init does?

CTile is a JavaScript object.  You would need to find the code for it to see what it’s new/init function does.

table.insert() would be similar to the push action.  Push sounds to me like it inserts it in the 1st position of the table.

Rob

Thanks Rob.

how do you add a new multi value element into a table?

table.insert(Table,{value1,value2,value3})

??

See:  http://docs.coronalabs.com/api/library/table/insert.html

table.insert( yourTable, yourObjectOrValue) will insert the new item at the end of the table, which is the same as:

yourTable[#yourTable+1] = yourObjectOrValue

If you want to insert the item in the middle of the table or at the begging you can specify a position #:

table.insert( yourTable, 1, yourObjectOrValue )   – inserts it at the front of the table

Rob

How about this, any javascript programmers over here?

JavaScript:

var \_aUpBlocks = new Array(); var \_aLeftBlocks = new Array(); var \_aRightBlocks = new Array(); \_aUpBlocks[0] = [];\_aLeftBlocks[0] = [];\_aRightBlocks[0] = []; \_aUpBlocks[1] = [];\_aLeftBlocks[1] = [0];\_aRightBlocks[1] = [2];

I translated this code into Lua like:

\_aUpBlocks = {} \_aLeftBlocks = {} \_aRightBlocks = {} \_aUpBlocks[0] = {};\_aLeftBlocks[0] = {};\_aRightBlocks[0] = {}; \_aUpBlocks[1] = {};\_aLeftBlocks[1] = 0;\_aRightBlocks[1] = 2;

But translating **_aUpBlocks[0] = [] to _aUpBlocks[0] = {} **doesn’t seem to be correct, it expects a numeric value.

JavaScript programmer here :slight_smile:

First off, you should probably index your arrays with 1, as that’s how Lua does it. That means all your array indices will be incremented by one. So _aUpBlocks[1] -> _aUpBlocks[2], etc.

Second, you’re setting _aLeftBlocks[1] to the number 0, rather than an array with the number 0 inside it. Same goes for _aRightBlocks[1].

\_aLeftBlocks[1] = {0}

And the semicolons are completely unneeded in Lua. Lua doesn’t have semicolon troubles.

  • Caleb

Thanks,

Yeah I already noticed indexing to 1 is common in Lua.

But what does

_aLeftBlocks[0] = []

exactly do in Javascript?

_aLeftBlocks[0] = {} doesn’t seem to be a correct translation as Lua expects a number value?

_aLeftBlocks[0] = [] in JavaScript means “set element #1 in the _aLeftBlocks array to an empty array”.

What do you mean by “Lua expects a number value”?

  • Caleb

Ok, so _aLeftBlocks[0] = {} is the correct translation from _aLeftBlocks[0] = [] in JavaScript?

If yes, I will figure out the rest :slight_smile: Thanks again.

Technically, yes. But, like I said, JavaScript uses 0 as the first element in an array, while Lua uses 1. A “semantic” translation would change the 0s to 1s and the 1s to 2s etc.

  • Caleb

CTile is a JavaScript object.  You would need to find the code for it to see what it’s new/init function does.

table.insert() would be similar to the push action.  Push sounds to me like it inserts it in the 1st position of the table.

Rob

Thanks Rob.

how do you add a new multi value element into a table?

table.insert(Table,{value1,value2,value3})

??

See:  http://docs.coronalabs.com/api/library/table/insert.html

table.insert( yourTable, yourObjectOrValue) will insert the new item at the end of the table, which is the same as:

yourTable[#yourTable+1] = yourObjectOrValue

If you want to insert the item in the middle of the table or at the begging you can specify a position #:

table.insert( yourTable, 1, yourObjectOrValue )   – inserts it at the front of the table

Rob

How about this, any javascript programmers over here?

JavaScript:

var \_aUpBlocks = new Array(); var \_aLeftBlocks = new Array(); var \_aRightBlocks = new Array(); \_aUpBlocks[0] = [];\_aLeftBlocks[0] = [];\_aRightBlocks[0] = []; \_aUpBlocks[1] = [];\_aLeftBlocks[1] = [0];\_aRightBlocks[1] = [2];

I translated this code into Lua like:

\_aUpBlocks = {} \_aLeftBlocks = {} \_aRightBlocks = {} \_aUpBlocks[0] = {};\_aLeftBlocks[0] = {};\_aRightBlocks[0] = {}; \_aUpBlocks[1] = {};\_aLeftBlocks[1] = 0;\_aRightBlocks[1] = 2;

But translating **_aUpBlocks[0] = [] to _aUpBlocks[0] = {} **doesn’t seem to be correct, it expects a numeric value.

JavaScript programmer here :slight_smile:

First off, you should probably index your arrays with 1, as that’s how Lua does it. That means all your array indices will be incremented by one. So _aUpBlocks[1] -> _aUpBlocks[2], etc.

Second, you’re setting _aLeftBlocks[1] to the number 0, rather than an array with the number 0 inside it. Same goes for _aRightBlocks[1].

\_aLeftBlocks[1] = {0}

And the semicolons are completely unneeded in Lua. Lua doesn’t have semicolon troubles.

  • Caleb

Thanks,

Yeah I already noticed indexing to 1 is common in Lua.

But what does

_aLeftBlocks[0] = []

exactly do in Javascript?

_aLeftBlocks[0] = {} doesn’t seem to be a correct translation as Lua expects a number value?

_aLeftBlocks[0] = [] in JavaScript means “set element #1 in the _aLeftBlocks array to an empty array”.

What do you mean by “Lua expects a number value”?

  • Caleb

Ok, so _aLeftBlocks[0] = {} is the correct translation from _aLeftBlocks[0] = [] in JavaScript?

If yes, I will figure out the rest :slight_smile: Thanks again.

Technically, yes. But, like I said, JavaScript uses 0 as the first element in an array, while Lua uses 1. A “semantic” translation would change the 0s to 1s and the 1s to 2s etc.

  • Caleb