-1

ok, Im almost completely new to lua and computercraft but I have alot of creativity. I'm trying to write code that will reprint a variable every second. here is what I have so far:

display = "Loading..."
While true do
 sleep(1)
 term.clear()
 term.setCursorPos(1,1)
 print (display)
end
sleep(3)
display = "hello"

I want to use it to render a 2d game, the "display" variable would change often and thus why i want it to be updated every second.

It does indeed refresh every second when I run the code but for some reason I cant seem to get the "display" variable to change after 3 seconds to test it.

What am I doing wrong?

Anima-t3d
  • 3,431
  • 6
  • 38
  • 56
ryze halberd
  • 137
  • 7
  • Possibly related to [this SO question](http://stackoverflow.com/questions/11926440/asynchronous-delay-in-lua-like-javascript-settimeout) – Anima-t3d Feb 11 '15 at 09:47

2 Answers2

1

while true is an infinite loop. The script never reaches sleep(3).

Perhaps you want to replace while true with for i=1,3.

lhf
  • 70,581
  • 9
  • 108
  • 149
  • The problem with that Is that I want the loop to run the whole time, eventually I want to use it to render a 2d game, the "display" variable would change often and thus why i want it to be updated every second. – ryze halberd Feb 11 '15 at 01:03
0

I am not experienced in Lua, but this might be a solution: answer on SO

In the UI thread, run:

while ((status=lua_resume(L_coroutine, 0)) == LUA_YIELD) {
  semaphore_wait(); /* whatever the appropriate C# call is */
}

"Wait for response" should look something like:

while not results[my_result] do
  coroutine.yield()
end

The "incoming message" function should look like the following in Lua:

results[cur_result]=parsed_message
Community
  • 1
  • 1
Anima-t3d
  • 3,431
  • 6
  • 38
  • 56
  • I think it should be a comment – Bartek Banachewicz Feb 11 '15 at 11:43
  • @BartekBanachewicz a comment doesn't provide the space to explain an answer to this depth. But yeah it's not a standard answer, but might be just what the OP needs in order to solve his problem. – Anima-t3d Feb 11 '15 at 21:35
  • Thank you for your answer. but at the current point that i'm at, the post you linked to is a bit over my head. I will still mark it as correct as it may help other viewers that are more experienced than I am. – ryze halberd Feb 15 '15 at 03:49
  • @ryzehalberd the most important thing is to change the `while true` to `while variable` that variable should return `false` at some point in the loop, so that the while can stop and code will move to the `sleep(3)` after the `end` of the while. – Anima-t3d Feb 15 '15 at 09:46