This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
UltimateLP's profile picture

My script doesn´t receive my keys!

Started by UltimateLP, 27 December 2012 - 01:04 AM
UltimateLP #1
Posted 27 December 2012 - 02:04 AM
READ POST #4

Hi everybody, i´ve got a problem with my new ComputerCraft-Project: "ChatSuite"
I programmed a menu for a chat software i programmed, but theres a problem! Every when I start my programm:

string["chat_clientGUI"]:33:<eof> excepted
I don´t know, what made I wrong?

The script in the attachment!

Thanks for help!

– UltimateLP (iRedCraft @ YouTube)
KaoS #2
Posted 27 December 2012 - 02:22 AM
where do you set the variable "m" ?
Edited on 27 December 2012 - 01:24 AM
remiX #3
Posted 27 December 2012 - 02:39 AM
Yeah and these too:


return n
local n=menue(options)

Why are you returning n?

What function is menue?
UltimateLP #4
Posted 27 December 2012 - 03:14 AM
OK my program is working now, thanks! But now theres another problem! When I select the first menupoint and type in the server id, it asks for the nickname, right so far! But when I type in anything I will be ported to the chat screen and cannot type in anything, hes not listening on my event!

Script so far: Attachment
Sammich Lord #5
Posted 27 December 2012 - 03:31 AM
I am not going to download a file to solve your problem. Either put it on Pastebin or put it on the forums with ['CODE']['/CODE'] tags.
remiX #6
Posted 27 December 2012 - 04:41 AM
I'm not sure what you're trying to do after you open your wifi but here's a fixed part of the menu, and the reading of server ID and nickname.

And why did you not use read() to read the server ID and nickname? :)/>

Spoiler

local w, h = term.getSize()
local input = ""
local name = ""
local s_rednet = "back"  -- Hier Seite vom Router eingeben! "top","bottom","front","back","right","left"
local options={
    "Chat beitreten",
    "Mail versenden",
    "Wer ist online?",
    "NickName aendern",
    "Einstellungen",
}

function menu(m)
    term.clear()
    term.setCursorPos(1,1)
    print("--------------------------------------------------")
    print("--------- ComputerCraft ChatSuite v1.1_a ---------")
    print("------------- by iRedCraft @ YouTube -------------")
    print("--------------------------------------------------")
    term.setCursorPos(1, 12)
    print("Zum auswaehlen: Pfeiltaste hoch / runter!")
    n=1
    while true do
        term.setCursorPos(1,6)
        for i=1, #m do
            term.clearLine()
            if i==n then
                print(i, " ["..m[i].."] ")
            else
                print(i, "  ", m[i], "  ")
            end
        end
        a, b= os.pullEvent()
        if a == "key" then
            if b==200 then n=n-1 end
            if b==208 then n=n+1 end
            if b==28 then break end
        end
        if n < 1 then n = #m
        elseif n > #m then n = 1 end
    end
    term.clear() term.setCursorPos(1,1)
    return n
end

local n = menu(options)

if n == 1 then
    repeat
        term.clear()
        term.setCursorPos(1, 1)
        write("Server ID: ")
        serverID = tonumber(read())
    until serverID ~= "" and type(serverID) == "number"
    repeat
        term.clear()
        term.setCursorPos(1, 1)
        write("NickName: ")
        name = read()
    until serverID ~= "" and not string.find(name, " ")
    
    rednet.open( s_rednet )
    rednet.send( serverID, "#CONNECT#"..name )
    local cInput = ""
    local sBars = ""
    local tChatHistory = {}
    local cBlink = ""
    local cBlinkT = 0

    for x=1,w - 1 do
        sBars = sBars.."-"
    end
 
    -- Funktion "draw"
    while true do
        sleep( 0 )
        cBlinkT = cBlinkT + 1
        if cBlinkT == 5 then
            if cBlink == "" then
                cBlink = "_"
            else
                cBlink = ""
            end
            cBlinkT = 0
        end
        term.clear()
        term.setCursorPos(1, 1)
        local calch = h - 4
        for x=table.getn( tChatHistory ) - calch,table.getn( tChatHistory ) do
            if tChatHistory[x] ~= nil then
                write( tChatHistory[x] )
            end
        end
        term.setCursorPos( 1, h - 1 )
        write( "> "..cInput..cBlink )
        term.setCursorPos( 1, h - 2 )
        write( sBars )
    end
 
    -- Funktion "key"
    while true do
        local event, p1 = os.pullEvent()
        if event == "key" then
            if p1 == 41 then
                rednet.send( serverID, "#QUIT___#"..name )
                break
            elseif p1 == 14 then
                cInput = string.sub( cInput, 1, string.len( cInput ) - 1 )
            elseif p1 == 28 then
                rednet.send( serverID, "ch_"..cInput )
                cInput = ""
            end
        elseif event == "char" then
            cInput = cInput..p1
        elseif event == "terminate" then
            rednet.send( serverID, "#QUIT___#"..name )
            break
        end
    end

    -- Funktion "receive"
    while true do
        local event, ID, MSG, DIS = os.pullEvent( "rednet_message" )
        if ID == serverID then
            tChatHistory[table.getn( tChatHistory ) + 1] = MSG
        end
    end

    parallel.waitForAny( draw, key, receive )
end
UltimateLP #7
Posted 27 December 2012 - 06:02 AM
OK, that works fine, but I can´t type in my message under the "————————————————"
remiX #8
Posted 27 December 2012 - 06:45 AM
I'm not sure what you're trying to do after you open your wifi but here's a fixed part of the menu, and the reading of server ID and nickname.
OK, that works fine, but I can´t type in my message under the "————————————————"

Read what I said in my post.. I don't know what you're trying to do then so I didn't change anything there
UltimateLP #9
Posted 28 December 2012 - 02:42 AM
The user should enter a message, after the entry of server id and nickname! Under the lines, the user enters a message which will sended to the server, and here it will be sendet to all clients, a working chat =)
remiX #10
Posted 28 December 2012 - 03:32 AM
It's not letting you type because of this code, not sure what it's doing…


    -- Funktion "draw"
    while true do
        sleep( 0 )
        cBlinkT = cBlinkT + 1
        if cBlinkT == 5 then
            if cBlink == "" then
                cBlink = "_"
            else
                cBlink = ""
            end
            cBlinkT = 0
        end
        term.clear()
        term.setCursorPos(1, 1)
        local calch = h - 4
        for x=table.getn( tChatHistory ) - calch,table.getn( tChatHistory ) do
            if tChatHistory[x] ~= nil then
                write( tChatHistory[x] )
            end
        end
        term.setCursorPos( 1, h - 1 )
        write( "> "..cInput..cBlink )
        term.setCursorPos( 1, h - 2 )
        write( sBars )
    end
UltimateLP #11
Posted 28 December 2012 - 05:54 AM
It writes the GUI for the chat surface, the lines, and the ">"! But when I take out this function, it not shows the chat…
Lyqyd #12
Posted 28 December 2012 - 06:45 AM
Either make your "funktion"s into actual functions and use the parallel call, or merge them into a single event-based loop and remove the parallel call. Right now, you enter the draw "funktion" but never leave it, so your code stays there the whole time. Of course it doesn't care about key presses.
UltimateLP #13
Posted 29 December 2012 - 02:23 AM
Sorry, im german, thats about this "funktion"s ;)/> And how can I leave this function?
AngelMalus #14
Posted 29 December 2012 - 02:43 AM
Seems like you are trying to run 2 processes at the same time. Your "FUNKTION" and the user Input. What Lua Liquidator suggests is to use the Parallel API and run 2 functions at the same time.


function1 = function()
print (rednet.receive())
end
function2 = function()
rednet.broadcast (read())
end
parallel.waitForAny (function1, function2)
UltimateLP #15
Posted 29 December 2012 - 02:48 AM
OK, but Im a noob, so can you place it in the script?
remiX #16
Posted 29 December 2012 - 03:40 AM
Seems like you are trying to run 2 processes at the same time. Your "FUNKTION" and the user Input. What Lua Liquidator suggests is to use the Parallel API and run 2 functions at the same time.

Lua Liquidator is his title, his name is Lyqyd.

OK, but Im a noob, so can you place it in the script?

I merged your key and receive 'functions' and put in the parallel:

Spoiler
local w, h = term.getSize()
local input = ""
local name = ""
local s_rednet = "back"  -- Hier Seite vom Router eingeben! "top","bottom","front","back","right","left"
local options={
    "Chat beitreten",
    "Mail versenden",
    "Wer ist online?",
    "NickName aendern",
    "Einstellungen",
}

function menu(m)
    term.clear()
    term.setCursorPos(1,1)
    print("--------------------------------------------------")
    print("--------- ComputerCraft ChatSuite v1.1_a ---------")
    print("------------- by iRedCraft @ YouTube -------------")
    print("--------------------------------------------------")
    term.setCursorPos(1, 12)
    print("Zum auswaehlen: Pfeiltaste hoch / runter!")
    n=1
    while true do
	    term.setCursorPos(1,6)
	    for i=1, #m do
		    term.clearLine()
		    if i==n then
			    print(i, " ["..m[i].."] ")
		    else
			    print(i, "  ", m[i], "  ")
		    end
	    end
	    a, b= os.pullEvent()
	    if a == "key" then
		    if b==200 then n=n-1 end
		    if b==208 then n=n+1 end
		    if b==28 then break end
	    end
	    if n < 1 then n = #m
	    elseif n > #m then n = 1 end
    end
    term.clear() term.setCursorPos(1,1)
    return n
end

local n = menu(options)

if n == 1 then
    repeat
	    term.clear()
	    term.setCursorPos(1, 1)
	    write("Server ID: ")
	    serverID = tonumber(read())
    until serverID ~= "" and type(serverID) == "number"
    repeat
	    term.clear()
	    term.setCursorPos(1, 1)
	    write("NickName: ")
	    name = read()
    until serverID ~= "" and not string.find(name, " ")
    
    rednet.open( s_rednet )
    rednet.send( serverID, "#CONNECT#"..name )
    local cInput = ""
    local sBars = ""
    local tChatHistory = {}
    local cBlink = ""
    local cBlinkT = 0

    for x=1,w - 1 do
	    sBars = sBars.."-"
    end
 end

function draw()
    cBlinkT = 0
    while true do
	    sleep( 0 )
	    cBlinkT = cBlinkT + 1
	    if cBlinkT == 5 then
		    if cBlink == "" then
			    cBlink = "_"
		    else
			    cBlink = ""
		    end
		    cBlinkT = 0
	    end
	    term.clear()
	    term.setCursorPos(1, 1)
	    local calch = h - 4
	    for x=table.getn( tChatHistory ) - calch,table.getn( tChatHistory ) do
		    if tChatHistory[x] ~= nil then
			    write( tChatHistory[x] )
		    end
	    end
	    term.setCursorPos( 1, h - 1 )
	    write( "> "..cInput..cBlink )
	    term.setCursorPos( 1, h - 2 )
	    write( sBars )
    end
end

function key()
    while true do
	    local event, p1 = os.pullEvent()
	    if event == "key" then
		    if p1 == 41 then
			    rednet.send( serverID, "#QUIT___#"..name )
			    break
		    elseif p1 == 14 then
			    cInput = string.sub( cInput, 1, string.len( cInput ) - 1 )
		    elseif p1 == 28 then
			    rednet.send( serverID, "ch_"..cInput )
			    cInput = ""
		    end
	    elseif event == "char" then
		    cInput = cInput..p1
	    elseif event == "terminate" then
		    rednet.send( serverID, "#QUIT___#"..name )
		    break
        elseif event == "rednet_message" then
            if ID == serverID then
                tChatHistory[table.getn( tChatHistory ) + 1] = MSG
            end
	    end
    end
end

parallel.waitForAny( draw, key )

A lot of errors will pop up because:
[indent=1]You're using 'tChatHistory as a table, you but never define it,[/indent]
[indent=1]cBlinkT is never defined either[/indent]
[indent=1].. There are probably more[/indent]
UltimateLP #17
Posted 30 December 2012 - 03:25 AM
And how to define the table?
remiX #18
Posted 30 December 2012 - 04:01 AM
If you do not know how to define a table, then you should go through the LUA manual and some tutorials in the tutorials sections and browse through and teach yourself. Then try the code again.

But anyway defining a table:
t_table = {}
UltimateLP #19
Posted 09 January 2013 - 03:05 AM
Guys, can you not give my a finish script that works?
remiX #20
Posted 09 January 2013 - 03:10 AM
Post the current code you have now and tell us your errors and i'll help you.