Reputation: 9
this is my code
function masspoke(serverConnectionHandlerID, clientID, txt)
local error = ts3.requestClientPoke(serverConnectionHandlerID, clientID, txt)
sleep(1)
local error = ts3.requestClientPoke(serverConnectionHandlerID, clientID, txt)
if error == ts3errors.ERROR_not_connected then
ts3.printMessage(serverConnectionHandlerID, "Error: Not Connected")
return
elseif error ~= ts3errors.ERROR_ok then
print("Getting Error: " .. error .. "by poking the ID: " .. clientID)
return
end
ts3.requestClientPoke(serverConnectionHandlerID, clientID, txt)
end
and my error is
attempt to call global 'sleep' (a nil value)
Upvotes: 0
Views: 2673
Reputation: 29571
Lua does not provide a sleep function. There are several ways to implement one as discussed on the Lua wiki, definitely take a look. The socket based solution is a better option than a timer loop because it doesnt spin the CPU (keep it busy), but it requires that you install the third party sockets
library. In all solutions, your app can't do anything else, it is stuck waiting for the time to elapse.
You should ask if you really need to block your thread for a set time, ie why not instead loop until a certain condition is met. In your case this could be loop until either the OK status is obtained from the request, or a certain amount of time has elapsed. This way, the loop will end as soon as your "go ahead" condition is reached, but function will return if it takes too long to reach the condition. The other advantage of this is that you might be able to give the TS app a chance to process other events, each time through the loop.
It would look like this (not tested):
function masspoke(serverConnectionHandlerID, clientID, txt)
local start = os.clock() -- start timing
local MAX_WAIT_SECS = 1 -- seconds to wait for OK
local error = ts3.requestClientPoke(serverConnectionHandlerID, clientID, txt)
while error ~= ts3errors.ERROR_ok do
if os.clock() - start > MAX_WAIT_SECS then -- too long, give up!
if error == ts3errors.ERROR_not_connected then
ts3.printMessage(serverConnectionHandlerID, "Error: Not Connected")
else
print("Getting Error: " .. error .. "by poking the ID: " .. clientID)
end
return
end
error = ts3.requestClientPoke(serverConnectionHandlerID, clientID, txt)
end
-- now that ts poke is ok, do whatever:
ts3.requestClientPoke(serverConnectionHandlerID, clientID, txt)
end
I think the above is a cleaner approach, the intent is clearer. If you really want to sleep the main thread via the socket module, then put this before your masspoke()
function:
require "socket" -- you need to install socket lib manually
function sleep(sec)
socket.select(nil, nil, sec)
end
But there are several other options on http://lua-users.org/wiki/SleepFunction that should be worth trying (depending on your platform, and whether you want your prog to run on multiple platforms or not) that do not require the installation of a third-party library. Make sure to read that page carefully and try out what it shows.
Upvotes: 0
Reputation: 26774
There is no sleep
function in Lua, hence the error you are getting. The simplest way to achieve what you want is to use socket.sleep if you have access to luasocket. There are several other options listed in this and this SO questions.
Upvotes: 1