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

Sleep without Sleeping

Started by Wing, 13 August 2012 - 06:44 PM
Wing #1
Posted 13 August 2012 - 08:44 PM
Hey guys,
me again lol, just wondering if there was a way to count seconds and then execute something without slowing/stoping the rest
of the program! I can post the code if needed also.

So basicly all I want is to wait one second then do: something something something.
Will everything else is still going on.

Thanks to anyone who may help!
Cranium #2
Posted 13 August 2012 - 08:52 PM
you can use os.startTimer(seconds), then use os.pullEvent("timer") to have it stop at a certain timer. I think it goes like this:

os.startTimer(10)
--your code here
if os.pullEvent("timer") then
os.reboot()
end
Never really used the timer, and you might get a better answer right after me… :P/>/>
OmegaVest #3
Posted 13 August 2012 - 08:56 PM
Errr. Not in any simple format. You could use timer events, or alarm events. I suppose, if nothing else was attached to the terminal you could use a redstone pulse and then a further redstone event. But this all requires your program to use os.pullEvent somewhere, and have a fast enough loop to catch it.

On the other hand, you could use a parallel function to sleep, then execute. This is still complicated, and there are a myriad of reasons why it would not work, but I don't feel like going into those.
Wing #4
Posted 13 August 2012 - 09:46 PM
I am already using os.pullEvent actually, I'm using it to move the character when a key is pressed though
Cranium #5
Posted 13 August 2012 - 10:03 PM
You can still use it for other things. If you use it in a function, and start and end your timer in that function, you can use the parallel API to run everything until it receives the stop, and stop everything.
Wing #6
Posted 13 August 2012 - 10:32 PM
Lol I still don't understand, I would need to actually talk to one of you anyway here's my code.




local direction = ">"
local x1, y1 = term.getSize()
local currentX = x1/2
local currentY = y1/2
term.clear()
textutils.slowPrint("Navigator Game by: Wing")
textutils.slowPrint("Use W,A,S,D to Navigate the world")
textutils.slowPrint("Avoid the Moving Monsters")
textutils.slowPrint("Collect the Star to win!")
sleep(2)
function collision(cX, cY, sign)
term.setCursorPos(cX, cY)
write(sign)
if cX == currentX and cY == currentY then
    if direction == "^" then
		    currentY = currentY +1
	    elseif direction == "v" then
		    currentY = currentY -1
	    elseif direction == ">" then
		    currentX = currentX -1
	    elseif direction == "<" then
		    currentX = currentX +1
	    end
draw()
end
end
function map()
----------------------- Vertical Left
collision(1,1, "O")
collision(1,2, "|")
collision(1,3, "|")
collision(1,4, "|")
collision(1,5, "|")
collision(1,6, "|")
collision(1,7, "|")
collision(1,8, "|")
collision(1,9, "|")
collision(1,10, "|")
collision(1,11, "|")
collision(1,12, "|")
collision(1,13, "|")
collision(1,14, "|")
collision(1,15, "|")
collision(1,16, "|")
collision(1,17, "|")
collision(1,18, "O")
-------------------------- Right Vertical
collision(49,1, "0")
collision(49,2, "|")
collision(49,3, "|")
collision(49,4, "|")
collision(49,5, "|")
collision(49,6, "|")
collision(49,7, "|")
collision(49,8, "|")
collision(49,9, "|")
collision(49,10, "|")
collision(49,11, "|")
collision(49,12, "|")
collision(49,13, "|")
collision(49,14, "|")
collision(49,15, "|")
collision(49,16, "|")
collision(49,17, "|")
collision(49,18, "0")
-------------------------- Top Horizontal
collision(2,1, "-")
collision(3,1, "-")
collision(4,1, "-")
collision(5,1, "-")
collision(6,1, "-")
collision(7,1, "-")
collision(8,1, "-")
collision(9,1, "-")
collision(10,1, "-")
collision(11,1, "-")
collision(12,1, "-")
collision(13,1, "-")
collision(14,1, "-")
collision(15,1, "-")
collision(16,1, "-")
collision(17,1, "-")
collision(18,1, "-")
collision(19,1, "-")
collision(20,1, "-")
collision(21,1, "-")
collision(22,1, "-")
collision(23,1, "-")
collision(24,1, "-")
collision(25,1, "-")
collision(26,1, "-")
collision(27,1, "-")
collision(28,1, "-")
collision(29,1, "-")
collision(30,1, "-")
collision(31,1, "-")
collision(32,1, "-")
collision(33,1, "-")
collision(34,1, "-")
collision(35,1, "-")
collision(36,1, "-")
collision(37,1, "-")
collision(38,1, "-")
collision(39,1, "-")
collision(40,1, "-")
collision(41,1, "-")
collision(42,1, "-")
collision(43,1, "-")
collision(44,1, "-")
collision(45,1, "-")
collision(46,1, "-")
collision(47,1, "-")
collision(48,1, "-")
------------------------- Bottom Horizontal
collision(2,18, "-")
collision(3,18, "-")
collision(4,18, "-")
collision(5,18, "-")
collision(6,18, "-")
collision(7,18, "-")
collision(8,18, "-")
collision(9,18, "-")
collision(10,18, "-")
collision(11,18, "-")
collision(12,18, "-")
collision(13,18, "-")
collision(14,18, "-")
collision(15,18, "-")
collision(16,18, "-")
collision(17,18, "-")
collision(18,18, "-")
collision(19,18, "-")
collision(20,18, "-")
collision(21,18, "-")
collision(22,18, "-")
collision(23,18, "-")
collision(24,18, "-")
collision(25,18, "-")
collision(26,18, "-")
collision(27,18, "-")
collision(28,18, "-")
collision(29,18, "-")
collision(30,18, "-")
collision(31,18, "-")
collision(32,18, "-")
collision(33,18, "-")
collision(34,18, "-")
collision(35,18, "-")
collision(36,18, "-")
collision(37,18, "-")
collision(38,18, "-")
collision(39,18, "-")
collision(40,18, "-")
collision(41,18, "-")
collision(42,18, "-")
collision(43,18, "-")
collision(44,18, "-")
collision(45,18, "-")
collision(46,18, "-")
collision(47,18, "-")
collision(48,18, "-")
-------------------------- Center Above
--collision(25, 8, "-")
-------------------------- Walls 2*
collision(2, 3, "O")
collision(2, 4, "|")
collision(2, 5, "|")
collision(2, 6, "O")
collision(2, 10, "O")
collision(2, 11, "|")
collision(2, 12, "|")
collision(2, 13, "|")
collision(2, 14, "|")
collision(2, 15, "|")
collision(2, 16, "|")
collision(2, 17, "O")
------------------------ Walls 3*
collision(3, 3, "-")
collision(3, 8, "-")
collision(3, 10, "O")
collision(3, 11, "|")
collision(3, 12, "|")
collision(3, 13, "O")
collision(3, 15, "O")
collision(3, 16, "O")
collision(3, 17, "O")
------------------------ Walls 4*
collision(4, 3, "O")
collision(4, 4, "|")
collision(4, 5, "|")
collision(4, 6, "O")
collision(4, 8, "-")
collision(4, 16, "-")
------------------------ Walls 5*
collision(5, 3, "O")
collision(5, 4, "|")
collision(5, 5, "|")
collision(5, 6, "O")
collision(5, 8, "O")
collision(5, 9, "|")
collision(5, 10, "|")
collision(5, 11, "|")
collision(5, 12, "O")
collision(5, 13, "|")
collision(5, 14, "O")
collision(5, 16, "-")
end
function draw()
term.clear()
map()
term.setCursorPos(currentX, currentY)
write(direction)
end
while true do
draw()
local sEvent, button = os.pullEvent("key")
if (sEvent == "key") then
    if (button == 17) then
    currentY = currentY -1
    direction = "^"
    term.setCursorPos(currentX, currentY)
    write(direction)
	    elseif (button == 31) then
	    currentY = currentY +1
	    direction = "v"
	    term.setCursorPos(currentX, currentY)
	    write(direction)
		    elseif (button == 30) then
		    currentX = currentX -1
		    direction = "<"
		    term.setCursorPos(currentX, currentY)
		    write(direction)
			    elseif (button == 32) then
			    currentX = currentX +1
			    direction = ">"
			    term.setCursorPos(currentX, currentY)
			    write(direction)
    end
end
end
Cranium #7
Posted 13 August 2012 - 10:44 PM
So what are you trying to do? Have it run the game and stop at a certain point?
Wing #8
Posted 13 August 2012 - 11:08 PM
nono, that is so I can have a moving npc, but to do so, I need to move the cursor to the coord reprint the character and still have your character print as well… Therefore, I want the npc to move each 2 seconds or so without interfering with the actual player character. See what I mean?
Cranium #9
Posted 13 August 2012 - 11:26 PM
Wow, that's ambitious. I have no idea how you would do that…
Wing #10
Posted 14 August 2012 - 01:02 AM
sarcastic or….
Pharap #11
Posted 14 August 2012 - 01:20 AM
nono, that is so I can have a moving npc, but to do so, I need to move the cursor to the coord reprint the character and still have your character print as well… Therefore, I want the npc to move each 2 seconds or so without interfering with the actual player character. See what I mean?

I believe you are trying to create what is known in the gaming world as a roguelike.
In which case, you are going about it the wrong way.

The standard structure for a game is:
Initialise()
LoadResources()
Loop
{
Update{Keyevents(); Predictive Collision Checking(); Co-ord changes(); other logic();}
Draw{Draw to screen();}
}
Unload()

So ideally, for a console-mode roguelike, you're going to be wanting to have set characters to act as certain types of tiles.
Then you'll need the x and y variable for your character
Then you'll have a loop where key input is taken, collisions are checked for, variables are changed and then the map is redrawn before the loop repeats.
In this case, you might be able to do it without redrawing the whole map, but for a moving npc, you'd ideally have a timer event in the update portion of the loop. Set the OS timer to a logical interval, then on the timer timing out, update the npc's x/y and upon draw, the npc will be redrawn in the new location.

It works in pretty much all programming languages, which makes them all the more fun.
Cranium #12
Posted 14 August 2012 - 02:14 AM
sarcastic or….
No not sarcastic. Just saying it's completely out of my realm of knowledge.