Another module question

I am using the modified Director Class that removed the module function: https://gist.github.com/1201974

Getting into a bit of a muddle with structuring my code and scoping

I have a scene game.lua

In this scene I create 3 main display groups as follows:

  
local keyFramesClass = require("keyFrames")  
  
M.new = function ()  
  
 local localGroup = display.newGroup()  
  
 local backgroundGroup = display.newGroup()  
 local foregroundGroup = display.newGroup()  
 local interfaceGroup = display.newGroup()  
  
 localGroup:insert(backgroundGroup)  
 localGroup:insert(foregroundGroup)  
 localGroup:insert(interfaceGroup)  
  
 displayKeyFrame("startGame")  
  

I want to add a graphic to one of these display groups from another “module” called keyFrames.lua

local difficultyLevelPanelClass= require("difficultyLevelPanel")  
  
function displayKeyFrame(passedKeyFrameID)  
  
  
  
 if (passedKeyFrameID == "startGame") then  
  
 local difficultyLevelPanel = difficultyLevelPanelClass.newDifficultyLevelPanel()  
 foregroundGroup:insert(difficultyLevelPanel)  
  
  

I am getting the following error

attempt to index global ‘foregroundGroup’ (a nil value)

Any ideas what I am doing wrong here?
[import]uid: 7863 topic_id: 16067 reply_id: 316067[/import]

it is all about the scope, you do not have the *local* variable foregroundGroup passed to the module keyFrames.lua

To fix your problem

change line 3 as

function displayKeyFrame(passedKeyFrameID, foregroundGroup)
now it will work for you when you call it from line 15 as

displayKeyFrame(“startGame”, foregroundGroup)

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16067 reply_id: 59742[/import]

Cheers for the swift reply Jay. That works well.

I assume that if I had made the 3 display groups global by removing the local prefix that I could access them from all my modules? Would this be a big no no?

Passing the path to the foreground in could be problematic as I call the displayKeyFrame function from other modules such as one called myGameUtils.lua

main.lua

require("myGameUtils")  

myGameUtils.lua

[code]

function nextKeyframe()

local nextKeyframe = gameState:getState(“audioCompleteDestination”)

displayKeyFrame(nextKeyframe)

end
function NarrationFinished(event)

if event.completed then

nextKeyframe()

else
print(“Narration was stopped before completion”)
end

end
function stopAllSounds()

audio.stop()

end

function playAudio(audioId, destination, playOrStop)

gameState:setState(“audioCompleteDestination”,destination)

local narrationSpeech = audio.loadStream(audioId)

narrationChannel = audio.play( narrationSpeech, { duration=30000, onComplete=NarrationFinished })
end
[/code] [import]uid: 7863 topic_id: 16067 reply_id: 59743[/import]

This answer is good. Now I know how to do it with my module.

KC [import]uid: 94613 topic_id: 16067 reply_id: 65348[/import]

I’m trying to do the same thing, but my external module looks like this:

  
myFunctions = {   
  
 createObject = function()   
  
 local object = myImage.loadImage("object.png")   
 object.x = \_W + 20  
 object.y = 200  
 object.Index = #object + 1  
 physics.addBody( object, "dynamic")  
  
 return object  
  
 end  
  
  
 }  
  

How would I insert the object created into a group that is in another module? [import]uid: 40538 topic_id: 16067 reply_id: 65509[/import]