Simulator Misbehaving

My simulator is misbehaving. I have a main.lua that calls three other files Setup.lua , Background.lua and PlayScene.lua . There is also a file called Functions.lua . My testing was progressing well when all of a sardine I got a Runtime Error. The simulator said it couldn’t perform arithmetic on a nil value. This field is setup in Setup.lua .It appears that the simulator is skipping directly to the PlayScene.lua without performing the Setup.lua and Background.lua. I tried to fool the simulator by changing the folder name as if I were starting a new project, same result. I then commented out the Background.lua and PlayScene.lua in main.lua . The simulator give the same result as if it is going directly to the PlayScene.lua called by main.lua, even althoug this line is now commented out.

How can I perhaps kill the history of the simulator or get around this problem?

Hi @PopsHugo,

If you ever need to “clear out” a project from the Sandbox, you can do it by going to (from the Simulator):

File > Show Project Sandbox

Then locate the folder and trash it.

However, this sounds more like a scoping issue or something else in your code. Are you using Storyboard or Director? Are you using global variables in places?

Brent

I agree, I was just about to write that this sounds like a scoping issue.  PopsHugo, is the field that you’re referring to a global variable (declared without “local” in front), or is it local to the Setup.lua module where you set it up?

Also, when you see the simulator error about performing arithmetic on a nil value, does it indicate the file and line number causing the error (it usually does)?  The reason I ask is that, if you’ve commented out the loading of the Background.lua and PlayScene.lua files, then I would suspect the error isn’t happening there at all (those files, for all intents and purposes, don’t exist).

  • Andrew

Hi Brent & Andrew, following your comments I have now commented out the line in main.lua that calls Setup.lua , so that there is effectively nothing in main.lua. The simulator still gives me a Runtime Error and references the code lines in PlayScene.lua which is also commented out. 

Any suggestions?

Hmm, that sounds very strange.  So your main.lua file is more or less completely blank (the lines that require the other modules are all commented out)?  Brent’s question is also a good one – are you using Storyboard or the Director class to move between scenes?

If it’s short, it might also be helpful if you copy/pasted your main.lua file directly into this thread so we can take a look.

  • Andrew

HI Andrew,

I am not using Storyboard or Director class.

My main.lua is as follows before I commented out the last three lines.

– DoDec3 main 

require (“Setup”)

require (“Functions”)

require (“PlayScene”)

Setup()

Background(0,255,0)

PlayScene()

Setup.lua is as follows:

require (“Tables”)

require (“Functions”)

function Setup()

    display.setStatusBar( display.HiddenStatusBar ) – Hide the Status Bar

– Setup Initial colors 

    ResetColors()

    SOSx = display.screenOriginX + 25 – Screen Offset x

    SOSy = display.screenOriginY + 50 – Screen Offset y

    ScH = display.viewableContentHeight – Viewable Content Height

    ScW = display.viewableContentWidth  – Viewable Content Width

    SF = 510

    print(“Setup” ,ScH, SOSx, SF, ScW,SOSy)

    SFx = (ScH - SOSx) / SF  – Scaling Factor x

    SFy = (ScW - SOSy) / SF  – Scaling Factor y

    DotDiam = 5 * SFx – Default dot diameter

    SW = 5 – Default line thickness

    nSeconds = 0

    Tap1 = 0

    Tap2 = 0

    SFlag = false

    CompleteFlag = false

    PlayButtonx = 250 * SFx + SOSx

    PlayButtony = 10 * SFx + SOSx

    PlayButtonW = 50

    PlayButtonH = 110

    RestartButtonx = 250 * SFx + SOSx

    RestartButtony = 70 * SFx + SOSx

    RestartButtonW = 50

    RestartButtonH = 110

    QuitButtonx = 250 * SFx + SOSx

    QuitButtony = 130 * SFx + SOSx

    QuitButtonW = 50

    QuitButtonH = 110

end

the first portion of PlayScene.lua is as follows:

require(“Functions”)

require(“ExitScene”)

local widget = require (“widget”)

local PlayButton = widget.newButton{

    id = “PlayBtn”,

    left = 100 * SFx + SOSx,

    top = 50 * SFy + SOSy,

    label = “Play”,

    width = 150, height = 28,

    cornerRadius = 8,

    onEvent = onPlayButtonEvent

}

The Runtime Erroe references line 7 in PlayScene.lua  :   left = 100 * SFx + SOSx,

But this doesn’t explain why the simulator is going directly to PlayScene.lua when I have the lines in main.lua commented out as follows:

– DoDec3 main 

require (“Setup”)

require (“Functions”)

require (“PlayScene”)

–Setup()

–Background(0,255,0)

–PlayScene()

I also have no " File > Show Project Sandbox" in the Simulator. The release I am using is Build: 2012:971

Any suggestions?

Have you commented out ‘require (“PlayScene”)’ ?

When you require a file, it immediately runs all the code in that file. Because the function setup() within setup lua has not been fired, the values SFx and SOSx that PlayScene.lua is trying to use are still nil.

You seem to be trying to run the setup.lua and playScene.lua files by calling them with setup() and playScene(), which is not correct. 

I’d recommend using storyboard for switching between scenes, it gives you a nice structure to work from and makes things a whole lot easier. 

Hi Nick,

I hear what you say, but why did this work previously and then all of a sardine stop working?

Also require (“Setup”) appears before require (“PlayScene”)  and Setup() appears before PlayScene() so the simulator should have run through the Setup code before the PlayScene code. 

Any comments?

I may be confused trying to get what is going on in your files. You have a function call for PlayScene() but that function isn’t shown in your code on your post. You only show playscene.lua with a button being created. If is not enclosed in a function, the button is created as soon as you require playscene.lua. Could this be your problem or you just not posting the function playscene() portion?

Hi @PopsHugo,

If you ever need to “clear out” a project from the Sandbox, you can do it by going to (from the Simulator):

File > Show Project Sandbox

Then locate the folder and trash it.

However, this sounds more like a scoping issue or something else in your code. Are you using Storyboard or Director? Are you using global variables in places?

Brent

I agree, I was just about to write that this sounds like a scoping issue.  PopsHugo, is the field that you’re referring to a global variable (declared without “local” in front), or is it local to the Setup.lua module where you set it up?

Also, when you see the simulator error about performing arithmetic on a nil value, does it indicate the file and line number causing the error (it usually does)?  The reason I ask is that, if you’ve commented out the loading of the Background.lua and PlayScene.lua files, then I would suspect the error isn’t happening there at all (those files, for all intents and purposes, don’t exist).

  • Andrew

Hi Brent & Andrew, following your comments I have now commented out the line in main.lua that calls Setup.lua , so that there is effectively nothing in main.lua. The simulator still gives me a Runtime Error and references the code lines in PlayScene.lua which is also commented out. 

Any suggestions?

Hmm, that sounds very strange.  So your main.lua file is more or less completely blank (the lines that require the other modules are all commented out)?  Brent’s question is also a good one – are you using Storyboard or the Director class to move between scenes?

If it’s short, it might also be helpful if you copy/pasted your main.lua file directly into this thread so we can take a look.

  • Andrew

HI Andrew,

I am not using Storyboard or Director class.

My main.lua is as follows before I commented out the last three lines.

– DoDec3 main 

require (“Setup”)

require (“Functions”)

require (“PlayScene”)

Setup()

Background(0,255,0)

PlayScene()

Setup.lua is as follows:

require (“Tables”)

require (“Functions”)

function Setup()

    display.setStatusBar( display.HiddenStatusBar ) – Hide the Status Bar

– Setup Initial colors 

    ResetColors()

    SOSx = display.screenOriginX + 25 – Screen Offset x

    SOSy = display.screenOriginY + 50 – Screen Offset y

    ScH = display.viewableContentHeight – Viewable Content Height

    ScW = display.viewableContentWidth  – Viewable Content Width

    SF = 510

    print(“Setup” ,ScH, SOSx, SF, ScW,SOSy)

    SFx = (ScH - SOSx) / SF  – Scaling Factor x

    SFy = (ScW - SOSy) / SF  – Scaling Factor y

    DotDiam = 5 * SFx – Default dot diameter

    SW = 5 – Default line thickness

    nSeconds = 0

    Tap1 = 0

    Tap2 = 0

    SFlag = false

    CompleteFlag = false

    PlayButtonx = 250 * SFx + SOSx

    PlayButtony = 10 * SFx + SOSx

    PlayButtonW = 50

    PlayButtonH = 110

    RestartButtonx = 250 * SFx + SOSx

    RestartButtony = 70 * SFx + SOSx

    RestartButtonW = 50

    RestartButtonH = 110

    QuitButtonx = 250 * SFx + SOSx

    QuitButtony = 130 * SFx + SOSx

    QuitButtonW = 50

    QuitButtonH = 110

end

the first portion of PlayScene.lua is as follows:

require(“Functions”)

require(“ExitScene”)

local widget = require (“widget”)

local PlayButton = widget.newButton{

    id = “PlayBtn”,

    left = 100 * SFx + SOSx,

    top = 50 * SFy + SOSy,

    label = “Play”,

    width = 150, height = 28,

    cornerRadius = 8,

    onEvent = onPlayButtonEvent

}

The Runtime Erroe references line 7 in PlayScene.lua  :   left = 100 * SFx + SOSx,

But this doesn’t explain why the simulator is going directly to PlayScene.lua when I have the lines in main.lua commented out as follows:

– DoDec3 main 

require (“Setup”)

require (“Functions”)

require (“PlayScene”)

–Setup()

–Background(0,255,0)

–PlayScene()

I also have no " File > Show Project Sandbox" in the Simulator. The release I am using is Build: 2012:971

Any suggestions?

Have you commented out ‘require (“PlayScene”)’ ?

When you require a file, it immediately runs all the code in that file. Because the function setup() within setup lua has not been fired, the values SFx and SOSx that PlayScene.lua is trying to use are still nil.

You seem to be trying to run the setup.lua and playScene.lua files by calling them with setup() and playScene(), which is not correct. 

I’d recommend using storyboard for switching between scenes, it gives you a nice structure to work from and makes things a whole lot easier. 

Hi Nick,

I hear what you say, but why did this work previously and then all of a sardine stop working?

Also require (“Setup”) appears before require (“PlayScene”)  and Setup() appears before PlayScene() so the simulator should have run through the Setup code before the PlayScene code. 

Any comments?

I may be confused trying to get what is going on in your files. You have a function call for PlayScene() but that function isn’t shown in your code on your post. You only show playscene.lua with a button being created. If is not enclosed in a function, the button is created as soon as you require playscene.lua. Could this be your problem or you just not posting the function playscene() portion?