I am trying to write an extension to filter script actions, which the following actions seem to require more code than expected (or maybe I am just not familiar with lua). Maybe more lua api functions can be added to make life easier.
- the RemoveAction function shifts the script.actions indexes. Here's the code I am using to remove multiple selected actions, please let me know if there is an easier way.
local script = ofs.Script(ofs.ActiveIdx())
if ofs.HasSelection(script) then
local to_delete = {}
for idx, action in ipairs(script.actions) do
if action.selected then
table.insert(to_delete, idx-#to_delete)
end
end
for i, idx in ipairs(to_delete) do
ofs.RemoveAction(script, script.actions[idx])
end
ofs.Commit(script)
end
- I am trying to add some functions that I feel is missing for funscript editing. For example to toggle selection of action at/closest to cursor. Here's the code I came up with.
function toggle_select(val)
local script = ofs.Script(ofs.ActiveIdx())
if #script.actions > 0 then
local t = player.CurrentTime()*1000
local closest = player.Duration()*1000
local i = nil
for idx, action in ipairs(script.actions) do
local diff = math.abs(t-action.at)
if diff < closest then
closest = diff
i = idx
else
break
end
end
script.actions[i].selected = val
ofs.Commit(script)
end
end
I tried using ClosestActionAfter and ClosestActionBefore, but it still requires quite a few if to find the closest action, and if the cursor is on an action, the 2 functions give you the index of after and before the current action.
-
OFS could directly return a table of selected indexes instead of having to use for...if script.actions[idx].selected (this is very minor)
-
There is a typo in the lua api reference, where ofs.SameLine() has a lowercase L.
-
One more thing, if an extension gui is opened for a long time (maybe 30mins?), it will eventually encounter stack overflow.