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

Help

Started by SooBahkCode, 10 March 2015 - 02:22 AM
SooBahkCode #1
Posted 10 March 2015 - 03:22 AM
I'm trying to figure out how much time has passed since a certain point in my program so I can use it to start an elseif .. then statement but I don't know how. Help.
:-) #2
Posted 10 March 2015 - 03:57 AM
well maybe don't type "help" as the title because some people will just look at it and say "this title isn't very informative so im just not going to read it" i learned that from other forums…
Edited on 10 March 2015 - 02:58 AM
Bomb Bloke #3
Posted 10 March 2015 - 04:30 AM
os.clock() returns the amount of time the system's been running. If you store that value in a variable, then check up on it later and compare, you can tell how much time has gone past.

However, often you'll be better off using timers for this sort of thing.
SooBahkCode #4
Posted 10 March 2015 - 10:08 PM
Well I can't seem to figure out how much time has passed since a certain point in the program. Also I've tried looking on youtube for timers that are for something other than Redstone circuits but I can't find any.
SpencerBeige #5
Posted 10 March 2015 - 10:16 PM
os.startTimer(TIME)
os.pullEvent("timer")
KingofGamesYami #6
Posted 10 March 2015 - 11:14 PM
Ah, could we see the program in question, and what you want it to do? I bet there's a very easy solution.
SooBahkCode #7
Posted 10 March 2015 - 11:19 PM
this is the program: http://pastebin.com/raw.php?i=FZtbVTeB
SooBahkCode #8
Posted 10 March 2015 - 11:32 PM
If u look at the program, you will notice a elseif .. then statement that hasn't been finished. This is were i am having my problems.
KingofGamesYami #9
Posted 10 March 2015 - 11:47 PM
Are you writing a program frame by frame? That'd take forever… and wouldn't be worth the time. You need to keep track of your character.

Take a look at this example (It limits your framerate to 10 FPS, but does not inhibit movement):


local playerx, playery = 1, 1
local id = os.startTimer( 0 )
while true do
   local event = { os.pullEvent() }
   if event[ 1 ] == "timer" and event[ 2 ] == id then
     term.clear()
     term.setCursorPos( playerx, playery)
     term.write( "x" )
     id = os.startTimer( 0.1 )
   elseif event[ 1 ] == "key" then
     if event[ 2 ] == keys.up then
       playery = playery - 1
     elseif event[ 2 ] == keys.down then 
       playery = playery + 1
     elseif event[ 2 ] == keys.left then
       playerx = playerx - 1
     elseif event[ 2 ] == keys.right then
       playerx = playerx + 1
     end
   end
end
Edited on 10 March 2015 - 10:49 PM
SooBahkCode #10
Posted 10 March 2015 - 11:51 PM
Also i was making this game as a means to teach myself how to use entire preset images and animations with timers and button-pressing. i was also hoping to keep this to being able to run on the basic cc computer. Can u help me figure out how to figure out how much time has passed since one point in the program? if you look into the hill() function, there is some code for creating a sort-of quick-time event. i was trying to get it to were if the player doesnt press the spacebar in time, the animation for them dying would appear. sadly, i am unable to get the whole timing your button press down.
KingofGamesYami #11
Posted 10 March 2015 - 11:54 PM
Oh, that's easy enough. Here's a program that times out if you don't press something for five seconds:


local id = os.startTimer( 5 )
while true do
  local event = {os.pullEvent()}
  if event[ 1 ] == "timer" and event[ 2 ] == id then
    print( "timed out!" )
    break
  elseif event[ 1 ] == "mouse_click" or event[ 1 ] == "key" then
    print( event[ 1 ] .. " detected!  Resetting Timout" )
    id = os.startTimer( 5 )
  end
end
Edited on 11 March 2015 - 01:10 AM
SooBahkCode #12
Posted 10 March 2015 - 11:56 PM
currently, my skill of coding in computercraft is limited to making Operating Systems for cc Basic Computers.
PixeltonOS:http://pastebin.com/e75RjgS5
PixeltonOSPE:http://pastebin.com/jfjNAYjJ
SooBahkCode #13
Posted 11 March 2015 - 12:23 AM
your timing program isnt working. just tried it. put down exactly the same code.
KingofGamesYami #14
Posted 11 March 2015 - 01:30 AM
Ah, sorry. Forgot to actually call os.pullEvent… Edited the code in my post.

Also: I dunno if I'd call that an OS. I'd call OneOS and LyqydOS true OSs, as they have useful features that CraftOS does not.

You should be able to program a simple game before programming an OS.
SooBahkCode #15
Posted 11 March 2015 - 02:57 AM
1. I would consider those basic OS, even though they don't have anything better than CraftOS.
2. An OS is an Operating System, and they aren't always upgrades from the one you currently use.
3. I need to be able to define the key that has to be pressed otherwise that bit of code you wrote up isn't going to help in the slightest.
Thank you for trying. (But not for insulting the currently under-development PixeltonOS and PixeltonOSPE.)
KingofGamesYami #16
Posted 11 March 2015 - 12:37 PM
I apologize for insulting your OSs, but I think pretty much everyone on the forums would agree: You should be able to program something like this before making an OS.

You can easily define the key that has to be pressed by editing this line:

  elseif event[ 1 ] == "mouse_click" or event[ 1 ] == "key" then
SooBahkCode #17
Posted 11 March 2015 - 12:42 PM
what part of the line do i edit? i need the key being pressed to be key 57 a.k.a the spacebar. btw those OSs are still under development but i needed to halt that project so i could get this one done for school.
KingofGamesYami #18
Posted 11 March 2015 - 12:43 PM
elseif event[ 1 ] == "key" and event[ 2 ] == 57 then

Like this ^
Bomb Bloke #19
Posted 11 March 2015 - 10:58 PM
Don't use 57, though. Use keys.space. Much more readable.
SooBahkCode #20
Posted 11 March 2015 - 11:01 PM
Still didn't work.
Bomb Bloke #21
Posted 11 March 2015 - 11:37 PM
What didn't work? Show the state of the code you're trying to run.
SooBahkCode #22
Posted 23 March 2015 - 02:07 AM
now it wont accept input period.
Dragon53535 #23
Posted 23 March 2015 - 05:14 AM
now it wont accept input period.
Then post the code so we can see what went wrong. That way we can help. We're not psychic here so we can't tell you how to fix your problem without seeing the code.