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