Console library

My latest project
Anyone want some code?

Yes, I want the code. Can we type the command on the screen?

What do you mean? I have this example set up using the touched function

Here is my code:

--# Console
console = {}

console.deltaTime = 0
console.log = {}

function console:draw()
            textWrapWidth(WIDTH / 2)
            local a = 255
            if ElapsedTime - self.deltaTime > 4 then
                a = 255 - (ElapsedTime - self.deltaTime - 4) * 128
            fill(0, 0, 0, a)
            translate(10, 50)
            for i = 1, #self.log do
                local h = {textSize(self.log[i])}
                text(self.log[i], 0, 0)
                translate(0, h[2])
            local m = modelMatrix()

function log(str)
    str = str or RETURN
    str = tostring(str)
    table.insert(console.log, 1, str)
    console.deltaTime = ElapsedTime

--# Main
-- Codea Upgrade

-- Use this function to perform your initial setup
function setup()
    log("Console library version 1.0")
    i = 1
    toLog = {
        "Hello All!",
        "This is my console library",
        "Think fullscreen printing,",
        "And it doesnt obscure the task at hand!",
        "Because after a few seconds...",
        "It disappears...",
        "Appears with a simple log() command",
        "And viola! like magic!",
        "by Jordan"

-- This function gets called once every frame
function draw()
    background(255, 255, 255, 255)

function touched(touch)
    if touch.state == ENDED then
        i = i + 1

Thank you for the code. I see how the code works.


I like your code and I was testing the frame rate as things were being displayed. With nothing displayed, the frame rate was 60 fps. I added count=0 in setup. Added count=count+1 and log(count) in function draw() so that it would add to the count value and display the count on your console. The frame rate started to drop and by the time the count got to 500, the frame rate was about 8 fps. I’m sure it would have dropped a lot more if I let it run. I added the below code to your function log(str) and the frame rate only dropped to about 41 fps and stayed there. The added code limits the console.log size to 60 lines so that it doesn’t keep building in size and slowing the frame rate. No point in keeping lines that aren’t displayed.

function log(str)
    str = str or RETURN
    str = tostring(str)
    table.insert(console.log, 1, str)

    -- added 3 lines
    if #console.log==60 then

    console.deltaTime = ElapsedTime

Thanks @dave1707, I never tested the frame rate… I first thought of doing an if statement to see if the text was visible on screen, but i figured that was more memory intensive than before. A problem with your method is that some strings wrap, which gives them a greater height. So there might be unnecessary calls. I like your solution, and cant believe I never thought of it (I have already used that method before, in my FPS spedometer). But thanks again for your comment.


I changed the font size to 14, so I had a lot more lines on the screen than you did. A calculation can probably be done based on the font, font size, and the screen resolution to get a value close to what’s needed. You could also keep track of the line wraps and figure that into the calculation.

edit-- After playing with this more, I modified the code so that a triple tap would hide the console and another triple tap would show it again. Also, while it’s hidden, I would immediately return from the console:draw() function allowing the fps to jump back to 60 fps.

Good stuff, thanks for sharing.