Howdy, Stranger!

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

io.read (Codea does not understand?) what is the Codea syntax for io.read?

edited October 28 in Questions Posts: 46

UPDATE, I FOUND WHERE TO TYPE IN THE CONSOLE
Still not working. I am receiving attempt to call string value
see attached image.

Read below for the issue. Which was typing into console, now that I found it, not getting results from the tutorial. I think it has to do with io.read not being part of Codeas/Lua - library.

ORIGINAL POST:
Trying to follow along with my Lua tutorial.

In video linked below, fast forward to .40 second mark, he places the following code:

The outcome is I am supposed to type a number and receive a response.

i0.read

I pasted the rest of the code below.

function setup()
    print "Guess a number"
    math.randomseed(os.time())
    math.random()
    number = math.random(100)
    answer = io.read("*n")
    print(number)
    print(answer)
end

Video:

Comments

  • dave1707dave1707 Mod
    Posts: 7,836

    @tactfulgamer See your other post where you show the original video. As I said there, Codea doesn’t have io.read for keyboard input. You can’t take everything you see in that video and use it in Codea. Codea isn’t a console based program. There is a keyboard input function, but you need more code to use it. You’ll eventually learn how to use it.

    Also, keep all of your questions about the video in one discussion instead of spreading it out in different discussions. It makes it easier to keep all the answers in one place.

  • dave1707dave1707 Mod
    Posts: 7,836

    Here’s how to get a keyboard value in Codea. This gets a single keystroke only. In order to get multiple keys combined, a little more code needs to be added.

    displayMode(FULLSCREEN)
    
    function setup()
        fill(255)
        kbKey=""
        showKeyboard()
    end
    
    function draw()
        background(0)
        text(kbKey,WIDTH/2,HEIGHT-300)    
    end
    
    function keyboard(k)
        kbKey=k
    end
    
  • edited October 29 Posts: 46

    @dave1707 understood about keeping it on one thread. I’ll keep everything contained in this thread from here on forward, regarding this Lua tutorial.

    Regarding io.read
    When you brought up io.read my other thread - it didn’t stay in my mind after reading your response on my other thread, since at that time, I did not encounter it and never heard of it.

    My attention was locked in the responses from you, that were speaking to me... about that particular issue from that thread.

    My bad there, I’ll try to keep up in the future, if you bring up something I haven’t encountered yet.

    Your Keyboard Code
    After typing in your code, I get a bad argument. User Error (meaning me) is now officially in the building!

    THOUGHT PROCESS BEHIND CODE PLACEMENT:

    1) I placed your code fIrst so the keyboard can come up first when Codea runs the program.

    2) I then typed the code from the course

    3) I did not type in io.read, since you stated Codea does not have io.read for keyboard input.

    4) When I run the code, I receive a bad argument in the console screen

    BAD ARGUMENT:

    Main:15: bad argument #1 to ‘text’ (string expected, got nil) 
    stack traceback:
    [C]: in function ‘text’
    Main: 15: in function ‘draw”
    
    Main:15: bad argument #1 to ‘text’ (string expected, got nil) 
    stack traceback:
    [C]: in function ‘text’
    Main: 15: in function ‘draw”
    

    MY CODE:

    displayMode(FULLSCREEN)
    
    
    function setup()
    fill(255)
        kbKey=""
        showKeyboard()
        end
    
    function draw()
        background(0)
        text(kbKey,WIDTH/2,HEIGHT-300)
    end
    
    function keyboard(k)
        kbKey=k
    end
    
    function setup()
        print "Guess a number"
        math.randomseed(os.time())
        math.random()
        number = math.random(100)
        answer = ("*n")
        print(number)
        print(answer)
    end
    
  • dave1707dave1707 Mod
    Posts: 7,836

    @tactfulgamer I think the videos are making things harder for you. You’re trying to take console based code and run it directly on Codea. That’s like watching a video on how to ride a bicycle, then getting on a motorcycle. Your first question is, where’s the pedals. I don’t know for sure, but I feel you’re getting frustrated with Codea. There isn’t much to Codea for basic code. At bare minimum, you can use function setup() to run code. That would be your console based code, but it’s limited in what you can do. Basically, Codea uses function setup(), and function draw(). You should only have one setup() and one draw(). In your code above, you have 2 setup() functions. Only the last setup() is used which is why you’re getting errors. I would like to help you to get going with Codea, but I have a few questions. How much coding experience do you have, and how old are you. The first question is important, the second not so much. The Guess a Number example can be coded in Codea, but you need more experience with Codea to know how to convert from console based, to video based or whatever you want to call Codea. I’m here for as long as you want answers.

  • dave1707dave1707 Mod
    Posts: 7,836

    Here’s your above code. It doesn’t do much and not what you expect. The keyboard routine only returns 1 key as you press it. More code needs to be added to build up what you need from the keyboard routine.

    function setup()
        fill(255)
        kbKey=""
        showKeyboard()
        print "Guess a number"
        math.randomseed(os.time())
        math.random()
        number = math.random(100)
        answer = ("*n")
        print(number)
        print(answer)
    end
    
    function draw()
        background(0)
        text(kbKey,WIDTH/2,HEIGHT-300)
    end
    
    function keyboard(k)
        kbKey=k
    end
    
  • dave1707dave1707 Mod
    Posts: 7,836

    @tactfulgamer Here’s one way to do the Guess a Number game. Look thru this code and try to understand what’s happening. Put comments on lines for what you think it does. Try adding different colors, change font size, change positions of the text, etc to see what happens. If you have any questions, just ask.

    displayMode(FULLSCREEN)
    
    function setup()
        fill(255)
        guess=math.random(100)
        showKeyboard()
        newGame=false
    end
    
    function draw()
        background(40, 40, 50)
        text("Guess a Number from 1 to 100",WIDTH/2,HEIGHT-50)
        text("Key a number, then press return",WIDTH/2,HEIGHT-100)
        val=tonumber(val)
        if val~= nil then
            text(val,WIDTH/2,HEIGHT-150)
            if val>guess then
                text("Too high",WIDTH/2,HEIGHT-200)
            elseif val<guess then
                text("Too low",WIDTH/2,HEIGHT-200)
            else
                text("You guessed it",WIDTH/2,HEIGHT-200)
                text("Press return for a new game",WIDTH/2,HEIGHT-250)
                newGame=true
            end
        else
            str=keyboardBuffer()
            if string.sub(str,#str)==RETURN then
                str=string.sub(str,1,#str-1)
            end
            text(str,WIDTH/2,HEIGHT-150)
        end
    end
    
    function keyboard(k)
        val=nil
        if k==RETURN then
            if newGame then
                setup()
                return
            end
            val=keyboardBuffer()
            hideKeyboard()
            showKeyboard()
        end    
    end
    
Sign In or Register to comment.