table as argument

Hi I’m getting nuts with this error, please can anybody assist:

Runtime error
…tK0iCwFUOGTxnGoM+l4k+++TI/TemporaryItems/10/main.lua:43: attempt to call field ‘targetNearestEnemy’ (a nil value)
stack traceback:
[C]: in function ‘targetNearestEnemy’
…tK0iCwFUOGTxnGoM+l4k+++TI/TemporaryItems/10/main.lua:43: in function <…tk0icwfuogtxngom>
?: in function <?:214>

I just want to call a function from the module:

main.lua

<br>...<br><br>local enemy = display.newRect( game, 100, 20, 30, 25 )<br>enemy.enemyType = "cell_enem_normal";<br>tab_enemigos[#tab_enemigos+1]= enemy;<br><br>local function mover(event)<br> transition.to( player, { time=1500, x=event.x, y=event.y } )<br>end<br><br>Runtime:addEventListener("touch", mover)<br><br>local function duplicar(event)<br> if event.phase == "ended" then<br> local newCell = celula.new({cellType="cell_division", x=player.x, y=player.y});<br> game:insert(newCell)<br> print(#tab_enemigos)<br> newCell.targetNearestEnemy(tab_enemigos);<br> end<br>end<br><br>

module celula

<br><br>module(..., package.seeall);<br><br>function new(params)<br>...<br>end<br><br>function targetNearestEnemy(enemigosTab)<br> <br> local enemigosCercanos = {};<br> <br> for i,enemActual in ipairs(enemigosTab) do<br> --local enemActual = enemigosTab[i];<br> print(enemActual.enemyType)<br> <br> end<br> <br>end<br>

Thanks!. [import]uid: 8933 topic_id: 7648 reply_id: 307648[/import] </…tk0icwfuogtxngom>

line 18 should be celula.targetNearestEnemy(tab_enemigos);

In the new() function you could have stuck the targetNearestEnemy() function into the object, but you didn’t. [import]uid: 12108 topic_id: 7648 reply_id: 27119[/import]

Hi, I just changed the targetNearestEnemy() to the new function, but it’s still throwing the same error on main.lua:

[code]

function new(params)
– Objeto principal
local celula = display.newCircle( _W / 2, _H / 2, 10 )
celula:setReferencePoint ( display.CenterReferencePoint);

– Color dependiendo de si es la celula principal o celula de particion
if (params) then
if (params.cellType == “cell_origen”) then
celula:setFillColor(255, 255, 0)
celula.target = nil
celula.collision = onCollision
celula:addEventListener ( “collision”, celula)
physics.addBody ( celula, “dinamyc”, {density=0.2, friction=0.2, bounce=0})

– TargetNearestEnemy function
targetNearestEnemy = function (enemigosTab)

local enemigosCercanos = {};

for i,enemActual in ipairs(enemigosTab) do
–local enemActual = enemigosTab[i];
print(enemActual.enemyType)

end
end

return celula

end

[/code] [import]uid: 8933 topic_id: 7648 reply_id: 27127[/import]

you are not understanding how modules work

for your first example you would have
[lua]local celula = require(“celula”)

– function is module function
celula.targetNearestEnemy(tab_enemigos)[/lua]

for your second example, in your new function try
[lua]celula.targetNearestEnemy = function (enemigosTab)[/lua]

it’s a method of the object, not the module in that instance.

condsider the following

[lua]module(…, package.seeall)

function new(id,x,y)

local cell = {}

local rect = display.newRect(0,0,100,100)
rect:setFillColor(255,0,0,255)
rect:setReferencePoint(display.TopLeftReferencePoint)
rect.x=x
rect.y=y

cell.id = id
cell.rect = rect

function cell.targetNearestEnemy(enemy_tab)
print(“enemy_tab length=”…(#enemy_tab))
print("target nearest enemy to "…(cell.id))
cell.rect.alpha=0.5
end

return cell

end

function generalFunction()
print(“a general function”)
end[/lua]

[lua]local celula = require(“celula”)

local enemyTable = {}

local newCell = celula.new(“cell1”, 0,0 )
enemyTable[#enemyTable+1] = newCell
newCell.targetNearestEnemy(enemyTable)
– enemy_tab length=1, target nearest enemy to cell1

local newCell2 = celula.new(“cell2”, 100,100 )
enemyTable[#enemyTable+1] = newCell2
newCell2.targetNearestEnemy(enemyTable)
– enemy_tab length=2, target nearest enemy to cell2

celula.generalFunction() – a general function[/lua]
personally i would use metatables though, although my method above is slightly different than graham’s…
http://developer.anscamobile.com/forum/2011/01/19/big-refactor#comment-17393 [import]uid: 6645 topic_id: 7648 reply_id: 27156[/import]

I thank you very much for your kind assistance, I’m taking a look to the metatable thing.

Regards.
David. [import]uid: 8933 topic_id: 7648 reply_id: 27183[/import]

Hi, I’m testing with metatable now. I picked a good example of yours jmp909 and tried to code something similar but I’m definitely missing something again:

celula_mt.lua

module(..., package.seeall)  
   
celula = {}  
celula\_mt = { \_\_index = celula }   
   
-- functions  
--local spawnAnim;  
--local onCellCollision  
--local onCellTouch  
   
   
function new(params)  
  
 local self = {}  
  
 if (params.cellType == "cell\_origen") then  
  
 --cell = display.newImage("celula\_A\_small\_1.png")  
 cell = display.newCircle( \_W / 2, \_H / 2, 10 )  
 cell.x = \_W / 2;  
 cell.y = \_H / 2;  
 cell:setReferencePoint ( display.CenterReferencePoint);  
 --cell.collision = onCellCollision  
 --cell.touch = onCellTouch  
 --cell:addEventListener ( "touch", cell)  
 --cell:addEventListener ( "collision", cell)  
  
 -- set reference back to "word" object (self) so events can reference it  
 cell.\_parent = self;  
  
 physics.addBody ( cell, "dinamyc", {density=0.2, friction=0.2, bounce=0})  
 elseif (params.cellType =="cell\_division") then  
 ...  
 end  
  
 self.\_name = "celula"  
 self.target = nil  
  
 setmetatable(self, celula\_mt)   
 return self  
  
end  

main.lua

local celula = require("celula\_mt")  
  
physics = require("physics");  
physics.start()  
physics.setGravity(0,0)  
  
\_H = display.contentHeight;  
\_W = display.contentWidth;  
  
local game = display.newGroup( );  
  
local player = celula.new({cellType="cell\_origen"});  
game:insert(player);  
  

This is throwing the following exemption in game:insert statement because it seems that nil is returned from the new function:

Runtime error
…tK0iCwFUOGTxnGoM+l4k+++TI/TemporaryItems/10/main.lua:24: bad argument #-2 to ‘insert’ (Proxy expected, got nil)
stack traceback:
[C]: ?

I compared with the code from the example but I cannot see differences that may cause this…

Thanks!. [import]uid: 8933 topic_id: 7648 reply_id: 27385[/import]

yes your celula_mt new() function does not return a display object, it returns a table

in your module, before the return, add in [lua]self.cell = cell[/lua]

then in your main file call

[lua]local player = celula.new({cellType=“cell_origen”});
game:insert(player.cell);[/lua]
[import]uid: 6645 topic_id: 7648 reply_id: 27387[/import]

yes your celula_mt new() function does not return a display object, it returns a table

in your module, before the return, add in [lua]self.cell = cell[/lua]

then in your main file call

[lua]local player = celula.new({cellType=“cell_origen”});
game:insert(player.cell);[/lua]
[import]uid: 6645 topic_id: 7648 reply_id: 27388[/import]

yes your celula_mt new() function does not return a display object, it returns a table

in your module, before the return, add in [lua]self.cell = cell[/lua]

then in your main file call

[lua]local player = celula.new({cellType=“cell_origen”});
game:insert(player.cell);[/lua]

whilst it’s fine for you to use “self” in your constructor (new) i tend not to.

i use it only in the table functions

eg in my module

[lua]function new()
local obj={}
local cell = diplay.newImage(“cell.png”)

– etc etc…

obj.cell = cell

setmetatable(obj, celula_mt)

return obj
end

function celula:setPos(x,y)

self.cell.x = x
self.cell.y = y
end[/lua]

in my main code

[lua]player:setPos(10,10)[/lua]

it’s essentially the same thing though. i just prefer to not override reserved keywords

also just to explain the : notation, from what i understand

[lua]player:setPos(10,10)[/lua] when calling [lua]function celula:setPos(x,y)[/lua] is essentially the same as using
[lua]player.setPos(player,10,10)[/lua] when calling [lua]function setPos(self,x,y)[/lua]

you can see that the : is a hidden way of passing in “self”

basically instance methods vs factory methods
http://lua-users.org/wiki/ColonForMethodCall

it’s in the docs anyway
http://developer.anscamobile.com/content/introduction#Object_Methods

anyway like i said, if you prefer to use “self” in your new() function it’s fine.

j [import]uid: 6645 topic_id: 7648 reply_id: 27390[/import]

Thanks for your explanations and time, your advice is really appreciated for a nooby like me, and hopefully for many people getting started with lua and Corona.

Wonderful community!. [import]uid: 8933 topic_id: 7648 reply_id: 27392[/import]