Module trouble

So I have a simple toolbar module, as below. I use it to create a toolbar object in my menu.lua file. It has a function that takes an item to add to the toolbar, but when I call toolbar:takeItem(crate) as an example, inside the module where crate should be taken on by the “item” paramater, it instead it seems to become bound to self, which is supposed to be the module display group.

– menu.lua

local composer = require( "composer" ) local scene = composer.newScene() local toolbar = require "toolbar" function scene:create( event ) local toolbar = toolbar:new() local crate = display.newImage("crate.png") crate.x, crate.y = 100, 200 --toolbar.takeItem(5) toolbar.takeItem(crate) end function scene:show( event ) local sceneGroup = self.view end function scene:hide( event ) local sceneGroup = self.view end function scene:destroy( event ) local sceneGroup = self.view end

– toolbar.lua

local toolbar = {} function toolbar:new() local self = display.newGroup() self.img = display.newImage("crate.png") self:insert(self.img) self.img.width, self.img.height = 300, 50 self.img.x, self.img.y = display.contentCenterX, 0 self.items = {} self.start = 10 self.xIncrement = 20 self.itemSize = self.xIncrement \* 0.9 function self:takeItem(item) print(self.x) print(item) end return self end return toolbar

hasty,

Hope this helps.

  1. Check out this link first, where it explains the use of ‘:’ or ‘.’  in defining functions in lua.

http://stackoverflow.com/questions/20667757/lua-modules-whats-the-difference-between-using-and-when-defining-func

  1. In menu.lua I think it best to not use the exact same ‘toolbar’ for requiring the module and then creating a local ‘toolbar’ handle to the module.  I usually differentiate them somehow. In the example I post below, I uppercase ‘Toolbar’ that requires the module. You could also do something like ‘toolBar’ and ‘toolbar’.

  2. I also figure toolbar is likely going to be needed in some of the other functions of the scene, so I declared it outside of scene:create and then defined in scene:create. Then scene:show can access it. However, if you only are needing it in scene:create then certainly declare and define it all in that function scene:create

  3. I use the dot in the function definition for the function ‘new’, but I have seen ‘:’  often used; it is just that you have to understand the ‘implied self argument’ that is in play when using the ‘:’ and then using a local ‘self’ within that function as you did will be an issue. For me, I try tho be very consistent in how I do it and I almost always use the dot, rather then the “:”.  Although others consistently use the ‘:’.

    local composer = require( “composer” ) local scene = composer.newScene() local Toolbar = require “toolbar” local toolbar function scene:create( event ) local crate = display.newImage(“crate.png”) crate.x, crate.y = 100, 200 toolbar = Toolbar.new() --toolbar.takeItem(5) toolbar.takeItem(crate) end function scene:show( event ) local sceneGroup = self.view end function scene:hide( event ) local sceneGroup = self.view end function scene:destroy( event ) local sceneGroup = self.view end

    local toolbar = {} function toolbar.new() local self = {} self.group = display.newGroup() local img = display.newImage(“crate.png”) self.group:insert(img) img.width, img.height = 300, 50 img.x, img.y = display.contentCenterX, 0 self.items = {} self.start = 10 self.xIncrement = 20 self.itemSize = self.xIncrement * 0.9 function self.takeItem(item) print(item.x) print(item) end return self end return toolbar

Good luck. Hope this helps.

Bob

This is really helpful, thanks. I’m still a bit foggy on a few points regarding the whole “self” business, but I think a little experimentation will be illuminating. On point number 2, yeah, I normally do similar, but I overlooked it on this occasion.

Thanks a lot!

hasty,

Hope this helps.

  1. Check out this link first, where it explains the use of ‘:’ or ‘.’  in defining functions in lua.

http://stackoverflow.com/questions/20667757/lua-modules-whats-the-difference-between-using-and-when-defining-func

  1. In menu.lua I think it best to not use the exact same ‘toolbar’ for requiring the module and then creating a local ‘toolbar’ handle to the module.  I usually differentiate them somehow. In the example I post below, I uppercase ‘Toolbar’ that requires the module. You could also do something like ‘toolBar’ and ‘toolbar’.

  2. I also figure toolbar is likely going to be needed in some of the other functions of the scene, so I declared it outside of scene:create and then defined in scene:create. Then scene:show can access it. However, if you only are needing it in scene:create then certainly declare and define it all in that function scene:create

  3. I use the dot in the function definition for the function ‘new’, but I have seen ‘:’  often used; it is just that you have to understand the ‘implied self argument’ that is in play when using the ‘:’ and then using a local ‘self’ within that function as you did will be an issue. For me, I try tho be very consistent in how I do it and I almost always use the dot, rather then the “:”.  Although others consistently use the ‘:’.

    local composer = require( “composer” ) local scene = composer.newScene() local Toolbar = require “toolbar” local toolbar function scene:create( event ) local crate = display.newImage(“crate.png”) crate.x, crate.y = 100, 200 toolbar = Toolbar.new() --toolbar.takeItem(5) toolbar.takeItem(crate) end function scene:show( event ) local sceneGroup = self.view end function scene:hide( event ) local sceneGroup = self.view end function scene:destroy( event ) local sceneGroup = self.view end

    local toolbar = {} function toolbar.new() local self = {} self.group = display.newGroup() local img = display.newImage(“crate.png”) self.group:insert(img) img.width, img.height = 300, 50 img.x, img.y = display.contentCenterX, 0 self.items = {} self.start = 10 self.xIncrement = 20 self.itemSize = self.xIncrement * 0.9 function self.takeItem(item) print(item.x) print(item) end return self end return toolbar

Good luck. Hope this helps.

Bob

This is really helpful, thanks. I’m still a bit foggy on a few points regarding the whole “self” business, but I think a little experimentation will be illuminating. On point number 2, yeah, I normally do similar, but I overlooked it on this occasion.

Thanks a lot!