How to do a delay on a while loop loop in lua? - lua

I'm Making something in Lua (I'm new to Lua so I'm not the best) and I was wondering how to Put a 1-second delay on while loop.
I've already tried to put a sleep(1) or a wait(1) but those just still caused the same error (lag)
local x = 0
while true do
--execute example code
print(x)
x=x+1
-- put a wait so it waits before doing it again
end

In Roblox, you can make a loop execute in the background by using 'spawn' to make the code execute in a different thread.
local x = 0
-- make it loop in a background thread forever
spawn(function()
while true do
print(x)
x=x+1
wait()
end
end)
print( "I can execute immediately" )

Try this:
while true do
print(x)
x=x+1
wait(1)
end

Related

How to turn off a function after a desired amount of time?

I know my question would sound silly but I'm new to Lua so I'm trying to make the best practice as I can.
function wait(n)
local start = os.time()
repeat until os.time() > start + n
end
function hi(x)
while x do
print("Hi")
wait(.5)
end
end
hi(true)
For example, I want to turn off the function "hi" after running for 6 seconds and re-enable it after stopping for 2 seconds, how can I do it? Thank you so much!
Try this changes...
function wait(n)
local start = os.clock()
repeat until os.clock() > start + n
end
function hi(x)
for i = 1, x do
print("Hi")
wait(.5)
end
end
hi(4) -- With the hardcoded wait(.5) this will end after 2s
One warning.
Lua is fast so wait() will run in high performance mode.
Let it run by hi(120) for a minute will also run your fans in high cooling mode.
...it is better to have a more CPU friendly wait/sleep.
Easy Peasy with LuaJIT: https://luajit.org/ext_ffi_tutorial.html

Strange "attempt to perform arithmetic on a function value" error in lua

I'm writing a BASIC interpreter in plain Lua, have hit a wall when writing my SLEEP X function.
I'm no expert but nothing looks wrong here...
function s(time)
local time=tonumber(time)
if useSleep then sleep(time) elseif useWait then wait(time) else
--oh no
--we will try our best
local ct=os.time+time
repeat until(os.time>=ct)
end end
--test
s(5)
You need to call os.time, with os.time().
os.time is the function.
os.time() is its result.

How to break out of a loop in x seconds

I am trying to modify a FiveM script and I am trying to break out of a loop in Lua after 4 seconds but I don't know how.
I don't know what I am doing and need some help.
Citizen.CreateThread(function()
function drawscaleform(scaleform)
scaleform = RequestScaleformMovie(scaleform)
while not HasScaleformMovieLoaded(scaleform) do
Citizen.Wait(0)
end
PushScaleformMovieFunction(scaleform, "SHOW_POPUP_WARNING")
PushScaleformMovieFunctionParameterFloat(500.0)
PushScaleformMovieFunctionParameterString("ALERT")
PushScaleformMovieFunctionParameterString("~b~Peacetime Active")
PushScaleformMovieFunctionParameterString("This Means No Priority Calls")
PushScaleformMovieFunctionParameterBool(true)
PushScaleformMovieFunctionParameterInt(0)
PopScaleformMovieFunctionVoid()
DrawScaleformMovieFullscreen(scaleform, 255, 255, 255, 255, 0)
end
while true do
Citizen.Wait(0)
drawscaleform("POPUP_WARNING")
end
end)
I would like to break out of the while true after 4 seconds
Most likely some combination of Lua’s break command, setting a condition on your while loop that more clearly communicates the loop’s intention (other than just while true...) and better using FiveM’s Citizen.Wait() function. The documentation for that function here says the argument is the number of milliseconds to pause the current execution thread.
Take some time to understand these elements, the code you are trying to modify, and experiment. SO won’t just code for you.
There is a FiveM function Citizen.SetTimeout to call a function after a period has elapsed. Here is one (untested) way you could use it:
Citizen.CreateThread(function()
function drawscaleform(scaleform)
...
end
local wait = true
Citizen.SetTimeout(4000, function() wait = false end)
while wait do
Citizen.Wait(0)
drawscaleform("POPUP_WARNING")
end
end)

how to resume a coroutine in lua?

I was try to use coroutine in lua, I tried the code below, repl.it here https://repl.it/repls/WordyWonderfulVisitor and it does not print the list content in the loop.
local list = {1,2,3};
local function iter()
for i, v in ipairs(list) do
print(i, v)
coroutine.yield();
end
end
local co = coroutine.create(iter);
coroutine.resume(co);
coroutine.resume(co);
-- iter();
what's wrong with my code ?
There is nothing wrong with your code. It prints 1 1 and 2 2 as expected and the results are the same in Lua 5.1-5.4 versions.
If you want to see one more result 3 3, then you need to call resume one more time. You can also check the status of the coroutine by using coroutine.status, so after the first two executions you'll get "suspended" and after the execution is completed you'll get "dead".

Pause iteration

I have Lua table, t, which I iterate:
for k, v in pairs(t) do
b = false
my_func(v)
end
and want iteration to pause until b global variable changes to true
Is it possible is Lua?
Unless you're in a coroutine, there's no concept of a Lua variable changing value without your code doing it. So you would be pausing until something that can't possibly happen happens. Lua is inherently single-threaded.
As previously stated, you can use a coroutine to do this, but you'll have to modify your code accordingly:
function CoIterateTable(t)
for k, v in pairs(t) do
b = false
my_func(v)
while(b == false) do coroutine.yield() end
end
end
local co = coroutine.create(CoIterateTable)
assert(co.resume(t))
--Coroutine has exited. Possibly through a yield, possibly returned.
while(co.running()) do
--your processing between iterations.
assert(co.resume(t))
end
Note that changing the table referenced by t between iterations is not going to do something useful.

Resources