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

os.pullEvent = os.pullEventRaw failing

Started by Micheal Pearce, 17 April 2013 - 03:19 PM
Micheal Pearce #1
Posted 17 April 2013 - 05:19 PM
os i tried making this program unhackable or whatever and added os.pullEvent thing and tested the ctrl+T and sometimes it returns 75 sometimes it returns 74 sometimes it returns some java fuck up or something ive never seen sometimes it returns nothing and just nuls the program and other times it freezes the computer heres the code its fine if noone can help it would be nice to have it unhackable


function Coms()
KID = fs.exists(Gname)
if KID == true then
file = fs.open(Gname,"r")
Cstatus = file.readLine()
file.close()
term.setCursorPos(Px,Py)
term.setBackgroundColor(colors.lightGray)
write("+"..Cname)
if Cstatus == "ON" then
  term.setBackgroundColor(colors.lime)
elseif Cstatus == "OFF" then
  term.setBackgroundColor(colors.red)
end
write(" ")
Py = Py + 2
else
term.setCursorPos(Px,Py)
term.setBackgroundColor(colors.lightGray)
write("+"..Cname)
term.setBackgroundColor(colors.orange)
write(" ")
Py = Py + 2
end
end
function PutComsLeft()
Px = 1
Py = 3
Cname = "Computer 1"
Gname = "C1"
Coms()
Cname = "Computer 2"
Gname = "C2"
Coms()
Cname = "Computer 3"
Gname = "C3"
Coms()
Cname = "Computer 4"
Gname = "C4"
Coms()
Cname = "Computer 5"
Gname = "C5"
Coms()
Cname = "Computer 6"
Gname = "C6"
Coms()
Cname = "Computer 7"
Gname = "C7"
Coms()
Cname = "Computer 8"
Gname = "C8"
Coms()
Cname = "Computer 9"
Gname = "C9"
Coms()
Px = 13
Py = 3
Cname = "Computer 10"
Gname = "C10"
Coms()
end
function SyncCom()
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.lime)
print("Make sure the computer that your're syncing is already on and realy to sync")
write("Computer ID: ")
SyncID = read()
write("Syncing Computer")
textutils.slowPrint("...",1)
rednet.send(217,"PasswordPLZ")
id , PASS, dis = rednet.receive()
print(dis)
if PASS == "8932" then
end
end
function Background()
term.setBackgroundColor(colors.blue)
term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(colors.gray)
print("SteamPunk Servers v1.5							 ")
term.setBackgroundColor(colors.blue)
print("GREEN = ON, RED = OFF ORANGE = CALL KIDGAMER")
end
function MainSelect()
event, button,X,Y = os.pullEvent("mouse_click")
if button == 1 then
if X == 1 and Y == 3 then
  CN = "Computer 1"
  CS = "C1"
  ComSc()
elseif X == 1 and Y == 5 then
  CN = "Computer 2"
  CS = "C2"
  ComsBack()
elseif X == 1 and Y == 7 then
  CN = "Computer 3"
  CS = "C3"
  ComsBack()
elseif X == 1 and Y == 9 then
  CN = "Computer 4"
  CS = "C4"
  ComsBack()
elseif X == 1 and Y == 11 then
  CN = "Computer 5"
  CS = "C5"
  ComsBack()
elseif X == 1 and Y == 13 then
  CN = "Computer 6"
  CS = "C6"
  ComsBack()
elseif X == 1 and Y == 15 then
  CN = "Computer 7"
  CS = "C7"
  ComsBack()
elseif X == 1 and Y == 17 then
  CN = "Computer 8"
  CS = "C8"
  ComsBack()
elseif X == 1 and Y == 19 then
  CN = "Computer 9"
  CS = "C9"
  ComsBack()
elseif X == 13 and Y == 3 then
  CN = "Computer 10"
  CS = "C10"
  ComsBack()
  end
end
end
function updateComs()
rednet.open("top")
end
function ComsBack()
term.setBackgroundColor(colors.blue)
term.clear()
term.setBackgroundColor(colors.gray)
term.setCursorPos(1,1)
print("SteamPunk Servers v1.5							 ")
end
function ComSc()
ComsBack()
term.setBackgroundColor(colors.blue)
write("Loading")
textutils.slowPrint("...",4)
term.clear()
ComsBack()
ADD = fs.exists(CS)
if ADD == true then
file = io.open(CS,"r")
Pstatus = file:read()
file:close()
term.setCursorPos(2,3)
term.setBackgroundColor(colors.blue)
write(CN.." Status = "..Pstatus)
print()
write(" +Back")
event,button,X,Y = os.pullEvent("mouse_click")
while true do
if button == 1 then
  if X == 2 and Y == 4 then
   main()
  end
end
end
end
end
function main()
os.pullEvent = os.pullEventRaw
rednet.open("top")
Background()
PutComsLeft()
MainSelect()
sleep(1)
end
main()
Sariaz #2
Posted 17 April 2013 - 05:56 PM
In your comsback function where you do event,button,x,y = os.pullEvent("mouse_click") if you change that to



repeat
  event,button,x,y = os.pullEvent()
until event == "mouse_click"


then it should fix do the same thing anywhere you specify what event to pull. This is because for some reason in my experience making it lock with the pullEventRaw messes with os.pullEvents where an event to wait for is specified.

Also id advise tabbing after every line of code that needs a end, makes it look neater also spacing between functions end and beginning little things like that and comments make your code a lot easier to read.
LordIkol #3
Posted 17 April 2013 - 09:26 PM
Hi Yuri,

I looked over your code and made some changes see comments for information.
Main thing is that i put your pullevents into loops so that the code is not finishing when you click at an undefined location.
and added some loops to make it little bit shorter

Spoiler

os.pullEvent = os.pullEventRaw -- i prefer to set os Pullevent to raw at the first line but should make no difference.
function Coms()
KID = fs.exists(Gname)
if KID then
file = fs.open(Gname,"r")
Cstatus = file.readLine()
file.close()
term.setCursorPos(Px,Py)
term.setBackgroundColor(colors.lightGray)
write("+"..Cname)
  if Cstatus == "ON" then
   term.setBackgroundColor(colors.lime)
  elseif Cstatus == "OFF" then
   term.setBackgroundColor(colors.red)
  end
write(" ")
Py = Py + 2
else
term.setCursorPos(Px,Py)
term.setBackgroundColor(colors.lightGray)
write("+"..Cname)
term.setBackgroundColor(colors.orange)
write(" ")
Py = Py + 2
end
end

function PutComsLeft()
Px = 1
Py = 3
for i = 1, 9 do  -- put that in a loop cause its shorter and easier to read :)/>
Cname = "Computer " ..i
Gname = "C"..i
Coms()
end
Px = 13
Py = 3
Cname = "Computer 10"
Gname = "C10"
Coms()
end
function SyncCom()
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.lime)
print("Make sure the computer that your're syncing is already on and realy to sync")
write("Computer ID: ")
SyncID = read()
write("Syncing Computer")
textutils.slowPrint("...",1)
rednet.send(217,"PasswordPLZ")
id , PASS, dis = rednet.receive()
print(dis)
if PASS == "8932" then -- this does not do anything
end
end
function Background()
term.setBackgroundColor(colors.blue)
term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(colors.gray)
print("SteamPunk Servers v1.5												    ")
term.setBackgroundColor(colors.blue)
print("GREEN = ON, RED = OFF ORANGE = CALL KIDGAMER")
end
function MainSelect()
while true do --wrapped this into a while loop else the code will break if you click on a position that is not defined in the function.
event, button,X,Y = os.pullEvent("mouse_click")
  if button == 1 then
   if X == 1 and Y == 3 then -- outside the loop cause it calls a different function
    CN = "Computer 1"
    CS = "C1"
    ComSc()
   end
local cnstart = 2 --ID with that we start
   for i = 5,19,2 do -- a loop that counts up in steps of 2
    if X == 1 and Y == i then
	 CN = "Computer "..cnstart -- print "Computer" and add current ID
	 CS = "C"..cnstart
	 ComsBack()
	 cnstart = cnstart + 1  -- count up the ID counter :D/>
    end
   end
   if X == 13 and Y == 3 then
    CN = "Computer 10"
    CS = "C10"
    ComsBack()
   end
end
end
end
function updateComs()
rednet.open("top")
end
function ComsBack()
term.setBackgroundColor(colors.blue)
term.clear()
term.setBackgroundColor(colors.gray)
term.setCursorPos(1,1)
print("SteamPunk Servers v1.5												    ")
end
function ComSc()
ComsBack()
term.setBackgroundColor(colors.blue)
write("Loading")
textutils.slowPrint("...",4)
term.clear()
ComsBack()
ADD = fs.exists(CS)
if ADD == true then
file = io.open(CS,"r")
Pstatus = file:read()
file:close()
term.setCursorPos(2,3)
term.setBackgroundColor(colors.blue)
write(CN.." Status = "..Pstatus)
print()
write(" +Back")
while true do
  event,button,X,Y = os.pullEvent("mouse_click") -- put the pullevent into the loop same as above.
   if button == 1 then
   if X == 2 and Y == 4 then
    main()
   end
  end
end
end
end
function main()
rednet.open("top")
Background()
PutComsLeft()
MainSelect()
sleep(1)
end
main()

i have tested control+T and its not terminating :)/>

Geets Loki
JokerRH #4
Posted 17 April 2013 - 09:42 PM
This is because for some reason in my experience making it lock with the pullEventRaw messes with os.pullEvents where an event to wait for is specified.

os.pullEvent(filter)
calls
os.pullEventRaw(filter)
calls
coroutine.yield(filter)

There shouldn't be any problem.

Edit: I have no idea where your problem is. I copied your program from your post and tested it and it terminated just fine…
Edit2: Do you know how it feels if you write sh*t and as you want to delete it your internet connection crashes?
Sariaz #5
Posted 17 April 2013 - 09:44 PM
This is because for some reason in my experience making it lock with the pullEventRaw messes with os.pullEvents where an event to wait for is specified.

os.pullEvent(filter)
calls
os.pullEventRaw(filter)
calls
coroutine.yield(filter)

There shouldn't be any problem.
This is because for some reason in my experience making it lock with the pullEventRaw messes with os.pullEvents where an event to wait for is specified.

os.pullEvent(filter)
calls
os.pullEventRaw(filter)
calls
coroutine.yield(filter)

There shouldn't be any problem.

it just seams that if i ctrl+t then it triggers os.PullEvent(filter) event though not right event.
JokerRH #6
Posted 17 April 2013 - 10:08 PM

function MainSelect()
  event, button,X,Y = os.pullEvent("mouse_click")
    if button == 1 then
       ...
    end
end

There's your problem.
Computers are resumed with an api similar to the parallel api (I think Cloudy said that sometimes), and that checks if the filter matches or the event is "terminate".

In your case os.pullEvent will be resumed with "terminate" and your button variable is nil, so the program stops.
That's the same reason why you can press the right mouse button to stop the program…
Micheal Pearce #7
Posted 18 April 2013 - 10:16 AM
Thanks guys :)/>
Sariaz #8
Posted 18 April 2013 - 10:39 AM
Our pleasure.