-
|
Hello! Is there a way currently to save a variable separately from a function? As a test, I tried to have a custom lua function derived from "Total This Period" where I compare Any idea / similar case on your hands? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
|
Hi. You do not actually need persistent state for this. You can use a function for example that always returns one data point containing the longest streak of all time. For example: local core = require("tng.core")
return function(source)
local emitted = false
-- Source data points are reverse chronological, so collect them first.
local points = source.dpall()
local current_streak = 0
local best_streak = 0
local newest_point = points[1]
-- Walk oldest -> newest.
for i = #points, 1, -1 do
local dp = points[i]
if dp.value and dp.value > 0 then
current_streak = current_streak + 1
if current_streak > best_streak then
best_streak = current_streak
end
else
current_streak = 0
end
end
return function()
if emitted then return nil end
emitted = true
if newest_point then
return {
timestamp = newest_point.timestamp,
offset = newest_point.offset,
value = best_streak,
label = "Best streak"
}
end
return {
timestamp = core.time().timestamp,
value = 0,
label = "Best streak"
}
end
end |
Beta Was this translation helpful? Give feedback.




Hi. You do not actually need persistent state for this. You can use a function for example that always returns one data point containing the longest streak of all time. For example: