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

How to ignore a pullEvent

Started by axel.codeFail(), 29 December 2013 - 02:27 AM
axel.codeFail() #1
Posted 29 December 2013 - 03:27 AM
I'm helping a guy on this one server I play on make a slots machine using the monitor peripherals. my only problem is that if a player right-clicks twice on the monitor, it returns, "slot :42: Expected number." If you only right-click once, it works fine, so how would I ignore the player interaction until the program is done doing its thing? Code.

--Finding the monitor side
local side=nil
for r,s in pairs(rs.getSides()) do
  if peripheral.getType(s)=='monitor' then
	side=s
	break
  end
end
--Wrapping the peripheral and setting up defaults
local m = peripheral.wrap(side)
m.setBackgroundColor(colors.black)
m.clear()
m.setTextScale(1)
m.setCursorPos(1,1)
m.setTextColor(colors.yellow)
m.write("SLOTS")
--Playing the game
while true do
  if os.pullEvent()=='monitor_touch' then
  --Spinner 1
	local sponge1=nil
	local number1 = math.random(1, 6)
	if number1 == 1 then
	  sponge1 = colors.yellow
	elseif number1 == 2 then
	  sponge1 = colors.green
	elseif number1 == 3 then
	  sponge1 = colors.blue
	elseif number1 == 4 then
	  sponge1 = colors.violet
	elseif number1 == 5 then
	  sponge1 = colors.red
	elseif number1 == 6 then
	  sponge1 = colors.orange
	end
	function slot1(x1,y1,x2,y2,color1)
	  x1,y1,x2,y2 = 1,2,2,2
	  for i = x1,x2 do
		for o = y1,y2 do
		  color1 = sponge1
		  m.setCursorPos(i,o)
		  m.setBackgroundColor(color1)
		  m.write(" ")
		end
	  end
	end
	if number1==1 then
	  slot1(x1,y1,x2,y2,color1)		
	elseif number1==2 then
	  slot1(x1,y1,x2,y2,color1)
	elseif number1==3 then
	  slot1(x1,y1,x2,y2,color1)
	elseif number1==4 then
	  slot1(x1,y1,x2,y2,color1)
	elseif number1==5 then
	  slot1(x1,y1,x2,y2,color1)
	elseif number1==6 then
	  slot1(x1.y1.x2.y2.color1)
	end
  end
end

The part in specific that I'm having troubles with is line 19


if os.pullEvent()=='monitor_touch' then

Any help would be greatly appreciated. = ^_^/>=
Edited on 29 December 2013 - 03:03 AM
wieselkatze #2
Posted 29 December 2013 - 07:54 AM
The problem isn't the os.pullEvent().
In your pastebin code there is "nimber1" two times instead of "number1".
Also when you are calling your slot1() function with the variable "color1" that variable is nil, because it wasn't declared before.
(In the function call slot1(x1,y1,x2,y2) those variables don't even make sense, because they are already given in the function itself.)
A solution could look like this:
http://pastebin.com/BkuieRtY
axel.codeFail() #3
Posted 29 December 2013 - 06:57 PM
The problem isn't the os.pullEvent().
In your pastebin code there is "nimber1" two times instead of "number1".
Also when you are calling your slot1() function with the variable "color1" that variable is nil, because it wasn't declared before.
(In the function call slot1(x1,y1,x2,y2) those variables don't even make sense, because they are already given in the function itself.)
A solution could look like this:
http://pastebin.com/BkuieRtY

As I said above, if I only right-click once on the screen, then it works fine. The problem only arises when I double right-click. Also, I saw the "nimber" errors, and corrected them. For some reason they never gave me any troubles.

The error it gives me is

slot:42: Expected number

I only get trouble on line 42

m.setBackgroudColor(color1)


&EDIT&

You tell the function slot1{} to use the variable number1 as a color, but number1 is a number between 1 and 6. That would only confuse the program, and all the if's with the variable sponge1 would be run, but unused.



    if number1==1 then
	  slot1(x1,y1,x2,y2,number1)		  
    elseif number1==2 then
	  slot1(x1,y1,x2,y2,number1)
    elseif number1==3 then
	  slot1(x1,y1,x2,y2,number1)
    elseif number1==4 then
	  slot1(x1,y1,x2,y2,number1)
    elseif number1==5 then
	  slot1(x1,y1,x2,y2,number1)
    elseif number1==6 then
	  slot1(x1,y1,x2,y2,number1)
Edited on 29 December 2013 - 06:04 PM
CometWolf #4
Posted 30 December 2013 - 05:39 AM
What he's saying is this is pointless

	    function slot1(x1,y1,x2,y2,color1)
		  x1,y1,x2,y2 = 1,2,2,2
		  for i = x1,x2 do
			    for o = y1,y2 do
				  color1 = sponge1
				  m.setCursorPos(i,o)
				  m.setBackgroundColor(color1)
				  m.write(" ")
			    end
		  end
	    end
	    if number1==1 then
		  slot1(x1,y1,x2,y2,color1)		   
	    elseif number1==2 then
		  slot1(x1,y1,x2,y2,color1)
	    elseif number1==3 then
		  slot1(x1,y1,x2,y2,color1)
	    elseif number1==4 then
		  slot1(x1,y1,x2,y2,color1)
	    elseif number1==5 then
		  slot1(x1,y1,x2,y2,color1)
	    elseif number1==6 then
		  slot1(x1.y1.x2.y2.color1)
	    end

Because you delcare all the function arguments within the function itself.
There's no need to pass the same arguments over and over if you're not using them anyway

A better way to do this would be like this


	    function slot1()
		  for i = 1,2 do
			    for o = 2,2
				  m.setCursorPos(i,o)
				  m.setBackgroundColor(sponge1)
				  m.write(" ")
			    end
		  end
	    end
            slot1()		   

To be honest there's no need for slot1 to be a function at all.



As for your original request, the program will only loop the code in the loop after the first one is done. Basically it won't react to your second click until it's done processing the first one. Therefore rendering any ignoring of events pointless.
Edited on 30 December 2013 - 07:13 AM
wieselkatze #5
Posted 30 December 2013 - 10:04 AM
Thanks, CometWolf.
@axel.codeFail():
You can also write your program in line one half of the current length.

color = {
colors.yellow;
colors.green;
colors.blue;
colors.magenta;
colors.red;
colors.orange
}
for i, v in pairs(rs.getSides()) do
  if peripheral.getType(v) == "monitor" then
  m = peripheral.wrap(v)
  break
  end
end
m.setTextScale(1)
m.setCursorPos(1, 1)
m.setBackgroundColor(32768)
m.write("SLOTS")
while true do
e = {os.pullEvent()}
  if e[1] == "monitor_touch" then
  m.setCursorPos(1, 2)
  m.setBackgroundColor(color[math.random(1, #color)])
  m.write("  ")
  end
end

This would be the code. Just insert your colors in the color table and that's it.
Edited on 30 December 2013 - 09:04 AM