Jump to content




[Question] How do I use read() to control a cobblegen


  • You cannot reply to this topic
4 replies to this topic

#1 Theray070696

  • Members
  • 7 posts

Posted 03 April 2012 - 02:29 PM

I am trying to use the read() event to control how long my cobble generator will run for. This is for Feed the Beast. My code is here (don't know how to do spoilers)
print("Cobblestone Generator Control")
sleep(1)
print("How long should this run for?")
read(m)
redstone.setOutput( "right", true )
os.startTimer(m)
redstone.setOutput( "right", false )

The error I get is timer:6: bad argument: double expected, got nil

Is there any way to do this?

#2 EatenAlive3

  • New Members
  • 53 posts

Posted 03 April 2012 - 02:43 PM

A double is a number like 1.5, or 20, and the error is in line 6. When you are trying to assign the variable m to whatever the user types in, it's only being used a parameter for read(). To assign a variable, do m = read().

Read() will only give you a string, which isn't what os.startTimer() uses; to convert it to a number, do os.startTimer(tonumber(m)).

#3 OniNoSeishin

  • New Members
  • 17 posts

Posted 03 April 2012 - 02:55 PM

also, os.startTimer(m) will throw an event after 'm' seconds, which you have to catch with os.pullEvent() inside a loop.
Something like this:
m = read()
yourTimer = os.startTimer(m) -- here you start the timer

while true do
  local event, argument = os.pullEvent()

  if (event == "timer") and (argument == yourTimer) then
    break -- exit from loop when timer ended
  end
end

OR, if you need something simplier, just use sleep(m), which pause the script for 'm' seconds

#4 MrPiggles

  • New Members
  • 3 posts

Posted 03 April 2012 - 04:07 PM

For stuff like this, it's generally a good idea to use some error protection.

Here's an example of how I'd do it:
local m = read()
local yourTimer = os.startTimer(tonumber(m) or 0) -- If m can't be made into a number, it reverts to 0 instead


#5 Theray070696

  • Members
  • 7 posts

Posted 03 April 2012 - 07:02 PM

View PostOniNoSeishin, on 03 April 2012 - 02:55 PM, said:

also, os.startTimer(m) will throw an event after 'm' seconds, which you have to catch with os.pullEvent() inside a loop.
Something like this:
m = read()
yourTimer = os.startTimer(m) -- here you start the timer

while true do
  local event, argument = os.pullEvent()

  if (event == "timer") and (argument == yourTimer) then
	break -- exit from loop when timer ended
  end
end

OR, if you need something simplier, just use sleep(m), which pause the script for 'm' seconds
Thanks, this works perfectly!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users