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

Problems with os.pullEven("key") ...

Started by Tjakka5, 04 December 2012 - 01:34 AM
Tjakka5 #1
Posted 04 December 2012 - 02:34 AM
  • b = os.pullEvent("key")
  • if b == 208 then
  • print("Hi"
  • end


Having problems with that, anyone knows why?
Sammich Lord #2
Posted 04 December 2012 - 02:36 AM
os.pullEvent returns the event, and then the parameters. So you do:

local event, p1 = os,pullEvent("key")
if p1 == 209 then
  print("hI")
end
Doyle3694 #3
Posted 04 December 2012 - 02:41 AM
or even
p = {os.pullEvent("key")}
if p[1] == 208 then
   --do stuff
end

Featureing tables!
Tjakka5 #4
Posted 04 December 2012 - 02:44 AM
Thanks all, havent done much (Is equal to none) with the Os.blabla stuff.
Tjakka5 #5
Posted 04 December 2012 - 02:55 AM
Aaw, darnit, me still haz problems


local pressKeyStartScreen
local chooseScreen == "1"

function startScreen()
  while true do
--
    local event, pressKeyStartScreen = os.pullEvent("key")
	print("lol")
	if pressKeyStartScreen == 208 then
	  if chooseScreen == "1" then
		chooseScreen = "2"
	  end
	  if chooseScreen == "2" then
		chooseScreen = "3"
	  end
	  if chooseScreen == "3" then
		chooseScreen = "1"
	  end
	end
  end
  startScreen()

makerimages #6
Posted 04 December 2012 - 03:03 AM
Describe your problem in depth, otherwise we cant help you, and learn to use the code tags as well.
Tjakka5 #7
Posted 04 December 2012 - 03:05 AM
Describe your problem in depth, otherwise we cant help you, and learn to use the code tags as well.

Theres not much to explain…

I dont understand os.pullEvent("key")
makerimages #8
Posted 04 December 2012 - 03:24 AM
I`m not sure, but try removing the
print("lol")
​ that might be it.
otherwise: http://computercraft.info/wiki/index.php?title=Os.pullEvent
PixelToast #9
Posted 04 December 2012 - 03:56 AM
ffffffffffffffffffffffff *tableflip*

you are doing it so wrong and everyone refuses to help
he obiously dosent understand lua's syntax
every do, then, and function needs a matching end
if statements can be combined like this:

if a==1 then
-- do stuff
elseif a==2 then
-- do more stuff
elseif a==3 then
-- etc
end

or even better use modulous


local pressKeyStartScreen
local chooseScreen == 1
function startScreen()
  while true do
	local event, pressKeyStartScreen = os.pullEvent("key")
		if pressKeyStartScreen == 208 then
			chooseScreen=(chooseScreen+1) % 4 -- modulous!
			break
		end
  end
end
startScreen()
storing numbers as strings is unnecicary

"break" can be used to end the inner most loop (in this case the "while true do" block)
Tjakka5 #10
Posted 04 December 2012 - 07:32 AM
ffffffffffffffffffffffff *tableflip*

you are doing it so wrong and everyone refuses to help
he obiously dosent understand lua's syntax
every do, then, and function needs a matching end
if statements can be combined like this:

if a==1 then
-- do stuff
elseif a==2 then
-- do more stuff
elseif a==3 then
-- etc
end

or even better use modulous


local pressKeyStartScreen
local chooseScreen == 1
function startScreen()
  while true do
	local event, pressKeyStartScreen = os.pullEvent("key")
		if pressKeyStartScreen == 208 then
			chooseScreen=(chooseScreen+1) % 4 -- modulous!
			break
		end
  end
end
startScreen()
storing numbers as strings is unnecicary

"break" can be used to end the inner most loop (in this case the "while true do" block)

If I may ask, what exactly is the point of 'ifelse' instead of 'if', does it speed up the program, or..?

EDIT: OH GOSH, I finaaly got it, I set it to 2, then to 3, then back to 1, printing the 1, oh, thanks!
Doyle3694 #11
Posted 04 December 2012 - 07:38 AM
because if's will run after each other. a elseif will on check if the previous condition wasn't met.
Tjakka5 #12
Posted 04 December 2012 - 07:49 AM
IT STILL DOES NOT WORK!!!

Have a look at the whole program, dont worry about the graphical stuff, its dutch.
And, please, only comment about the os.blabla stuff, I know I can make other bits of code smaller, but I dont feel like it right now.

http://pastebin.com/hmJUFZwT
ChunLing #13
Posted 04 December 2012 - 08:18 AM
You are still using sequential if then ends rather than if then elseif then end.

In your previous example, here's what happened if chooseScreen was 1:
if chooseScreen == "1" then chooseScreen = "2" end –chooseScreen is now 2
if chooseScreen == "2" then chooseScreen = "3" end –chooseScreen is now 3
if chooseScreen == "3" then chooseScreen = "1" end –chooseScreen is now 1
See the problem here? In a sequence of conditionals where you only want one of them to be executed, use elseif's so that only one of them is.

Now, you tried to fix that particular problem (though the rest of your code is full of if then end if then ends where you should be using elseifs), but chooseScreen =(chooseScreen+1) % 3 doesn't do what you want, you add the one after doing the mod. Otherwise, when chooseScreen is 2 you get zero instead of three. So "chooseScreen = (chooseScreen % 3)+1"

I know that's not exactly your fault, PixelToast should have paid more attention to what you wanted to have happen there. But you have some responsibility to figure out how a proposed solution works and adapt it to your needs.
Tjakka5 #14
Posted 05 December 2012 - 02:51 AM
Sooo…

This:


local event, pressKeyStartScreen = os.pullEvent("key")
if pressKeyStartScreen == "208" then
   if chooseScreen == "1" then
	 chooseScreen = "2"
   ifelse chooseScreen == "2" then
	 chooseScreen = "3"
   ifelse chooseScreen == "3" then
	  chooseScreen = "1"
   end
end   

should work, right?
Tjakka5 #15
Posted 05 December 2012 - 03:00 AM
After some more editing the above code, fixing the ifelse to elseif…

Pressing the down arrow key (or any other key) it shows the '1' picture.

And then It doesnt do a thing…
Kingdaro #16
Posted 05 December 2012 - 03:21 AM
I forgot to reply to this yesterday, whoops.

But anyway, the "208" in your code shouldn't have quotes around it.


local event, pressKeyStartScreen = os.pullEvent("key")
if pressKeyStartScreen == 208 then
   if chooseScreen == "1" then
         chooseScreen = "2"
   elseif chooseScreen == "2" then
         chooseScreen = "3"
   elseif chooseScreen == "3" then
          chooseScreen = "1"
   end
end   
Doyle3694 #17
Posted 05 December 2012 - 04:52 AM
OMFGBBQ

No but seriously, you got them the wrong way around ;)/> elseif and not ifelse
Tjakka5 #18
Posted 05 December 2012 - 06:20 AM
OMFGBBQ

No but seriously, you got them the wrong way around ;)/> elseif and not ifelse

OMFGBBQ…

O My F*cking G*d, Go BarByQue?

And, yes, I know, I derped :3
Tjakka5 #19
Posted 05 December 2012 - 06:27 AM
GOOD NEWS EVERYONE!

I FINALLY(!) got it to work!
Thanks for all your support, and from now on…

This topic is dead.

:3