If/Then/Else

New to Lua. Why doesn’t this work?
if a = 1 then
local title = display.newText( “Frog”)
elseif a = 0
local title = display.newText( “Dog”)
endif
This doesn’t work either

if a = 1 then
local title = display.newText( “Frog”)
else
local title = display.newText( “Dog”)
end

Thoughts?

Thanks! [import]uid: 115085 topic_id: 24679 reply_id: 324679[/import]

You are assigning a = 1
You need to do this:

[lua]if a == 1 then
local title = display.newText( “Frog”)
else
local title = display.newText( “Dog”)
end[/lua] [import]uid: 10389 topic_id: 24679 reply_id: 99964[/import]

Well, that sorta works. The right text shows up now, but when I click a button to move to another scene, it goes back to the start screen! That makes no sense!

If I take the if\then\else out and just have one local title, everything works fine! [import]uid: 115085 topic_id: 24679 reply_id: 99974[/import]

I’d need a bit more code to read before being able to assist with that new issue :slight_smile:

If you use < lua > and < /lua > around your code it will appear as mine does above :slight_smile: [import]uid: 10389 topic_id: 24679 reply_id: 99983[/import]

Yikes, that’s a lot of code and scenes!

Here’s what the terminal says…
Runtime error
/Users/niles/Corona/TextAdventure/room1.lua:194: ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’
stack traceback:
[C]: ?
[C]: in function ‘insert’
/Users/niles/Corona/TextAdventure/room1.lua:194: in function
?: in function ‘dispatchEvent’
?: in function ‘gotoScene’
/Users/niles/Corona/TextAdventure/startscreen.lua:117: in function
?: in function <?:215>
When I go to the scene room1, everything looks good. It resolves the variable like you showed me, but I get this error in the terminal. When I click on a button to go to another scene, all hell breaks loose and scenes start overlapping.

If I take the if\then\end out and just have one line of text (like I had initially) everything works fine!

[import]uid: 115085 topic_id: 24679 reply_id: 99990[/import]

Ok that’s the error. Now we need the code which causes the error, please :slight_smile: [import]uid: 10389 topic_id: 24679 reply_id: 99991[/import]

[lua]local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view

local bg = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
bg:setFillColor( 0 )

if silverkey == 1 then
local title = display.newText( “You are in what appears to be the master bedroom. The king-sized bed frame is all that’s left. The grand window on the north side overlooks the ocean. A small island can been seen in the distance.\n\nExits lead South and East.”, 0, 0, 320, 500, native.systemFont, 16 )
title:setTextColor( r, g, b )
else
local title = display.newText( “You are in what appears to be the master bedroom. The king-sized bed frame is all that’s left. The grand window on the north side overlooks the ocean. A small island can been seen in the distance.\n\nThere is a key here.\n\nExits lead South and East.”, 0, 0, 320, 500, native.systemFont, 16 )
title:setTextColor( r, g, b )
end

local northbutton = display.newRect(0, 0, 76, 30, 4)
northbutton.strokeWidth = 2
northbutton:setFillColor(0)
northbutton.x = display.contentWidth / 2
northbutton.y = display.contentHeight - 160

local ntitle = display.newText( “North”, 0, 0, native.systemFont, 16 )
ntitle:setTextColor( 255, 255, 255 )
ntitle:setReferencePoint( display.CenterReferencePoint )
ntitle.x = display.contentWidth / 2
ntitle.y = display.contentHeight - 160

function northbutton:tap( event )

local alert = native.showAlert( “You can’t go that way.”, “”, { “OK” },
onComplete )
end

northbutton:addEventListener( “tap”, northbutton )

local southbutton = display.newRect(0, 0, 76, 30, 4)
southbutton.strokeWidth = 2
southbutton:setFillColor(0)
southbutton.x = display.contentWidth / 2
southbutton.y = display.contentHeight - 55

local stitle = display.newText( “South”, 0, 0, native.systemFont, 16 )
stitle:setTextColor( 255, 255, 255 )
stitle:setReferencePoint( display.CenterReferencePoint )
stitle.x = display.contentWidth / 2
stitle.y = display.contentHeight - 55

function southbutton:tap( event )

storyboard.gotoScene( “room2” )
end

southbutton:addEventListener( “tap”, southbutton )

local westbutton = display.newRect(0, 0, 76, 30, 4)
westbutton.strokeWidth = 2
westbutton:setFillColor(0)
westbutton.x = display.contentWidth / 3
westbutton.y = display.contentHeight - 110

local wtitle = display.newText( “West”, 0, 0, native.systemFont, 16 )
wtitle:setTextColor( 255 )
wtitle:setReferencePoint( display.CenterReferencePoint )
wtitle.x = display.contentWidth / 3
wtitle.y = display.contentHeight - 110

function westbutton:tap( event )

local alert = native.showAlert( “You can’t go that way.”, “”, { “OK” },
onComplete )
end

westbutton:addEventListener( “tap”, westbutton )

local eastbutton = display.newRect(0, 0, 76, 30, 4)
eastbutton.strokeWidth = 2
eastbutton:setFillColor(0)
eastbutton.x = display.contentWidth / 1.5
eastbutton.y = display.contentHeight - 110

local etitle = display.newText( “East”, 0, 0, native.systemFont, 16 )
etitle:setTextColor( 255 )
etitle:setReferencePoint( display.CenterReferencePoint )
etitle.x = display.contentWidth / 1.5
etitle.y = display.contentHeight - 110

function eastbutton:tap( event )

storyboard.gotoScene( “room4” )
end

eastbutton:addEventListener( “tap”, eastbutton )

local upbutton = display.newRect(0, 0, 76, 30, 4)
upbutton.strokeWidth = 2
upbutton:setFillColor(0)
upbutton.x = display.contentWidth / 1.2
upbutton.y = display.contentHeight - 180

local utitle = display.newText( “Up”, 0, 0, native.systemFont, 16 )
utitle:setTextColor( 255 )
utitle:setReferencePoint( display.CenterReferencePoint )
utitle.x = display.contentWidth / 1.2
utitle.y = display.contentHeight - 180

function upbutton:tap( event )

local alert = native.showAlert( “You can’t go that way.”, “”, { “OK” },
onComplete )
end

upbutton:addEventListener( “tap”, upbutton )

local downbutton = display.newRect(0, 0, 76, 30, 4)
downbutton.strokeWidth = 2
downbutton:setFillColor(0)
downbutton.x = display.contentWidth / 1.2
downbutton.y = display.contentHeight - 35

local dtitle = display.newText( “Down”, 0, 0, native.systemFont, 16 )
dtitle:setTextColor( 255 )
dtitle:setReferencePoint( display.CenterReferencePoint )
dtitle.x = display.contentWidth / 1.2
dtitle.y = display.contentHeight - 35

function downbutton:tap( event )

local alert = native.showAlert( “You can’t go that way.”, “”, { “OK” },
onComplete )
end

downbutton:addEventListener( “tap”, downbutton )

local asbutton = display.newRect(0, 0, 76, 30, 4)
asbutton.strokeWidth = 2
asbutton:setFillColor(0)
asbutton.x = display.contentWidth / 5.8
asbutton.y = display.contentHeight - 180

local astitle = display.newText( “Assests”, 0, 0, native.systemFont, 16 )
astitle:setTextColor( 255 )
astitle:setReferencePoint( display.CenterReferencePoint )
astitle.x = display.contentWidth / 5.8
astitle.y = display.contentHeight - 180

function asbutton:tap( event )

local alert = native.showAlert( inv, “”, { “OK” },
onComplete )
end

asbutton:addEventListener( “tap”, asbutton )

local morebutton = display.newRect(0, 0, 76, 30, 4)
morebutton.strokeWidth = 2
morebutton:setFillColor(0)
morebutton.x = display.contentWidth / 5.8
morebutton.y = display.contentHeight - 35

local moretitle = display.newText( “More”, 0, 0, native.systemFont, 16 )
moretitle:setTextColor( 255 )
moretitle:setReferencePoint( display.CenterReferencePoint )
moretitle.x = display.contentWidth / 5.8
moretitle.y = display.contentHeight - 35

function morebutton:tap( event )

storyboard.gotoScene( “room1_2” )
end

morebutton:addEventListener( “tap”, morebutton )

– all objects must be added to group (e.g. self.view)
group:insert( bg )
group:insert( title )
group:insert( northbutton )
group:insert( ntitle )
group:insert( southbutton )
group:insert( stitle )
group:insert( westbutton )
group:insert( wtitle )
group:insert( eastbutton )
group:insert( etitle )
group:insert( upbutton )
group:insert( utitle )
group:insert( downbutton )
group:insert( dtitle )
group:insert( asbutton )
group:insert( astitle )
group:insert( morebutton )
group:insert( moretitle )

end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view

– Do nothing

end

– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view

– INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)

end

– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view

– INSERT code here (e.g. remove listeners, remove widgets, save state variables, etc.)

end


– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched whenever before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )


return scene[/lua]

I’m setting the silverkey and r, g and b variables in the main.lua.

[import]uid: 115085 topic_id: 24679 reply_id: 99994[/import]

Ouch.
That takes me back.
To about 1980, actually.

Leaving aside the code error for the moment…

If your game is going to reach any size (number of rooms/objects) then this approach is going to get painful very quickly.
An adventure game of this kind is much easier to write if you think in terms of objects.

A room object has

RoomNumber
Description
NorthTo
SouthTo
EastTo
WestTo
UpTo
DownTo
… plus maybe things like NeedsTorch and the like.

The NorthTo / SouthTo will hold a number if it is possible to travel in that direction, and the number is the room to which you can travel.

Change to the right room, display the description, wait for input.

Objects have

Description
Location (room number, or -1 if in the 'inventory)
IsEatable
IsBreakable
IsLightSource
IsTakable

You have
Location
IsHungry
IsThirsty
Inventory{} … an array of objects which you are carrying.

By manipulating those numbers, you can quickly get an engine up and running.
Once you have the basic mechanics, adding rooms and objects becomes simple.
[import]uid: 108660 topic_id: 24679 reply_id: 100004[/import]

I’ve already written this for Mac OS X, Windows, and Linux. I’m wanting to port this as closely as possible for speed and simplicity reasons.

Actually, my way does make sense. You simply make a template “scene” with your standard commands all set to the negative (You can’t go that way, there is nothing to read here, etc…) and a dummy description. You copy this for each “room” you make. then go in and make the changes. Worked well for my other 3 ports.

So, why does putting in the if/then/end blow this sucker up? [import]uid: 115085 topic_id: 24679 reply_id: 100058[/import]

With a quick glance, it looks like your “title” object is out of scope.

put:
local title = nil

at the top of your code, then take out the two “local” keywords when assigning to title. [import]uid: 21331 topic_id: 24679 reply_id: 100075[/import]

Awesome! That was it! Thank you!!!

Wonder why that didn’t matter without the if/then/end?

[import]uid: 115085 topic_id: 24679 reply_id: 100076[/import]

The reason it gave you an error is something called “variable scope”.

Lua, like most programming languages are based on blocks. In Lua they are called chunks.

A given block can see variables declared immediately inside it and its parent blocks. It cannot see inside of blocks that it contains.

Given this structure:

scene.lua:  
 local A  
  
 local function XXX()  
 local B  
  
 if X == Y then  
 local C  
 end  
 end  

The insides of function XXX can see variable A but it cannot see variable C.

Outside of function XXX, the program cannot see variable B nor C.

In side the if block, variable C is exposed as well as B and A.
In your case, you had “local” in front of the title variable inside the IF/THEN/ELSE blocks. (FYI, the THEN block can’t see the ELSE block either) which made them local to the block they were defined in.

Once the IF/THEN/ELSE was through, “title” was no longer in scope and was now an undefined variable which by default is nil.

So when you later did a:

group:insert(title)

an error occurred.

@TheRealTonyK’s advice created a variable “title” that was locally scoped to the whole scene file (making it available to every block within that file) and then when you take the ‘local’ off of the ones inside the “if” then they are now referencing the variable defined at the top of the file.
[import]uid: 19626 topic_id: 24679 reply_id: 100088[/import]