Lua recursion/Tail Calls problem in Corona

Hello,

I’m trying to write a function for path finding.

--//node table is like this: --//{{y, x, cost, parent}, {y2, x2, cost2, parent2}} --//A function like this: function count(nodeTable) local y, x, cost, parent for i=1, #nodeTabledo y = nodeTable[i][1] --// ALWAYS error here if #nodeTable is not 1 when I try tail calls x = nodeTable[i][2] cost = nodeTable[i][3] parent = nodeTable[i][4] doSomething() --//checks things, removes checked nodes and insert new nodes into a new table for the next loop end if(#newNodeTable ~= 0) then return count(newNodeTable) --// tail call end end

However it will cause a error when the size of node table is not 1 at the line I marked

If I don’t use tail call, that line will pass just fine, but I need recursion t finish the path finder.

There must be something wrong. :frowning:

Can someone help me?

Thanks!

So by tail call, you just mean you’re running the function continuously until you reach a desired output? Okay…

  1. It’s a bit hard to tell what’s going on because you’re not (in the sample) passing the values on anywhere. You’ve declared y,x,cost,parent but they aren’t going into a node table. doSomething() is called but isn’t passing in those values.

  2. When you remove elements from a table, it’s highly recommended that you look through it backwards. If you remove while moving forward, your table will have gaps and those gaps *will* crash your code.

[lua]-- Iterate backwards

for i = #nodeTable, 1, -1 do

  – whatever, ie:

  table.remove(nodeTable, i) – removes this entry safely from the table

end

[/lua]

Thanks,

I found that my nodeTable removal causes potential logic errors.

By the way, if I want to make my nodes structure much more like an object (table).

How do I define the table so that I can make declare a nodeTable?

What I did is:

local node= {}

node.x = 0

node.y = 0

node.cost = 0

node.parent = 0

And now how do I new a node array like Java/C?

require “module” seems not an ideal solution in my case, I need a lot of nodes.

Thanks!

That structure works, and I find it easiest to use. But there are two methods:

[lua] – Requires seperate statements but you don’t have to juggle commas

local node = {}

node.x = 0

node.y = 0

node.yummy = true

– Builds the table in one shot, but requires comma management

local node2 = {

  x = 0,

  y = 0,

  yummy = true,

}

– Of course, once the table is created you can always add on to it.

node2.big = “maybe”

[/lua]

Not really sure what you mean with your last question.

Hello,

What I mean is:

If I want to new a object array

(something like this :

Node[] myNode = new Node[];  

How do I this in lua? or something close…

Thanks!

Not really sure what that is - I don’t use c much…

But to append to a table, the best method is

[lua]-- let’s assume you have a table called stuff{}

stuff[#stuff+1] = “morestuff”

– or,

stuff[#stuff+1] = { x=32, y=34, hungry=true }

[/lua]

So by tail call, you just mean you’re running the function continuously until you reach a desired output? Okay…

  1. It’s a bit hard to tell what’s going on because you’re not (in the sample) passing the values on anywhere. You’ve declared y,x,cost,parent but they aren’t going into a node table. doSomething() is called but isn’t passing in those values.

  2. When you remove elements from a table, it’s highly recommended that you look through it backwards. If you remove while moving forward, your table will have gaps and those gaps *will* crash your code.

[lua]-- Iterate backwards

for i = #nodeTable, 1, -1 do

  – whatever, ie:

  table.remove(nodeTable, i) – removes this entry safely from the table

end

[/lua]

Thanks,

I found that my nodeTable removal causes potential logic errors.

By the way, if I want to make my nodes structure much more like an object (table).

How do I define the table so that I can make declare a nodeTable?

What I did is:

local node= {}

node.x = 0

node.y = 0

node.cost = 0

node.parent = 0

And now how do I new a node array like Java/C?

require “module” seems not an ideal solution in my case, I need a lot of nodes.

Thanks!

That structure works, and I find it easiest to use. But there are two methods:

[lua] – Requires seperate statements but you don’t have to juggle commas

local node = {}

node.x = 0

node.y = 0

node.yummy = true

– Builds the table in one shot, but requires comma management

local node2 = {

  x = 0,

  y = 0,

  yummy = true,

}

– Of course, once the table is created you can always add on to it.

node2.big = “maybe”

[/lua]

Not really sure what you mean with your last question.

Hello,

What I mean is:

If I want to new a object array

(something like this :

Node[] myNode = new Node[];  

How do I this in lua? or something close…

Thanks!

Not really sure what that is - I don’t use c much…

But to append to a table, the best method is

[lua]-- let’s assume you have a table called stuff{}

stuff[#stuff+1] = “morestuff”

– or,

stuff[#stuff+1] = { x=32, y=34, hungry=true }

[/lua]