Linking local functions... global vs. local scope

Hi, I’m still relatively new to lua and I was wondering if there is a way for local functions to “talk?” to each other.

Currently I am planning to use the ‘ListView’ widget but within a function, however the widget requires seperate functions.

E.g. 

function easyGameMode() --Create a group to hold our widgets & images local widgetGroup = display.newGroup() -- Create a background to go behind our tableView local background = display.newImage( widgetGroup, "Assets/bg.png", true ) -- The gradient used by the title bar local titleGradient = graphics.newGradient( { 255, 255, 255, 255 }, { 139, 157, 180, 255 }, "down" ) -- Create toolbar to go at the top of the screen local titleBar = display.newRect( 0, 0, display.contentWidth, 32 ) titleBar.y = display.statusBarHeight + ( titleBar.contentHeight \* 0.5 ) titleBar:setFillColor( titleGradient ) titleBar.y = display.screenOriginY + titleBar.contentHeight \* 0.5 -- create embossed text to go on toolbar local titleText = display.newEmbossedText( "My List", 0, 0, native.systemFontBold, 20 ) titleText:setReferencePoint( display.CenterReferencePoint ) titleText:setTextColor( 255 ) titleText.x = 160 titleText.y = titleBar.y -- create a shadow underneath the titlebar (for a nice touch) local shadow = display.newImage( "Assets/shadow.png" ) shadow:setReferencePoint( display.TopLeftReferencePoint ) shadow.x, shadow.y = 0, titleBar.y + titleBar.contentHeight \* 0.5 shadow.xScale = 320 / shadow.contentWidth shadow.alpha = 0.45 --Text to show which item we selected local itemSelected = display.newText( "You selected item ", 0, 0, native.systemFontBold, 28 ) itemSelected.x = display.contentWidth + itemSelected.contentWidth \* 0.5 itemSelected.y = display.contentCenterY widgetGroup:insert( itemSelected ) -- Forward reference for our back button & tableview local backButton, list -- Create a tableView list = widget.newTableView { top = 38, width = 320, height = 448, hideBackground = true, maskFile = "Assets/mask-320x448.png", onRowRender = onRowRender, onRowTouch = onRowTouch, } --Insert widgets/images into a group widgetGroup:insert( list ) widgetGroup:insert( titleBar ) widgetGroup:insert( titleText ) widgetGroup:insert( shadow ) --Create the back button backButton = widget.newButton { width = 298, height = 56, label = "Back", labelYOffset = - 1, onRelease = onBackRelease } backButton.alpha = 0 backButton.x = display.contentCenterX backButton.y = display.contentHeight - backButton.contentHeight widgetGroup:insert( backButton ) -- insert rows into list (tableView widget) for i = 1, 20 do list:insertRow { height = 72, rowColor = { default = { 255, 255, 255, 0 }, }, } end end -- Handle row rendering function onRowRender( event ) local phase = event.phase local row = event.row local rowTitle = display.newText( row, "List item " .. row.index, 0, 0, native.systemFontBold, 16 ) rowTitle.x = row.x - ( row.contentWidth \* 0.5 ) + ( rowTitle.contentWidth \* 0.5 ) rowTitle.y = row.contentHeight \* 0.5 local rowArrow = display.newImage( row, "Assets/rowArrow.png", false ) rowArrow.x = row.x + ( row.contentWidth \* 0.5 ) - rowArrow.contentWidth rowArrow.y = row.contentHeight \* 0.5 end

Originally “function onRowRender( event )” was local, but it wouldn’t work. Perhaps this is possible with multiple .lua files?

It’s a matter of the order that  you declare things.

widget.tableView {

    onRowRender=myRowRender

}

 

local function myRowRender()

end

then when the compiler first see’s “myRowRender” in the widget call, it has no clue what that is.  However if you do this:

local function myRowRender()

end

 

widget.newTableView {

      onRowRender = myRowRender

}

then it will know that myRowReder() is a function and that it has a pointer to be assigned.  So order matters.  If you get caught where you have to put the function after when you first use it (some times happens with functions need to call each other) then you do this:

 

local myRowRender

 

widget.newTableView {

     onRowRender = myRowRender

}

 

myRowRender = function()

end

Aha, okay! Thank you so much.

It’s a matter of the order that  you declare things.

widget.tableView {

    onRowRender=myRowRender

}

 

local function myRowRender()

end

then when the compiler first see’s “myRowRender” in the widget call, it has no clue what that is.  However if you do this:

local function myRowRender()

end

 

widget.newTableView {

      onRowRender = myRowRender

}

then it will know that myRowReder() is a function and that it has a pointer to be assigned.  So order matters.  If you get caught where you have to put the function after when you first use it (some times happens with functions need to call each other) then you do this:

 

local myRowRender

 

widget.newTableView {

     onRowRender = myRowRender

}

 

myRowRender = function()

end

Aha, okay! Thank you so much.