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

Sleep Function and How to change image?

Started by Noodle, 18 June 2012 - 06:45 PM
Noodle #1
Posted 18 June 2012 - 08:45 PM
I've been searching on google on how to do a sleep function.
I have found things but they won't work?

I know computercraft has a sleep( seconds ) but I want to make this for myself…
How would you do this?



Things I have already tried
local clock = os.clock
function sleep(n)  -- seconds
		local t0 = clock()
		while clock() - t0 <= n do end
end

Also, how do I change images?
I have it redefine to sprite = "guy.png" – Different API
Example, when you hit a key it should change images but it doesnt.
Code:

-- DOWN
if event.y > 278 and event.y < 310 and event.x > 32 and event.x < 64 then
  print( "DOWN" )
  local sprite = "guy.png"
  for i = 1,5 do
   char.y = char.y + 1
  end
end
Xfel #2
Posted 18 June 2012 - 08:51 PM
Why would you want this? You could just use the original one. The code you provided will most probably not work, as cc doesn't allow programs to run for more than a little time without a yield.
Noodle #3
Posted 18 June 2012 - 09:03 PM
Xfel, no need to question. How would you do this?
This'll help, Timeout doesn't exist.
MysticT #4
Posted 18 June 2012 - 09:11 PM
Is this for CC? Cause it doesn't look like.
If it is, you can use a timer for the sleep function (like sleep does):

function sleep(n)
  local timer = os.startTimer(n)
  while true do
    local evt, arg = os.pullEvent("timer")
    if arg == timer then
      break
    end
  end
end

Also, how do I change images?
I have it redefine to sprite = "guy.png" – Different API
Example, when you hit a key it should change images but it doesnt.
Code:

-- DOWN
if event.y > 278 and event.y < 310 and event.x > 32 and event.x < 64 then
  print( "DOWN" )
  local sprite = "guy.png"
  for i = 1,5 do
   char.y = char.y + 1
  end
end
Change images?

If this is not for CC, go somewhere else. Sorry, but this is a CC forum after all.
Noodle #5
Posted 18 June 2012 - 09:21 PM
@MysticT
Changing images, I'm saying like redefining variables. It doesn't seem to work.
How would I redefine the variable in that code?
MysticT #6
Posted 18 June 2012 - 09:28 PM
The problem is that you'r creating a new local variable, not redefining it. Local variables only exist on the block they are defined.
Example:

local a = 10

while true do
  local b = 1
  if (a == 10) then
	local c = "Hello"
	print(c)
  end
end
a is created at program start, and exists during the whole program.
b is created in the while loop, and exists during one iteration of the loop. When the loop is repeated, the variable is created again.
c is crated in the if statement inside the while loop. Same as b, will be created with each iteration, but only if the if condition is met (wich will happen always in this code).
So, if the variable "sprite" already exists, you shouldn't be declaring it again as local.

local sprite = "" -- variable defined for the whole program

if event.y > 278 and event.y < 310 and event.x > 32 and event.x < 64 then
  print( "DOWN" )
  sprite = "guy.png" -- change the variable's content/value. No local here
  for i = 1,5 do
   char.y = char.y + 1
  end
end
Noodle #7
Posted 18 June 2012 - 09:31 PM
@MysticT
Hmm… I did that but it didn't seem to work?

EDIT: The original didn't work. I had it at the beginning
local sprite = "" – Sprite

The code without the local

EDIT: Tried again, still didn't work.
Want me to send the whole entire code piece?
MysticT #8
Posted 18 June 2012 - 09:32 PM
Well, it's pretty hard to know what's wrong without the code. If you can, post the code so we can help.
Noodle #9
Posted 18 June 2012 - 09:41 PM
Pastebin: http://pastebin.com/sgGW39u3
The game uses an custom api so…
MysticT #10
Posted 18 June 2012 - 09:45 PM
The problem is that you change your sprite variable, so it contains a new string, but you never change the char variable, wich contains the actual image.
Noodle #11
Posted 18 June 2012 - 10:03 PM
What?
I have it
sprite = "guy.png"
char = display.newImage( sprite )

So when you change the sprite var it should change char

EDIT: I GET IT!!
You have to re-redefine it..
char is already set for the sprite
If you don't reload the char it can't receive the new sprite var!
Pinkishu #12
Posted 18 June 2012 - 10:06 PM
*guesses this is for this Love2D Lua or what iwas called* :(/>/>
Noodle #13
Posted 18 June 2012 - 10:09 PM
@Pinkishu
Nope. I'm making an app.
Love2D I discovered years ago when I was trying to make a 3D game.
Search on google: Wiki - Game engines
Love2D shows there

But this isn't Love2D because the chances of making money from a flash game are more slim than posting an app for iPhone on the app store.

@MysticT
Thankyou!
Noodle #14
Posted 19 June 2012 - 12:13 AM
Hmm.. How about that sleep function?
MysticT #15
Posted 19 June 2012 - 12:21 AM
It greatly depends on the apis/platform, so we can't help you. Maybe in the documentation of the api you use there's something about it.
Noodle #16
Posted 19 June 2012 - 04:04 AM
@MysticT
I did see this:
local clock = os.clock
function sleep(n)  -- seconds
			    local t0 = clock()
			    while clock() - t0 <= n do end
end

But nothing else?
MysticT #17
Posted 19 June 2012 - 05:26 PM
@MysticT
I did see this:
local clock = os.clock
function sleep(n)  -- seconds
				local t0 = clock()
				while clock() - t0 <= n do end
end

But nothing else?
Unless there's some native function, that's the only way.
KaoS #18
Posted 22 June 2012 - 01:35 PM
there is an issue with that timer. it will keep looping as you do not redefine the t0 variable, and it is better to set the clock in the function do it sets it when you run the timer. try:

function sleep(n)
local clock=os.clock()
while clock-os.clock() <=n do end
end

I am not sure what platform you are using but that should work
let me know if there are any errors etc
KaoS #19
Posted 22 June 2012 - 01:36 PM
sorry flip the clock-os.clock() to make os.clock()-clock. my bad
MysticT #20
Posted 22 June 2012 - 04:45 PM
there is an issue with that timer. it will keep looping as you do not redefine the t0 variable, and it is better to set the clock in the function do it sets it when you run the timer. try:

function sleep(n)
local clock=os.clock()
while clock-os.clock() <=n do end
end

I am not sure what platform you are using but that should work
let me know if there are any errors etc
You do realize that it's the same code as before, just that you renamed the variable?