Attempt to concatenate global 'sceneName' (a nil value)

I only have 2 scripts in my project, both have under 10 lines. One of them is named main.lua, and the other is scene1.lua. I’m trying to go from main.lua to scene1.lua right as the app opens, but I keep getting this error.

This is the code for main.lua

local composer = require("composer") local scene = composer.newScene("main") composer.gotoScene("scene1")

And this is the code for scene1.lua

local composer = require("composer") local scene = composer.newScene("scene1") display.newText{ text = "Hello" }

As I open the app, I can see the text in the top left corner saying “Hello” but instantly after I get this error:

Attempt to concatenate global ‘sceneName’ (a nil value)

File: ?

stack traceback:

?: in function ‘gotoScene’

main.lua:5: in main chunk

 

I’ve gone to the wiki, copy and pasted the exact example and replaced some code to work with my own but nothing works. Anyone?

1 Like

–Try this:

local composer = require(“composer”)

local scene = composer.newScene(“scene1”)

function scene:create(event)
local view = self.view
local text = display.newText{
text = “Hello”
}
view:insert(text)
end

function scene:destroy(event)
end

function scene:enter(event)
end

function scene:exit(event)
end

Runtime:addEventListener(“create”, scene)
Runtime:addEventListener(“destroy”, scene)
Runtime:addEventListener(“enter”, scene)
Runtime:addEventListener(“exit”, scene)

return scene

To explain Coder101’s answer…

First, main.lua is never a “scene”. It’s a place to initialize app-wide features and it’s a way to get to your first scene. Therefore main.lua could be as simple as:

local composer = require("composer") composer.gotoScene("scene1")

However, scene1.lua has to be a valid scene file. That is there is an expectation that you will require Composer. Create a new scene. Define four event functions and setup their handling functions. And finally, the module has to “return” the scene.

We offer a default scene template: https://docs.coronalabs.com/api/library/composer/index.html#scene-template

That if you base all your scene’s off of and only add to, you will always have a good base scene. This is the minimal set you can work with.

Rob

–Try this:

local composer = require(“composer”)

local scene = composer.newScene(“scene1”)

function scene:create(event)
local view = self.view
local text = display.newText{
text = “Hello”
}
view:insert(text)
end

function scene:destroy(event)
end

function scene:enter(event)
end

function scene:exit(event)
end

Runtime:addEventListener(“create”, scene)
Runtime:addEventListener(“destroy”, scene)
Runtime:addEventListener(“enter”, scene)
Runtime:addEventListener(“exit”, scene)

return scene

To explain Coder101’s answer…

First, main.lua is never a “scene”. It’s a place to initialize app-wide features and it’s a way to get to your first scene. Therefore main.lua could be as simple as:

local composer = require("composer") composer.gotoScene("scene1")

However, scene1.lua has to be a valid scene file. That is there is an expectation that you will require Composer. Create a new scene. Define four event functions and setup their handling functions. And finally, the module has to “return” the scene.

We offer a default scene template: https://docs.coronalabs.com/api/library/composer/index.html#scene-template

That if you base all your scene’s off of and only add to, you will always have a good base scene. This is the minimal set you can work with.

Rob