Jump to content




"While" and "Until" help

lua help utility

9 replies to this topic

#1 ChaddJackson12

  • Members
  • 264 posts

Posted 02 October 2012 - 12:45 AM

Please help me with the following code if possible:


print("Press [Enter] To Unlock!")
event, key = os.pullEvent("key")
while not key == 13 do -- 13 is the key ENTER
os.sleep(0.1)
until key == 13 then
print("YAY")
end

It gives an error at startup, about a 'too close 'while' at line ...' I cut this out of some code so the number would be pointless to give, but if possible please help or correct this code. Thanks.

#2 ChaddJackson12

  • Members
  • 264 posts

Posted 02 October 2012 - 12:48 AM

View PostChaddJackson12, on 02 October 2012 - 12:45 AM, said:

Please help me with the following code if possible:


print("Press [Enter] To Unlock!")
event, key = os.pullEvent("key")
while not key == 13 do -- 13 is the key ENTER
os.sleep(0.1)
until key == 13 then
print("YAY")
end

It gives an error at startup, about a 'end expected, (too close 'while' at line #)' I cut this out of some code so the number would be pointless to give, but if possible please help or correct this code. Thanks.
I am using notepad++ and I have closed the function it is in, and that is the only other thing that needs to be closed. I did try closing it but it seems as if "until" closes it as well... If that helps you.

#3 Fatal_Exception

  • New Members
  • 105 posts

Posted 02 October 2012 - 01:06 AM

While and Until are two separate types of loop, and Until .. then is not valid syntax.
print("Press [Enter] To Unlock!")
repeat
  event, key = os.pullEvent("key")
  os.sleep(0.1)
until key == 13 -- 13 is the key ENTER
print("YAY")


#4 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 02 October 2012 - 01:07 AM

You got the while syntax wrong:
while <condition> do
  -- code
end
-- or repeat:
repeat
  -- code
until <condition>
So your code should be:
function someFunction() -- you said it's on a function
  print("Press [Enter] To Unlock!")
  repeat
	local event, key = os.pullEvent("key")
    -- removed the sleep, it's not necesary
  until key == 13 then
  print("YAY")
end

Edit: ninja'd :)/>

#5 ChaddJackson12

  • Members
  • 264 posts

Posted 02 October 2012 - 01:07 AM

View PostFatal_Exception, on 02 October 2012 - 01:06 AM, said:

While and Until are two separate types of loop, and Until .. then is not valid syntax.
print("Press [Enter] To Unlock!")
repeat
  event, key = os.pullEvent("key")
  os.sleep(0.1)
until key == 13 -- 13 is the key ENTER
print("YAY")

Ah, thanks.

#6 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 02 October 2012 - 01:09 AM

Oh, a little detail: 13 is not the enter key, it's 28.

#7 ChaddJackson12

  • Members
  • 264 posts

Posted 02 October 2012 - 01:10 AM

View PostMysticT, on 02 October 2012 - 01:07 AM, said:

You got the while syntax wrong:
while <condition> do
  -- code
end
-- or repeat:
repeat
  -- code
until <condition>
So your code should be:
function someFunction() -- you said it's on a function
  print("Press [Enter] To Unlock!")
  repeat
	local event, key = os.pullEvent("key")
	-- removed the sleep, it's not necesary
  until key == 13 then
  print("YAY")
end

Edit: ninja'd :)/>

Unexpected symbol on the "Until" line.. I copied what you had said... :3

EDIT: fixed, sorry.

#8 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 02 October 2012 - 01:12 AM

Derp, didn't see that you put a then after that. It should be:
function someFunction() -- you said it's on a function
  print("Press [Enter] To Unlock!")
  repeat
	local event, key = os.pullEvent("key")
  until key == 28 -- 28 is the enter key
  print("YAY")
end


#9 ChaddJackson12

  • Members
  • 264 posts

Posted 02 October 2012 - 01:13 AM

View PostMysticT, on 02 October 2012 - 01:09 AM, said:

Oh, a little detail: 13 is not the enter key, it's 28.
Wow, thanks, I was just BARELY experiencing this problem. What had happened is I had found the correct key but got it confused from a thing for adobe keyboard IDs, which I though it was a static id for everything.

#10 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 02 October 2012 - 01:15 AM

View PostChaddJackson12, on 02 October 2012 - 01:13 AM, said:

View PostMysticT, on 02 October 2012 - 01:09 AM, said:

Oh, a little detail: 13 is not the enter key, it's 28.
Wow, thanks, I was just BARELY experiencing this problem. What had happened is I had found the correct key but got it confused from a thing for adobe keyboard IDs, which I though it was a static id for everything.
Minecraft (and CC) uses this key codes. In CC 1.4 there's a keys api that makes it easier.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users