putting parameter with same name with local variable

this happened today while i was working on an app. I had already solved the problem, but i am curious if anyone can answer why  i can’t put the same name variable used in a table with local variable under the same scope

here’s the code

function \_.createSubMenuButton(group, x, y, width, height, param) --collect for parameter local world = param.world print(world) --create submenu button local subMenu = display.newRect(group, x, y, width, height) --putting properties subMenu.alpha = 0.5 subMenu.isHitTestable = true subMenu:addEventListener( 'tap', function() print('change scene',world) --[[par.world=world print("par",par.world)--]] dir.changeScene('subMenu', {world = world}) end) end

notice that if **dir.changeScene(‘subMenu’, {world = world}) **is coded, it will resulted in error, saying that the app is accessing nil value variable. BUT, if I change the left world to any name, it works.

any ideas? :slight_smile:

Interesting. For me, this code prints “something something” as expected

local x = "something" local tbl = {x = x} print(x, tbl.x)  

try to pass the variable between other files. probably passing in the same file won’t affect much. because this snippet code

dir.changeScene('subMenu', {world = world})

actually run into another .lua file, calling subMenu.lua

Gave it a try – can’t reproduce the issue. The below code also prints “something”:

main.lua

local other = require("other") local x = "something" other:someMethod({x = x})

other.lua

local other = {} function other:someMethod(someTable)     print(someTable.x) end return other

hmm. that is sure weird. here is the full code, just to be clear

–main.lua

dir = require('director') button = require('menuButton') \_w = display.contentWidth \_h = display.contentHeight wh = \_w\*0.5 hh = \_h\*0.5 \_sysLangStr = system.getPreference( 'ui', 'language' ) display.setStatusBar(display.HiddenStatusBar) if not string.find( 'de|fr|en|it|es', \_sysLangStr ) then \_sysLangStr = 'en' end dir.changeScene('mainMenu')  

–menuButton.lua

local \_={} function \_.createBackButton( group, x, y, width, height, param) --collect for parameter local world = param.world --create back button local backBut = display.newRect(group, x, y, width, height) --putting properties backBut.alpha = 0.5 backBut.isHitTestable = true backBut:addEventListener( 'tap', function() print('change scene') if world == 0 then dir.changeScene('mainMenu') end end) end function \_.createSubMenuButton(group, x, y, width, height, param) --collect for parameter local world = param.world local wName = param.wName local number = param.number --print(world) --create submenu button local subMenu = display.newRect(group, x, y, width, height) --putting properties subMenu.alpha = 0.5 subMenu.isHitTestable = true subMenu:addEventListener( 'tap', function() print('change scene') dir.changeScene('subMenu'..number, {worlds = world, worldName = wName}) end) end return \_

–director.lua

local \_={} local sceneName = 'main' local sceneGroup function \_.changeScene( newScene, par ) display.remove(sceneGroup) sceneGroup = nil --purging all previous group package.loaded[sceneName] = nil -- unload the module of the scene collectgarbage() collectgarbage() sceneGroup = require(newScene).new(par) sceneName = newScene end function \_.getSceneName() return sceneName end return \_

–mainMenu.lua

\_.new = function(params) local group = display.newGroup() local num = 1 --which subMenu identified with numbers --creating background local bgimg = display.newImage( group,'0141MULTI-Wimmel\_Puzzle/1-Design/2-Final/01\_mainmenu/ipad/mainmenu\_'..\_sysLangStr..'\_ipad.jpg', wh, hh) --setting up size and coordinates defX = 215 defY = 265 defWid = 250 defHei = 190 --creating submenu button button.createSubMenuButton(group, defX, defY, defWid, defHei, {world = 1, wName = 'bauernhof', number = num}) button.createSubMenuButton(group, defX+287, defY, defWid, defHei, {world = 2, wName = 'hafen', number = num}) button.createSubMenuButton(group, defX+574, defY, defWid, defHei, {world = 3, wName = 'polizei', number = num}) button.createSubMenuButton(group, defX, defY+252, defWid, defHei, {world = 4, wName = 'jahrmarkt', number = num}) button.createSubMenuButton(group, defX+287, defY+252, defWid, defHei, {world = 5, wName = 'spielplatz', number = num}) button.createSubMenuButton(group, defX+574, defY+252, defWid, defHei, {world = 6, wName = 'zoo', number = num}) return group end return \_

here’s the big picture: every time I want to change into a new scene, I use director.lua to direct into other .lua files. Each of the menu button is created through the menuButton.lua, so the creation method can be useable over time. the problem is when i try to use a menu button creation (using main menu as example) it get stuck when the variable name is like,

dir.changeScene('subMenu', {world = world})

Interesting. For me, this code prints “something something” as expected

local x = "something" local tbl = {x = x} print(x, tbl.x)  

try to pass the variable between other files. probably passing in the same file won’t affect much. because this snippet code

dir.changeScene('subMenu', {world = world})

actually run into another .lua file, calling subMenu.lua

Gave it a try – can’t reproduce the issue. The below code also prints “something”:

main.lua

local other = require("other") local x = "something" other:someMethod({x = x})

other.lua

local other = {} function other:someMethod(someTable)     print(someTable.x) end return other

hmm. that is sure weird. here is the full code, just to be clear

–main.lua

dir = require('director') button = require('menuButton') \_w = display.contentWidth \_h = display.contentHeight wh = \_w\*0.5 hh = \_h\*0.5 \_sysLangStr = system.getPreference( 'ui', 'language' ) display.setStatusBar(display.HiddenStatusBar) if not string.find( 'de|fr|en|it|es', \_sysLangStr ) then \_sysLangStr = 'en' end dir.changeScene('mainMenu')  

–menuButton.lua

local \_={} function \_.createBackButton( group, x, y, width, height, param) --collect for parameter local world = param.world --create back button local backBut = display.newRect(group, x, y, width, height) --putting properties backBut.alpha = 0.5 backBut.isHitTestable = true backBut:addEventListener( 'tap', function() print('change scene') if world == 0 then dir.changeScene('mainMenu') end end) end function \_.createSubMenuButton(group, x, y, width, height, param) --collect for parameter local world = param.world local wName = param.wName local number = param.number --print(world) --create submenu button local subMenu = display.newRect(group, x, y, width, height) --putting properties subMenu.alpha = 0.5 subMenu.isHitTestable = true subMenu:addEventListener( 'tap', function() print('change scene') dir.changeScene('subMenu'..number, {worlds = world, worldName = wName}) end) end return \_

–director.lua

local \_={} local sceneName = 'main' local sceneGroup function \_.changeScene( newScene, par ) display.remove(sceneGroup) sceneGroup = nil --purging all previous group package.loaded[sceneName] = nil -- unload the module of the scene collectgarbage() collectgarbage() sceneGroup = require(newScene).new(par) sceneName = newScene end function \_.getSceneName() return sceneName end return \_

–mainMenu.lua

\_.new = function(params) local group = display.newGroup() local num = 1 --which subMenu identified with numbers --creating background local bgimg = display.newImage( group,'0141MULTI-Wimmel\_Puzzle/1-Design/2-Final/01\_mainmenu/ipad/mainmenu\_'..\_sysLangStr..'\_ipad.jpg', wh, hh) --setting up size and coordinates defX = 215 defY = 265 defWid = 250 defHei = 190 --creating submenu button button.createSubMenuButton(group, defX, defY, defWid, defHei, {world = 1, wName = 'bauernhof', number = num}) button.createSubMenuButton(group, defX+287, defY, defWid, defHei, {world = 2, wName = 'hafen', number = num}) button.createSubMenuButton(group, defX+574, defY, defWid, defHei, {world = 3, wName = 'polizei', number = num}) button.createSubMenuButton(group, defX, defY+252, defWid, defHei, {world = 4, wName = 'jahrmarkt', number = num}) button.createSubMenuButton(group, defX+287, defY+252, defWid, defHei, {world = 5, wName = 'spielplatz', number = num}) button.createSubMenuButton(group, defX+574, defY+252, defWid, defHei, {world = 6, wName = 'zoo', number = num}) return group end return \_

here’s the big picture: every time I want to change into a new scene, I use director.lua to direct into other .lua files. Each of the menu button is created through the menuButton.lua, so the creation method can be useable over time. the problem is when i try to use a menu button creation (using main menu as example) it get stuck when the variable name is like,

dir.changeScene('subMenu', {world = world})