Modules and variables

I’m getting on quite well so far but prompted by another thread I’ve a few questions to ask to help get my head around a some lua concepts.

I’m bit confused by the workings of local variables. For example,

local M = {} local coordX = 10 function M.newCoordX( coordX ) coordX = coordX end return M

coordX is local to the module but it’s not part of the M table, so where does it actually exist? In the function above, can the parameter passed in to the function be the same name as the local variable, or does it have to be different?

When I add a local function, can it be added to M when it’s declared, or does it always have to be added separately afterwards as in the example below?

local M = {} local coordX = 10 function M.newCoordX( coordX ) coordX = coordX end local function incrementCoordX() coordX = coordX + 1 end M.incrementCoordX = incrementCoordX return M

I noticed sometimes I’ve forgotten to add the local function to the M table and it still works, so I’m definitely missing something here.

One final question. I’m not using self anywhere in my modules yet. Could you give me an example of where I would need to use it?

Many thanks,

Pete

coordX is a local variable and so it exists in the module. When you require a module, it will be loaded and cached. For a simple tutorial on the matter, you can check out http://lua-users.org/wiki/ModulesTutorial. This caching means that until you remove the module from the cache, all files where you require that specific module will in fact be working with the same version of the file. In other words, if you set  coordX to 20 from one file, it will be 20 when you check its value via other files as well.

Now, you can’t use coordX inside of the function like that because since you are passing an argument of the same name into the function, that argument will take precedent. You could just rename the argument to  newX and it’d work just fine.

You can declare a local function and then just add it to the table, but that essentially ends up being more work than just creating the function to the table from the start. Your line 5 basically does the same thing as your lines 9 and 13. The former is created to the table to begin with and the latter is first created and then added.

Now, self means you are actually working with methods. Methods are basically functions where the object itself, i.e. self, is always passed as an argument. For instance,

 

local M = {} function M.newRect( x, y, width, height ) local rect = display.newRect( x or 0, y or 0, width or 10, height or 10 ) return rect end function M.addMethod( object ) function object:getCoordinates() print( self.x, self.y ) end end return M

And then you’d use it like

local mod = require("myModule") local rect = mod.newRect( 200, 200 ) mod.addMethod( rect ) mod:getCoordinates() --\> 200, 200

Basically, you could use methods wherever you need to attach a function to some specific object, think of Corona’s :setFillColor() method, for instance. It could just as well be a function like display.setFillColor( object ), but it makes more sense to have it be a method, since you need to pass the object as an argument anyway and you are specifically working with that one object only at a time.

XeduR many thanks for the detailed reply, it’s starting to make a lot more sense. I think my confusion over local functions was due to initially declaring functions after they were called in the script.

Would it be right to say the local keywords before coordX and the function incrementCoordX should be removed, or do they make a difference in that context?

If you remove the “local” from in front of the variable and the function, then you just create globals, which is a big no no if there is no clear reason to do so (and in this case, there isn’t a reason for it). Just removing the “local” is different from adding them to the table to begin with.

The issue with coordX is simply about scope.

local coordX = 10 -- You are creating a local variable here -- Your function: function M.newCoordX( coordX ) -- You pass an argument of the same name to this function, which takes precedent. coordX = coordX -- All that simply happens here is "get argument, then set argument to argument". end -- How you should do it if you want to change the value of local coordX: function M.newCoordX( x ) coordX = x -- Now you pass argument x to the function and you set coordX to it. end

That makes sense XeduR, many thanks again.