Asset addressing

@Simeon - quick suggestion - I have just been running a project originally written by @dave1707 to run projects from a separate project list, referred to in a recent thread.

Thought it would be a good idea to provide an example which runs the examples provided with Codea from a project. I ran into the problem of addressing projects in collections. Played around with this but without success. Only way I could get it to work was to add the menu project to the Codea examples collection - then it ran.

Could we have collections added as addressable resources - a command to run them would also be useful.

@Simeon, my apologies - always pays to refer to the online documentation. Managed to get this running with using the text line “Collection:project” so I found “Examples:Lines” runs the lines demo.

Also with the new addressing mode the following ‘asset.documents.Examples.Lines’ finds the project. Will post later when I finish playing.

All,

Sorry about the slow response on this but I have not been able to get enough time to resolve the issues I have encountered. So I am posting the current state of my code (based on @dave1707 s code) with different approach. Please feel free to recommend changes.

Several points to note:

  1. Lost a lot of time trying to find addressing needed until I used “Examples:Lines” for instance.
  2. There seems to be a hangover from the menu in some of the examples eg in ‘bubbles’ the text at the top is very big.
  3. Errors keep cropping up for the touched() function, tried to add parsing for the touched() function but wouldn’t run properly.
  4. I use an image of the screen from Codea for the menu, it includes my chain2 project (top left) which runs within the examples collection properly using @dave1707 s button menu system. Trying to attach my image.

-- ProjectRunner 

function setup()
    --
    bck = readImage(asset.documents.XamplesD)
    button = readImage(asset.builtin.UI.Grey_Button_03)
    st1="if not reStarting then reStarting=true setup() end"
    st2="parameter.action('Main Menu',function() restart() end)"
    projNo = #proj
    dbug = false
    path = "Examples:"
    demo = 999
    str = ""
    menu = true
    v = 0
    lstart = HEIGHT - 40
    path = "Examples:"
    posX = {128,256,384,512,640}
    posY = {875,714,553,392,231,70}
end

function draw()
    -- This sets a dark background color 
    background(40, 48, 50)
    spriteMode(CENTER)
    sprite(bck,WIDTH/2,HEIGHT/2-40,WIDTH*0.9,HEIGHT*0.9)
    sprite(button,WIDTH/2,lstart,520,80)
    fontSize(56)
    fill(255, 21, 0)
    text("EXAMPLES MENU",WIDTH/2,lstart)
end

function touched(t)
    --
    if t.state == ENDED then
        for tx = 1,5 do
            if math.abs(t.x - posX[tx]) < 64 then
                for ty = 1,6 do
                    if math.abs(t.y - posY[ty]) < 80 then
                        demo = (ty-1)*5 + tx
                    end
                end
            end
        end
        if demo ~= 999 then
            b = proj[demo]
            b = path..b
            if b=="Exit" then 
                close()
            else
                if demo ~= 999 and menu == true then
                    projectBuild(b)
                    loadstring(str)()
                    menu = false
                end
                demo = 999
            end
        end
    end
end

function projectBuild(b)
    --
    lst=listProjectTabs(b)
    for c,d in pairs(lst) do
        str=str..readProjectTab(b..":"..d)
        str = str.."\
"
    end
            
    if dbug == true then print(str) end
    a,v=string.find(str,"function setup()",1,true)
    str=string.sub(str,1,v)..st2..string.sub(str,v+1,#str)
    a,v=string.find(str,"function draw()",1,true)
    str=string.sub(str,1,v)..st1..string.sub(str,v+1,#str)
end

proj = {
        "Lines",
        "Multi Touch",
        "Handling Touches",
        "Pencil Case",
        "Ellipse Modes",   
        "Shapes and Smooth",
        "Graphics Styles",
        "Blend Modes",
        "Speakeasy",
        "Sounds Plus",
        "Parameters",
        "Animation",
        "Camera",
        "Basic Physics",
        "Physics Lab",
        "Mesh",
        "Shaders",
        "Roller Coaster",
        "Flappy",
        "Cargo-Bot",
        "Anagrams",
        "Brickout",
        "Lua Jump",
        "Gravity",
        "Hoops n Bubbles",
        "Ping",
        "Bubbles",
        "Image IO",
        "Exit"
        }

Edit: added the forgotten Carg-Bot to the project list, and corrected search and replace errors in Mesh and Shaders.

@Simeon - probably know this anyway but just noticed the search facility in the Codea editor is not case sensitive - eg search for cH an you get touched() not a problem In most cases but if you ‘Replace All’ touched() changes accordingly.

Any chance on introducing case sensitivity to the search?

@Simeon Add an option for search to allow case sensitive or not.

All,
Finally removed all the errors in the proj[] table so many of the demos work OK but some have errors related to the touched function. Above code updated, will update further when I get round the errors.

@Bri_G Try making this change to your code to fix the touched function error. Add the line str=str…”
” . See the code below where it goes. What I found out was any tab that doesn’t have a blank line as it’s last line causes the end statement of the previous tab and the first line of the next tab to be one word. That causes an error when the code is run. The change I show forces a new line at the end of each tab. I was having trouble with Hoops n Bubbles when I tried using my original code and I tracked it down to the non blank last line of a tab.

PS. You need to add code so the touch function only works when you touch inside an example icon. Maybe it’s just the way I copied your examples picture, but if I touch outside an icon, I get an error.

                lst=listProjectTabs(b)
                for c,d in pairs(lst) do
                    str=str..readProjectTab(b..":"..d)
                    str=str.."\
"
                end

@dave1707 - thanks for that, I would never have found it. Funny though, I have often found it annoying when I have tapped on a listing in a tab at the bottom to find the cursor behind end or }. Never thought of it as an error but found it frustrating. Thanks again, will amend code above.

All,
Finally got this running, fingers crossed, removed my local demo Chain2 from the examples collection, so changed the image and updated the code to reflect that in the code entry above. Also added code to ignore touches outside the project icons in the image. Let me know if there is an issue. Thanks again to @dave1707 for the pointers to solve the errors.

Also pulling out the parameter window gives a route back to the menu page.

@Bri_G Starting code in the very first position or not putting a blank line at the end isn’t any kind of error, but just as you said, annoying. I always start on the second line and put a blank line at the end. It’s only a problem when you combine two tabs together. The “end“ of the previous tab and the first line of the next tab are combined to form one word and then cause an error when they’re run with loadstring. It took me awhile to figure out what the problem was, but that’s the fun part of coding.

I’ll add an option for case sensitive search

@dave1707 - neat example which has answered some of my problems. I am just struggling with file addressing changes and how the examples above work. I can see that a file is read in as a text file and is then run/interpreted by the Lua/Codea interpreter with the loadstr(str)() command. But I don’t know the mechanics of how tat works since two Lua scripts are in memory.

But the real problem for me was in moving from the string path creation for project files to the asset.file listing method. Just checking all seem to be stored as text. From the following edit to your code

proj=listProjects(asset.documents.Examples)

This Ignores the path and prints out the contents of the full project list and collections.

Adding the following code after your table.sort(project) prints out the file name of

for loop = 1,#proj do
        print(proj[loop]..”is text”)
    end

projects in the Examples project. So I am now assuming you need to use text based file addressing for projects not asset addressing.

@dave1707 - I have got into the habit of placing two remarked lines at the front of the code and usually finish the bottom of the code with a carriage return - but not always. I see a fair amount of posted code without the CR at the end.

Now, with this menu project, just need to modify it for the asset style file addressing.

Thanks again for your help.

All,

Tried to use asset type addressing as follows, when applied to the above program.


if demo ~= 999 then
            b = proj[demo]
        --    b = path..b
            if b=="Exit" then 
                close()
            else
                if demo ~= 999 and menu == true then
                  --  projectBuild(b)
                    require(b)
                    menu = false
                end
                demo = 999
            end
        end

Tried with require and also dofile with formatting options as below, but errors were thrown up- it appears string addressing is needed. Is there a way to get round this.


proj = {
        "asset.documents.Examples.Lines",
        asset.documents.Examples.Multi_Touch
        }

@Bri_G Not sure what you’re trying to do with the “asset.documents.Examples“. Here’s another version I had for running the Examples from a menu.

displayMode(FULLSCREEN)

function setup()
    proj=listProjects("Examples")
    table.sort(proj)
    dy=0
    rectMode(CENTER)
    st1="if not reStarting then reStarting=true setup() end"
end

function draw()
    background(157, 157, 181, 255)
    for a,b in pairs(proj) do
        fill(0, 206, 254, 255)
        rect(WIDTH/2+50,HEIGHT-60*a+dy,180,50)
        fill(247, 22, 22, 255)        
        text(b,WIDTH/2+50,HEIGHT-60*a+dy)
    end
    fill(255)
    text("Scroll up/down for all Examples.",150,HEIGHT/2)
    text("Press restart icon to exit running Example.",170,HEIGHT/2-50)
end

function touched(t)
    if t.state==ENDED then
        for a,b in pairs(proj) do
            str=""
            if t.x>WIDTH/2-90+50 and t.x<WIDTH/2+90+50 and 
                    t.y>HEIGHT-a*60+dy-25 and t.y<HEIGHT-a*60+dy+25 then
                b="Examples:"..b
                lst=listProjectTabs(b)
                for c,d in pairs(lst) do
                    str=str..readProjectTab(b..":"..d).."\
"
                end
                a,b=string.find(str,"function draw()",1,true)
                str=string.sub(str,1,b)..st1..string.sub(str,b+1,#str)
                loadstring(str)()
                return
            end
        end
    elseif t.state==CHANGED then
        dy=dy+t.deltaY
    end
end

@Simeon - thanks for the case selectivity button.