Calling a lua function at random time intervals in love2d - lua

So basically I want to call a function in lua at random time intervals in love2d. Iam clueless of how to do this so any help is appreciated.

local t = math.random(MIN, MAX);
function love.update(dt)
t = t - dt
if t <= 0 then
FUNCTION()
t = math.random(MIN, MAX);
end
end

try:
function love.load()
Time = math.random(1, (max number here))
end
function love.update(dt)
Time = Time - dt
if Time < 0 then
-- magic
Time = math.random(1, (max number here))
end
end

Related

How to make two functions work separately and together in luascript? -software logitech GHUB

I want this code to work together and separately when scrolllock is enabled. One using the mouse button(3) and the other the mouse button(2). Working separately they are working, however when I activate one the other does not work. I did a lot of research and it seems that there is a way to do it using a "coroutine", but I didn't find out how.
EnablePrimaryMouseButtonEvents(true);
function OnEvent(event, arg)
local r = 350
local x = 350
local s = 50
if IsKeyLockOn("scrolllock" )then
if IsMouseButtonPressed(3) then
local positions = { -- Move `MoveMouseRelative` args to an 2d array.
{-r,x},
{-r,x},
{r,x},
{r,x},
{r,-x},
{r,-x},
{-r,-x},
{-r,-x},
}
local index = 1
repeat
MoveMouseRelative(positions[index][1], positions[index][2])
Sleep(s)
index = (index % #positions) + 1 -- loop index when it reaches the array length.
until not IsMouseButtonPressed(3)
end
if IsMouseButtonPressed(2) then
repeat
PressAndReleaseKey("p")
Sleep(750)
until not IsMouseButtonPressed(2)
end
end
end
You should write each "task" in its own coroutine and write a coroutine-friendly implementation of Sleep().
local orig_Sleep = Sleep
local function Sleep(delay)
local co, is_main = coroutine.running()
if not is_main and co then
local tm = GetRunningTime() + delay
repeat
coroutine.yield(true)
until GetRunningTime() >= tm
else
orig_Sleep(delay)
end
end
local tasks = {}
local function add_task(task_function)
table.insert(tasks, coroutine.wrap(
function()
repeat
task_function()
coroutine.yield()
until nil
end
))
end
local function task_manager()
repeat
Sleep(5)
local active -- true when at least one task is inside "Sleep" now
for _, resume_task in ipairs(tasks) do
active = resume_task() or active
end
until not active
end
add_task(
function()
if IsKeyLockOn"scrolllock" and IsMouseButtonPressed(3) then
local r = 350
local x = 350
local s = 50
local positions = { -- `MoveMouseRelative` args
{-r,x},
{-r,x},
{r,x},
{r,x},
{r,-x},
{r,-x},
{-r,-x},
{-r,-x},
}
local index = 1
repeat
MoveMouseRelative(positions[index][1], positions[index][2])
Sleep(s)
index = (index % #positions) + 1 -- loop index when it reaches the array length.
until not IsMouseButtonPressed(3)
end
end
)
add_task(
function()
if IsKeyLockOn"scrolllock" and IsMouseButtonPressed(2) then
repeat
PressAndReleaseKey("p")
Sleep(750)
until not IsMouseButtonPressed(2)
end
end
)
EnablePrimaryMouseButtonEvents(true)
function OnEvent(event, arg)
task_manager()
end

How can I use non-integer without using float in LUA

In LUA for MoveMouse i can't use FLOAT. So i need to get around it. I don't know how to do this. So i need help.
function OnEvent(event, arg)
local multiplier = 2
if smth == CODE then
MoveMouseRelative(-1*multiplier, 0.1*multiplier)
Sleep(10)
MoveMouseRelative(-1*multiplier, 0.2*multiplier)
Sleep(10)
You can store fractional value in a variable and pass math.floor(value) to your functions.
Unspent fractional part will be accumulated for future using.
local x_frac, y_frac = 0
local function MoveMouseRelativeFractional(x, y)
x_frac = x_frac + x
y_frac = y_frac + y
local x_int = math.floor(x_frac)
local y_int = math.floor(y_frac)
x_frac = x_frac - x_int
y_frac = y_frac - y_int
if x_int ~= 0 or y_int ~= 0 then
MoveMouseRelative(x_int, y_int)
end
end
function OnEvent(event, arg)
local multiplier = 2
if smth == CODE then
MoveMouseRelativeFractional(-1*multiplier, 0.1*multiplier)
Sleep(10)
MoveMouseRelativeFractional(-1*multiplier, 0.2*multiplier)
Sleep(10)

How to run code whenever a value reaches a multiple of 50 in lua

Pretty simple, dont really have any code to give but just as an example:
local n = 0
local clock = os.clock
function sleep(n)
local t0 = clock()
while clock() - t0 <= n do end
end
while True do
sleep(1)
n = n + 1
(code to run when 'n' is a multiple of 50)
print("n is a multiple of 50")
end
Pretty simple, Any help appreciated!
not to wait for a long time, I took a small values:
local function sleep (a)
local sec = tonumber(os.clock() + a)
while os.clock() < sec do
end
end
local n = 0
while true do -- true~=True
sleep(1)
n = n + 1
print(n)
if n%5==0 then
print(n," - n is a multiple of 5")
end
if n>=10 then break end
end
and more: you need to wait for the result, when the script finishes working, such a feature of the Lua interpreter.

How to time interval between event in lua

I am writing a simple script in lua. If the LED is on, it will increment the counter by 1. If the led is off for more than 1 seconds, it reset the counter.
So how exactly do we time event like that in lua ?
This is what i have and been testing multiple ways so far.
function ReadADC1()
local adc_voltage_value = 0
adc_voltage_value = tonumber(adc.readadc()) * 2 -- 0.10 --get dec number out of this -- need to know where package adc come from
--convert to voltage
adc_voltage_value = adc_voltage_value *0.000537109375 --get V
adc_voltage_value = math.floor(adc_voltage_value *1000 +0.5) --since number is base off resolution
--print (adc_voltage_value)
return adc_voltage_value
end
-- end of readADC1() TESTED
function interval()
local counter1 =0
ledValue = ReadADC1()
if (ledValue >= OnThreshHold) then
ledStatus = 1
t1=os.time()
else
ledStatus = 0
t2 = os.time()
end
--counter1 =0
for i=1,20 do
if (ledStatus == 1) then -- if led is off for more than 1 second, reset counter = 0
counter1 = counter1 + 1
elseif ((ledStatus ==0 and ((os.difftime(os.time(),t2)/100000) > 1000))) then -- increment counter when led is on
counter1 = 0
end
end
print (counter1)
end
I know for sure the logic for interval is wrong since os.time return a huge number (i assume it in psecond instead of second).
Any suggest or solution is welcome. I tried vmstarttimer and vmstoptimmer before this but not sure how it work.
EDIT:
function counter()
local ledValue = ReadADC1()
local t1 = os.clock()
while true do
local t2 = os.clock()
local dt = t2 - t1
t1 = t2
if ((ledValue < OnThreshHold) and (dt < 1)) then -- if led is off for less than 1 second
ledCounter = ledCounter + 1
elseif ((ledValue < OnThreshHold) and (dt > 1)) then-- if led is off for more than 1 second
ledCounter = 0;
else
ledCounter = ledCounter
end
print (ledCounter)
end
end
Ultimately, it will be return ledCounter instead of print counter since I will plug the value of counter to another function that will print a message correspond to the number of counter
You could use os.clock which returns your programs runtime since start in seconds.
Returns an approximation of the amount in seconds of CPU time used by the program.
Source
This function could be used in this way.
local t1 = os.clock() -- begin
local t2 = os.clock() -- end
local dt = t2 - t1 -- calulate delta time
-- or looped
local t1 = os.clock() -- begin
while true do
local t2 = os.clock() -- end
local dt = t2 - t1 -- calulate delta time
t1 = t2 -- reset t1
-- use dt ...
end
-- or wait for time elapsed
-- runs until 1 second passed
local t1 = os.clock()
while (os.clock() - t1) < 1 do
-- do stuff while dt is smaller than 1
-- could even reset timer (t1) to current to
-- repeat waiting
-- t1 = os.clock() | ...
end
-- logic for your example
function counter()
local time = os.clock()
local lastLedOn = false
local counter = 0
while true do
if os.clock() - time > 1.0 then
break
end
if getLedValue() == on then -- replace
time = os.clock()
if not lastLedOn then
lastLedOn = true
counter = counter + 1
-- print(counter) | or here if you want to print repeatedly
end
end
end
print(counter)
end -- was unable to test it

How to have currentMilis and previousMillis like arduino in Lua script

I am writing an extremely simple script in Lua using simcom GPU.
It will count how many times a led flashing and return a string correspond to counter number. So far i have using os.clock() to do the job but the result is not as I expected.
function counter()
local t1 = os.clock()
while (ReadADC1() > 98) do
local t2 = os.clock()
local dt = t2 - t1
t1 = t2
local ledValue = ReadADC1()
if ((ledValue >= OnThreshHold) and (dt < 1)) then -- if led is on for less than 1 second
ledCounter = ledCounter + 1
--vmsleep(100)
elseif ((ReadADC1() < OnThreshHold) and (dt > 2.3)) then-- if led is off for more than 2 second
ledCounter = 0;
--vmsleep(100)
else
ledCounter = ledCounter
end
--print (ledValue,"\r\n")
end
print(ledCounter,"\r\n")
--return ledCounter
end
For this case, if i use this counter() function, it only work if the value from ReadADC() is input for short time then stop. It wont work if adc read continuously flow of data.
In arduino, the solution is extremely easy since you can use currentmillis and previousMillis to determine time pass between flashes. So in lua, do we have anything similar ?

Resources