help with modules

hello guys i need a help with module in lua, i’m newbie with programming.

i have a scene.lua where i can get a player table. like this:

local controller = require("controller")

player = {}

player.name = “player”

As its possible to see i have a modules controller.lua that helps me 

now comes the problem … i used i touch function to get touch x and y but since the code was becaming big i decided to create a controler script to it, and even my player table been global  the controler cant get it here is the code:

\_c = {} function \_c.onTouch(event) local tx = event.x local ty = event.y print(player.name) end return \_c

when i touch the print result is “nil”. i already try change the listener to create a function inside another like this:

Runtime:addEventListener("touch", function() controller.onTouch(event,player) end)

to see if i can pass player as parameter but that dint solve the  problem.

I would like if possible some explanation to learn how pass the variable in the right way through modules.

If you’re going to do this:

Runtime:addEventListener("touch", function() controller.onTouch(event,player) end)

First of all don’t. You cannot use anonymous functions with addEventListener() if you plan to remove them later with removeEventListener(). Instead do:

local function passPlayer( event, player )      controller.onTouch( event, player ) end Runtime:addEventListener("touch", passPlayer )

Now your controller function doesn’t accept more than one parameter, it should be re-written to be:

local \_c = {}  -- no need to make this global function \_c.onTouch( event, player )     local tx = event.x     local ty = event.y     print(player.name) end return \_c

Now player doesn’t need to be global.

Rob

Sorry i dint undestood , how i will pass this function on the runtime?

in your example and as i was able to see until now i cant pass extra parameters

Runtime:addEventListener("touch", passPlayer )

this up , has no extra parameters … so how i can use that code on other script file?

and also … there will be some situations where i need to use globals, so i still didnt undertood why the global was been seen as “nil” since it exists and has data on it.

You’re right, you can’t pass in extra parameters. Is there a reason you’re putting the touch on the Runtime instead of on each object?

yes, since is a RPG game i need the player to move, so if you click to left he goes to left , click to right goes right … etc no matter where on the left or right or top or down on the screen … this will make him walk only one step on that direction, but before i need check on 2 maps … on collision map to see if is walkable and monster map to see if is a monster… only then i can move (or attack if is the case)

of course i can make this kind of control on the sema lua scene but that will not be the only kind of control that will need extra parameters … as i told before the enemies will also have to do some stuff when became theirs turns.

Learn how implement that kind of passing parameters will be necessary on other situations also.

thats why i move the code for modules, so my scene will be cleaner … but i also cant get the parameters … im not good on programming but its like the code goes through the scene.lua to the “module.lua” but then comes back as “nil”.

Thats strange because the logic works very well on LOVE2D from where i’m trying to port the code.

any good idea how to change data between modules?

by modules i mean require(“module”) … but maybe i’m using the wrong expression.

How many player objects will you have at once?

You could just scope player that it’s visible to the touch handler and access player directly.

Rob

yeah that what is happening but i’m using a flag called “turn”

if turn == 1 then -- player action ( just to remember that player = list[1]) else -- list[turn] action end

If you’re going to do this:

Runtime:addEventListener("touch", function() controller.onTouch(event,player) end)

First of all don’t. You cannot use anonymous functions with addEventListener() if you plan to remove them later with removeEventListener(). Instead do:

local function passPlayer( event, player )      controller.onTouch( event, player ) end Runtime:addEventListener("touch", passPlayer )

Now your controller function doesn’t accept more than one parameter, it should be re-written to be:

local \_c = {}  -- no need to make this global function \_c.onTouch( event, player )     local tx = event.x     local ty = event.y     print(player.name) end return \_c

Now player doesn’t need to be global.

Rob

Sorry i dint undestood , how i will pass this function on the runtime?

in your example and as i was able to see until now i cant pass extra parameters

Runtime:addEventListener("touch", passPlayer )

this up , has no extra parameters … so how i can use that code on other script file?

and also … there will be some situations where i need to use globals, so i still didnt undertood why the global was been seen as “nil” since it exists and has data on it.

You’re right, you can’t pass in extra parameters. Is there a reason you’re putting the touch on the Runtime instead of on each object?

yes, since is a RPG game i need the player to move, so if you click to left he goes to left , click to right goes right … etc no matter where on the left or right or top or down on the screen … this will make him walk only one step on that direction, but before i need check on 2 maps … on collision map to see if is walkable and monster map to see if is a monster… only then i can move (or attack if is the case)

of course i can make this kind of control on the sema lua scene but that will not be the only kind of control that will need extra parameters … as i told before the enemies will also have to do some stuff when became theirs turns.

Learn how implement that kind of passing parameters will be necessary on other situations also.

thats why i move the code for modules, so my scene will be cleaner … but i also cant get the parameters … im not good on programming but its like the code goes through the scene.lua to the “module.lua” but then comes back as “nil”.

Thats strange because the logic works very well on LOVE2D from where i’m trying to port the code.

any good idea how to change data between modules?

by modules i mean require(“module”) … but maybe i’m using the wrong expression.

How many player objects will you have at once?

You could just scope player that it’s visible to the touch handler and access player directly.

Rob

yeah that what is happening but i’m using a flag called “turn”

if turn == 1 then -- player action ( just to remember that player = list[1]) else -- list[turn] action end