Splitting your code in to tabs

So I wanted to make a game that was split into 2 tabs: TitleScreen and Game (You can probably guess what each one is). I initially wanted it so that if you pressed the start button in the titlescreen tab, the console would then run the Game() tab. For help, I looked at a codea discussion that basically showed how you could split your code into tabs. Forum discussion link: http://codea.io/talk/discussion/210/file-managementdistributing-code-over-tabs-answered/p1

This gave me a basic utility and Idea of how to set up such a system, but there was a part of it that I wanted to edit.
This is the part of the code from the discussion that needed editing (the main tab’s code):

-- Main
function setup()
    scenes = {}
    scenes[1] = Test1()
    scenes[2] = Test2()

    -- SceneIndex parameter from 1 to number of scenes
    iparameter("SceneIndex",1,#scenes)
end

function draw()
    scenes[SceneIndex]:draw()
end

function touched(touch)
    scenes[SceneIndex]:touched(touch)
end

I wanted to change it so that I could have a global variable that you could change to change what tab the console is running instead of a parameter (so if x==1, then the console would run the TitleScreen tab. And if x==2, the console would run the game() tab). Unfortunately, as small as this code is, I could not entirely wrap my head around how the Scene[#] changes and also how the touched() function contributes to the code. Eventually I settled upon making a generic for loop that will only draw the scenes[] code if the table content selected == x (so that I could have the x=TitleScreen() and it would run the title screen tab or I could have x=Game() and it would run the game tab). As you would expect, this code is not working (for some reason).
Code for the Main tab:

function setup()
    scenes = {}
    scenes[1] = TitleScreen()
    scenes[2] = Game()
    x=TitleScreen()
    -- SceneIndex parameter from 1 to number of scenes
end
for i,z in pairs(scenes) do
if scenes[z] == x then
function draw()
    scenes[x]:draw()
end
end
end

Code for the TitleScreen tab (To show how me selecting “Start Game” should affect what tab the console runs):

TitleScreen = class()
function TitleScreen:init()
    rectMode(CORNER)
    spriteMode(CORNER)
    textMode(CENTER)
    st=false
    cr=false
    hs=false
end

function TitleScreen:draw()   
    sprite("SpaceCute:Background",0,0,WIDTH,HEIGHT)
    stroke(0, 231, 255, 255)
    strokeWidth(5)
    fill(8, 255, 0, 255)
    if st then
        fill(11, 0, 255, 255)
    end  
    rect(WIDTH/2-100,HEIGHT/2+125,200,50)

    fill(8, 255, 0, 255)
    if cr then
        fill(11, 0, 255, 255)
    end    
    rect(WIDTH/2-100,HEIGHT/2-25,200,50)

    fill(8, 255, 0, 255)
    if hs then
        fill(11, 0, 255, 255)
    end    
    rect(WIDTH/2-100,HEIGHT/2-175,200,50)

    fill(255, 0, 22, 255)
    text("Start Game",WIDTH/2,HEIGHT/2+150)
    text("Credits",WIDTH/2,HEIGHT/2)
    text("HighScores",WIDTH/2,HEIGHT/2-150)
    textSize(150,30)
end

function touched(touch)
    if touch.state == BEGAN then
        if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and 
            touch.y >= HEIGHT/2+125 and touch.y <= HEIGHT/2+175 then
                st=true
        end
        if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and 
            touch.y >= HEIGHT/2-25 and touch.y <= HEIGHT/2+25 then
                cr=true
        end
        if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and 
            touch.y >= HEIGHT/2-175 and touch.y <= HEIGHT/2-125 then
                hs=true
        end
    end
    if touch.state==ENDED then
        if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and 
            touch.y >= HEIGHT/2+125 and touch.y <= HEIGHT/2+175 then
        x=Game()
        end
        if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and 
            touch.y >= HEIGHT/2-25 and touch.y <= HEIGHT/2+25 then
        --credits Roll
        end
        if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and 
            touch.y >= HEIGHT/2-175 and touch.y <= HEIGHT/2-125 then
        --Goes to Game Center HighScores
        end
        st=false
        cr=false
        hs=false
    end
end

Please explain what I’m doing wrong(and what the proper syntax is), how the original forums Main tab works (in a full description), and any general tips that might improve my knowledge on this topic.

Thanks in advance.

Have a look at these, especially at the template link at the bottom

https://bitbucket.org/TwoLivesLeft/core/wiki/Step%20by%20step%20projects

@Paintcannon Here’s a slight modification to your code. I added the other classes that can go into their own tabs.


--# Main
    
function setup()
    x=TitleScreen()
end
        
function draw()
    x:draw()
end

function touched(t)
    x:touched(t)
end


--# Game
Game = class()

function Game:init()   
end

function Game:draw()
    background(40,40,50)
    text("Game: Tap screen",WIDTH/2,HEIGHT/2)    
end

function Game:touched(t)
    x=TitleScreen()
end


--# Credits
Credits = class()

function Credits:init()   
end

function Credits:draw()
    background(40,40,50)
    text("Credits: Tap screen",WIDTH/2,HEIGHT/2)    
end

function Credits:touched(t)
    x=TitleScreen()
end


--# HighScore
HighScore = class()

function HighScore:init()    
end

function HighScore:draw()
    background(40,40,50)
    text("HighScore: Tap screen",WIDTH/2,HEIGHT/2)    
end

function HighScore:touched(t)
    x=TitleScreen()
end


--# TitleScreen
TitleScreen = class()

function TitleScreen:init()
    rectMode(CORNER)
    spriteMode(CORNER)
    textMode(CENTER)
    st=false
    cr=false
    hs=false
end

function TitleScreen:draw()   
    sprite("SpaceCute:Background",0,0,WIDTH,HEIGHT)
    stroke(0, 231, 255, 255)
    strokeWidth(5)
    fill(8, 255, 0, 255)
    if st then
        fill(11, 0, 255, 255)
    end  
    rect(WIDTH/2-100,HEIGHT/2+125,200,50)

    fill(8, 255, 0, 255)
    if cr then
        fill(11, 0, 255, 255)
    end    
    rect(WIDTH/2-100,HEIGHT/2-25,200,50)

    fill(8, 255, 0, 255)
    if hs then
        fill(11, 0, 255, 255)
    end    
    rect(WIDTH/2-100,HEIGHT/2-175,200,50)

    fill(255, 0, 22, 255)
    text("Start Game",WIDTH/2,HEIGHT/2+150)
    text("Credits",WIDTH/2,HEIGHT/2)
    text("HighScores",WIDTH/2,HEIGHT/2-150)
    textSize(150,30)
end

function TitleScreen:touched(touch)
    if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and 
        touch.y >= HEIGHT/2+125 and touch.y <= HEIGHT/2+175 then
        if touch.state==BEGAN then
            st=true
        elseif touch.state==ENDED then
            x=Game()
        end
    end
    if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and 
        touch.y >= HEIGHT/2-25 and touch.y <= HEIGHT/2+25 then
        if touch.state==BEGAN then
            cr=true
        elseif touch.state==ENDED then
            x=Credits()
        end
    end
    if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and 
        touch.y >= HEIGHT/2-175 and touch.y <= HEIGHT/2-125 then
        if touch.state==BEGAN then
            hs=true
        elseif touch.state==ENDED then
            x=HighScore()
        end
    end
    if touch.state==ENDED then
        st=false
        cr=false
        hs=false
    end
end

Wow the code for tabs was much easier than i origionaly thought it was. Thanks :slight_smile: