Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i got an issue where i'm trying to make a wait function in lua but it's erroring does anyone know why and how to fix this issue thankyou to those who helped :)

Lua
<pre>--  Function for Vanila Lua

local function sleep(seconds)
  local waittime = os.clock() + seconds/10
  repeat until os.clock() > waittime
  return seconds
end

local returnedWait = sleep(seconds)

print(returnedWait)


Error

/usr/bin/lua: main.lua:22: attempt to perform arithmetic on a nil value (local 'seconds')
stack traceback:
	main.lua:22: in local 'sleep'
	main.lua:27: in main chunk
	[C]: in ?


What I have tried:

I have tried to debug the issue google searching and none of that fixed my issue
Posted
Updated 7-May-22 8:34am
v2
Comments
Richard MacCutchan 2-May-22 12:17pm    
The message is saying that seconds is not defined within the scope of the above code.
Anthony Durk 2-May-22 12:40pm    
it works but the problem is in a while loop it's not waiting it's timing out the script

local function wait(seconds)
local waittime = os.clock() + seconds/10
repeat until os.clock() > waittime
return seconds
end

local returnedWait = wait(10)

print(returnedWait)

while true do
wait(10)
print("this is printing every 10 seconds")
end

From Lua 5.3 Reference Manual[^]:
os.clock ()
Returns an approximation of the amount in seconds of CPU time used by the program.

It doesn't seem to me the most appropriate function for implementing a delay.
Try
Lua
local function wait(seconds)
  local tend = os.time() + seconds

  while os.time() < tend do
    -- just waste CPU cycles here
  end
  return seconds
end

local returnedWait = wait(10)
print(returnedWait)

while true do
  wait(10)
  print("this is printing every 10 seconds")
end

Please note, in order to obtain tinier time intervals, you have to call underlying OS (specific) functions.
 
Share this answer
 
Comments
Patrice T 7-May-22 14:09pm    
+5
CPallini 9-May-22 2:01am    
Thank you.
Look at your code: you do not give a value to the variable seconds when you pass it to your function.
Try this:
Lua
local function sleep(seconds)
  local waittime = os.clock() + seconds/10
  repeat until os.clock() > waittime
  return seconds
end

local returnedWait = sleep(60)
 
Share this answer
 
You method of waiting is a bad idea.
Lua
--  Function for Vanila Lua

local function sleep(seconds)
  local waittime = os.clock() + seconds/10
  repeat until os.clock() > waittime
  return seconds
end

This code is waiting from the user point of view, but it is continuously running from a hardware point of view, and thus it is hogging resources (on multitasking device, it makes a difference), on calculator, it is draining batteries.

You should study the usage of timers, they allow real sleep of hardware.
Category:timer - Inspired-Lua Wiki[^]
on.timer - Inspired-Lua Wiki[^]
Category:Events - Inspired-Lua Wiki[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900