Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Run time commands

edited February 2013 in General Posts: 58

What is the command bar supposed to be used for during run time? The only command I've used with it so far is output.clear()
I know it's there for a reason, but exactly what is that reason? Also, just to reaffirm, the whole output window, command bar, and parameters are only for testing and debugging your program.

Tagged:

Comments

  • SimeonSimeon Admin Mod
    Posts: 4,958

    You could use it to check the value of a variable, execute some command. I'm sure people will find uses for it.

    The parameters are, again, there for you to use however you like. I tend to use them to adjust parameters for my games to see what works.

  • Posts: 146

    In which context is the command entered into the command bar executed? I would assume the global environment of the program currently running?

  • SimeonSimeon Admin Mod
    edited February 2013 Posts: 4,958

    That is correct, @gunnar_z.

    I'm open to suggestions on how the command bar should return values — at the moment it returns them from the chunk, and prints the resulting values on the stack.

  • edited April 2013 Posts: 489

    I am interested in understanding what the new 'type a command' bar does in pseudo-code terms. Is my attempt to replicate it below in processCommand reasonably accurate?

    function setup()
        parameter.text("Command", "")
        parameter.action("Run", processCommand)
    end
     
    function draw() background(0) end
     
    function processCommand()
        local g = _G[Command]
        if g then
            print("=> "..tostring(g))
        else
            local f = loadstring(Command)
            if f then
                print("=> "..tostring((f()))) -- Extra () forces 'nil' if no result,
                                              -- but clamps to first result only
            else
                print("attempt to call a string value")
            end
        end
    end
  • edited April 2013 Posts: 140

    I primarily use the Command Bar as a calculator and to see values in my variables.

    But I'm also teaching my kids how to program (I guess we're actually learning together). For the initial lessons, I make them use just the Command Bar. I don't want to teach them about Codea's 'Draw' loop or functions--just yet. The command bar is perfect to show the kids arithmetic operations, basic assignment, relational operators and variable types. Once they have these basics down, we'll tackle the more complex constructs

    One nice addition would be if the Command Bar allowed the 'do' block to force code, typed on multiple lines, to execute as one unit. For instance, I could use the Command Bar to write:

    => do
    =>     print("\nHere are some numbers: ")
    =>     for N = 1, 5, do
    =>        print(N)
    =>     end
    =>     print("\nThat's the end of the numbers.")
    =>end
    

    And Codea wouldn't execute it until I typed the 'end' that closed the 'do' loop. Apparently, Lua command line interpreters allow this use of 'do.' Why would this be valuable? Because it would help beginners going through Lua tutorials. They could focus on the control structure's behavior without having to worry about the intimidating code inside Codea's editor.

    I used to be a technical writer for the Air Force back in my youth. One thing I learned: you have to move students from simple concepts to more complex. A user-friendly Command Bar might become part of a strategy that moved new Codea users from simple, one-line commands, to control structures, to functions, to classes, to full-fledged apps.

  • Posts: 489

    Hello @Ric_Esrey. In my 'type a command' replication above, as you may have spotted, you can enter (and see) multi-line code before pressing the 'Run' button.

  • Posts: 489

    Below, an improved version of the replication of the 'type a command' bar (perhaps, better than version 1.5.1 of the bar):

    function setup()
        parameter.text("Command", "")
        parameter.action("Run", processCommand)
    end
     
    function draw() background(0) end
     
    function processCommand()
        local g = _G[Command]
        if g then -- Is Command a global variable?
            print("=> "..tostring(g))
        else
            local f = loadstring(Command)
            if f then -- Is Command a valid function body?
                local r = {f()} -- Preserve any results
                local n = #r -- Count them
                if n == 0 then -- No results?
                    print("=> nil") -- Strictly, 'no result' not 'nil'
                else
                    for i = 1, n do
                        r[i] = tostring(r[i]) -- Convert to strings
                    end
                    print("=> "..table.concat(r, " ")) -- Output
                end
            else
                print("attempt to call a string value") -- Error
            end
        end
    end
  • BriarfoxBriarfox Mod
    Posts: 1,542

    @mpilgrem great code, thank you for sharing it. Couldn't figure out how to make a dev console work while in full screen mode!

  • Posts: 140

    Greetings @mpilgrem. Actually, I hadn't run your code until now, but you're right; this is exactly what I need. Very useful... thanks.

Sign In or Register to comment.