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

os.PullEvent error

Started by RoD, 30 March 2014 - 12:21 AM
RoD #1
Posted 30 March 2014 - 02:21 AM
Hi everyone,
i am trying to prevent users to ctrl+t my program and i already done the:
Spoiler

os.pullEvent = os.pullEventRaw
And when i try to ctrl+t to test it it says:

184:attempt to compare nil with number

Here's the code i am working on:
Spoiler

os.loadAPI("rodos/apis/defaults/crypt")

shell.run("rm rodos/.data/passwords/admin/rod")
shell.run("cp rodos/.data/passwords/rod rodos/.data/passwords/admin/rod")


--local pullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw

function clear(clx, cly)
       term.clear()
       term.setCursorPos(0, 0)
end

function cprint(txt)
       x, y = term.getSize()
       half = x/2
       textlenght = string.len(txt)
       div = textlenght/2
       pos = half-div
       cx, cy = term.getCursorPos()
       term.setCursorPos(pos, cy)
       print(txt)
end

function drawvertical(dx, dy, rep)
       count = 0
       repeat 
              term.setCursorPos(dx, dy)
              print("|")
              count = count + 1
              dy = dy + 1
       until count == rep
end

function read( _sReplaceChar, _tHistory )
term.setCursorBlink( true )
    local sLine = ""
local nHistoryPos = nil
local nPos = 0
    if _sReplaceChar then
  _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
end

local w, h = term.getSize()
local sx, sy = term.getCursorPos()

local function redraw( _sCustomReplaceChar )
  local nScroll = 0
  if sx + nPos >= w then
   nScroll = (sx + nPos) - w
  end

  term.setCursorPos( sx, sy )
  local sReplace = _sCustomReplaceChar or _sReplaceChar
  if sReplace then
   term.write( string.rep(sReplace, string.len(sLine) - nScroll) )
  else
   term.write( string.sub( sLine, nScroll + 1 ) )
  end
  term.setCursorPos( sx + nPos - nScroll, sy )
end

while true do
  local sEvent, param = os.pullEvent()
  if sEvent == "char" then
       if #sLine < 10 then  
       sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
       nPos = nPos + 1
       redraw()
       end


  elseif sEvent == "key" then
          if param == keys.enter then
    -- Enter
    break

   elseif param == keys.left then
    -- Left
    if nPos > 0 then
         nPos = nPos - 1
         redraw()
    end

   elseif param == keys.right then
    -- Right   
    if nPos < string.len(sLine) then
         nPos = nPos + 1
         redraw()
    end

   elseif param == keys.up or param == keys.down then
                            -- Up or down
    if _tHistory then
         redraw(" ");
         if param == keys.up then
          -- Up
          if nHistoryPos == nil then
           if #_tHistory > 0 then
            nHistoryPos = #_tHistory
           end
          elseif nHistoryPos > 1 then
           nHistoryPos = nHistoryPos - 1
          end
         else
          -- Down
          if nHistoryPos == #_tHistory then
           nHistoryPos = nil
          elseif nHistoryPos ~= nil then
           nHistoryPos = nHistoryPos + 1
          end    
         end

         if nHistoryPos then
                                         sLine = _tHistory[nHistoryPos]
                                         nPos = string.len( sLine )
                                    else
          sLine = ""
          nPos = 0
         end
         redraw()
                            end
   elseif param == keys.backspace then
    -- Backspace
    if nPos > 0 then
         redraw(" ");
         sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
         nPos = nPos - 1        
         redraw()
    end
   elseif param == keys.home then
    -- Home
    nPos = 0
    redraw() 
   elseif param == keys.delete then
    if nPos < string.len(sLine) then
         redraw(" ");
         sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )   
         redraw()
    end
   elseif param == keys["end"] then
    -- End
    nPos = string.len(sLine)
    redraw()
   end
  end
end

term.setCursorBlink( false )
term.setCursorPos( w + 1, sy )
print()

return sLine
end

function main(bollean, error)
       term.setBackgroundColor(colors.white)
       clear()
       start = paintutils.loadImage("rodos/images/start")
       paintutils.drawImage(start, 0 , 0)
       term.setCursorPos(4,4)
       cprint("RoDOS ALFA")
       term.setCursorPos(14, 14)
       cprint("Copyright (c) 2014")

       term.setBackgroundColor(colors.orange)
       term.setCursorPos(15, 8)
       print("login")
       term.setCursorPos(27, 8)
       print("register")

       if bollean == true then
              term.setBackgroundColor(colors.red)
              term.setCursorPos(5,6)
              cprint(error)
              sleep(2)
              main()
       elseif bollean == false then
       end

       while true do
              event, button, xPos, yPos = os.pullEvent("mouse_click")
              if xPos >= 15 and xPos <= 19 and yPos == 8 then
                     login()
              elseif xPos >= 27 and xPos <= 35 and yPos == 8 then
                     register(false)
              end
       end
end

function login(account)
       term.setBackgroundColor(colors.white)
       clear()
       paintutils.drawImage(start, 0 , 0)
       term.setCursorPos(4,4)
       cprint("Login")
       term.setCursorPos(7,7)
       term.setBackgroundColor(colors.orange)
       cprint("Username:           ")
        term.setCursorPos(7,9)
       cprint("Password:           ")
       term.setCursorPos(24,7)
       user = read()
       term.setCursorPos(24, 9)
       pass = read("*") 

       if account == "admin" then
                if fs.exists("rodos/.data/passwords/admin/"..user) then
              pfile = fs.open("rodos/.data/passwords/admin/"..user, "r")
              passd = pfile.readAll()
              pfile.close()
              passd = crypt.decrypt(passd, "123")
              end
         --     print(user.." and "..passd)


       if passd == pass then
              register(true)
       elseif fs.exists("rodos/.data/passwords/admin/"..user) == false then
              main(true, "Not admin or inexistent user")
       elseif passd ~= pass then
              main(true, "Incorrect password")
       end    
       elseif account ~= "admin" then
       if fs.exists("rodos/.data/passwords/"..user) then
              pfile = fs.open("rodos/.data/passwords/"..user, "r")
              passd = pfile.readAll()
              pfile.close()
              passd = crypt.decrypt(passd, "123")
       end

       if passd == pass then
              term.setBackgroundColor(colors.black)
              clear()
              os.exit()
       elseif not fs.exists("rodos/.data/passwords/"..user) then
              main(true, "Inexistent username")
       elseif passd ~= pass then
              main(true, "Incorrect password")
       end
end
end

function register(bollean)
       if bollean == false then
              login("admin")
       elseif bollean == true then
       end
       term.setBackgroundColor(colors.white)
       clear()
       paintutils.drawImage(start, 0 , 0)
       term.setCursorPos(4,4)
       cprint("Register")
       term.setCursorPos(7,7)
       term.setBackgroundColor(colors.orange)
       cprint("Username:           ")
        term.setCursorPos(7,9)
       cprint("Password:           ")
       term.setCursorPos(24,11)
       cprint("Confirm             ")
       cprint("Password:           ")
       term.setCursorPos(24,7)
       user = read()
       term.setCursorPos(24, 9)
       pass = read("*") 
       term.setCursorPos(24, 12)
       passconf = read("*") 
       if fs.exists("rodos/.data/passwords/"..user) then
              main(true, "Unavailable username")
       elseif pass ~= passconf then
              main(true, "Passwords dont match")
       elseif pass == passconf then
              pass = crypt.encrypt(pass, "123")
              file = fs.open("rodos/.data/passwords/"..user, "w")
              file.write(pass)
              file.close()
              main(false)
       end
end
main()


Any ideas of what the problem is?
Edited on 30 March 2014 - 07:10 AM
CometWolf #2
Posted 30 March 2014 - 02:48 AM
Seeing as this is line 184

		   term.setBackgroundColor(colors.orange)
Im guessing this program isn't the problem. Please post the program included in the error message.
RoD #3
Posted 30 March 2014 - 09:10 AM
Seeing as this is line 184

		   term.setBackgroundColor(colors.orange)
Im guessing this program isn't the problem. Please post the program included in the error message.
My bad, i changed a few lines of code after posting the error line, updatedthe code in my post and as you can see the error is near a pullEvent function, wich makes more sense.
Here is the loop that has the error (its a simple mouse button):

while true do
			  event, button, xPos, yPos = os.pullEvent("mouse_click")
			  if xPos >= 15 and xPos <= 19 and yPos == 8 then
					 login()
			  elseif xPos >= 27 and xPos <= 35 and yPos == 8 then
					 register(false)
			  end
	   end
Bomb Bloke #4
Posted 30 March 2014 - 09:58 AM
Let's assume either xPos or yPos is ending up as nil somehow. How about checking for that prior to your comparisons, and printing out what event/button/etc are set to?

if not (xPos and yPos) then
	print(event)
	print(button)
	print(xPos)
	print(yPos)
end
RoD #5
Posted 30 March 2014 - 10:41 AM
Let's assume either xPos or yPos is ending up as nil somehow. How about checking for that prior to your comparisons, and printing out what event/button/etc are set to?

if not (xPos and yPos) then
	print(event)
	print(button)
	print(xPos)
	print(yPos)
end
I implemented that code in my code.
I tried to only use that and ignore the button code and this is the result:
Spoiler
  • I can't press any buttons(obvious XD)
  • When i do ctrl+t it prints out Terminate and sets the cursor to 4 lines bellow
  • Everytime i do ctrl+t i print treminate again and so on.

Then i tried to have all the code and add your code to mine:
Spoiler
  • I can press buttons
  • When i do ctrl+t it prints out Terminate and sets the cursor to 4 lines bellow
  • Everytime i do ctrl+t i print treminate again and so on.


EDIT: I realized that the terminate that was printing out was from the print(event), i ignored that line and added a term.setCursorPos(0,0) everytime ctrl+t is called. So, the program is now fixed. Thanks CometWolf and Bomb Bloke. Thread can be closed.
Bomb Bloke #6
Posted 30 March 2014 - 12:34 PM
Ok, so what that tells us is that in this instance, os.pullEvent("mouse_click") is able to return a "terminate" event. Unless I'm suffering a massive mental blank, that should not be happening.

For now, you can work around the issue (in that code block at least) with something like this:

while true do
	event, button, xPos, yPos = os.pullEvent("mouse_click")
	if event == "mouse_click" then
		if xPos >= 15 and xPos <= 19 and yPos == 8 then
			login()
		elseif xPos >= 27 and xPos <= 35 and yPos == 8 then
			register(false)
		end
	end
end

However, I'd be very interested to know what version of ComputerCraft you're running.
MKlegoman357 #7
Posted 30 March 2014 - 12:39 PM
This is because terminate event fires no matter what filter you give to os.pullEventRaw. To fix your issue just check if the fired event is mouse_click.

@Bomb Bloke: It has always worked like that (as far as I remember).

EDIT: Don't forget that he has set os.pullEvent to os.pullEventRaw at the start of his program.
Edited on 30 March 2014 - 10:46 AM
Bomb Bloke #8
Posted 30 March 2014 - 12:53 PM
Ah - well I've learned something new. :)/>
theoriginalbit #9
Posted 30 March 2014 - 12:57 PM
Ok, so what that tells us is that in this instance, os.pullEvent("mouse_click") is able to return a "terminate" event. Unless I'm suffering a massive mental blank, that should not be happening.
@Bomb Bloke: It has always worked like that (as far as I remember).
Nope, some versions do allow termination when a filter is supplied, others do not allow it, although don't quote me on which versions are which :P/>
RoD #10
Posted 30 March 2014 - 01:11 PM
Ok, so what that tells us is that in this instance, os.pullEvent("mouse_click") is able to return a "terminate" event. Unless I'm suffering a massive mental blank, that should not be happening.

For now, you can work around the issue (in that code block at least) with something like this:

while true do
	event, button, xPos, yPos = os.pullEvent("mouse_click")
	if event == "mouse_click" then
		if xPos >= 15 and xPos <= 19 and yPos == 8 then
			login()
		elseif xPos >= 27 and xPos <= 35 and yPos == 8 then
			register(false)
		end
	end
end

However, I'd be very interested to know what version of ComputerCraft you're running.
Its fixed now, but the versions are:
  • Minecraft- 1.5.2
  • ComputerCraft- 1.53
Edited on 30 March 2014 - 11:11 AM