File management/distributing code over tabs [ANSWERED]

My list of projects is already becoming cumbersome. I understand that ‘tags’ are forthcoming and will make project management easier but in the meantime I wonder if it is possible to use a project’s tabs/files to manage one’s own experiments as well as the various code snippets and samples gathered from other users, and if so, how best to do this.

So far I understand a project must contain one “main” file and understand (at least in principle) the use of other files for creating “classes” but is it possible to place alternative bits of testing code (for example the ‘Understanding Touches’ code which Andrew posted this morning) in another tab and then turn the tab on or off at will?

I hope my question makes sense… Up to now I have been using ‘block comments’ to comment out sections of code that I don’t need but wish to keep as a reference but it would be nice to be able to place said code in another tab (and turn them off when not needed).

John has done this with a physics demo app that he’s preparing for the physics update.

Basically he has a number of tabs (Test1, Test2, Test3, …) and in the Main file he creates an iparameter, as you drag the slider the physics scene changes from Test1 to Test2 and so on. There are a number of ways you could do this, the most basic would be:

Test1 - draws a rect

-- Test 1 Scene
Test1 = class()

function Test1:init()
    -- init scene (treat this as if it's setup() in Main)
end

function Test1:draw()
    -- draw a rect
    background(0)
    fill(255,0,0)
    rect(0,0,100,100)
end

function Test1:touched(touch)
    --handle touches
end

Test2 - draws an ellipse

-- Test 2 Scene
Test2 = class()

function Test2:init()
    -- init scene (treat this as if it's setup() in Main)
end

function Test2:draw()
    -- draw an ellipse
    background(0)
    fill(0,255,0)
    ellipse(50,50,100,100)
end

function Test2:touched(touch)
    --handle touches
end

Main - switch between scenes

-- 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

EDIT: Fixed the bug, thanks Blanchot

Ah very nice (and more pieces of the picture fall into place… click, clack, click)!

Thanks Simeon!

Are there plans to place such examples as this and the DragMe class on the Wiki so others might easily find them?

Feel free to make an examples section and post them there if you like. I’d like to but don’t have the time right now – just sorting some things out with Codea.

(In case another novice comes along and is confused…)

There’s one small typo in Simeon’s main code which throws an error…

The table of scenes in main’s setup() is called ‘scenes’ and the function calls in draw() and touched(touch) index a table called ‘scene’. Just change one (or two) to conform with the other and all is good.

Sorry about that, I didn’t get a chance to test it. Thanks for pointing it out. Should be correct now.

Feel free to make an examples section and post them there if you like. I'd like to but don't have the time right now – just sorting some things out with Codea.

Hey… everyone is glad that you are working on Codea! :slight_smile:

I’ll add the examples to the wiki in the next few days…