Weird behavior in scheduling callback from script_load

danieleambrosino

New Member
I've written a stupid-simple Lua script to automate scene switching pseudo-randomly for a live streaming, and I ran into some bizarre behavior.
This function schedules the callback for the next scene switch:
Code:
local function schedule_next_switch()
  obslua.timer_remove(switch_scene)
  local next_switch_after_ms = get_scene_duration_ms(current_scene_index)
  obslua.timer_add(switch_scene, next_switch_after_ms)
end
and it works perfectly in the scene switching function:
Code:
local function switch_scene()
  current_scene_index = pick_next_scene_index()
  obslua.obs_frontend_set_current_scene(scenes[current_scene_index])
  schedule_next_switch()
end
but if I call schedule_next_switch in script_load like this:
Code:
function script_load(options)
  schedule_next_switch()
end
the callback doesn't get scheduled! And the even stranger thing is that if I instead schedule the switch directly calling obslua.timer_add like this:
Code:
function script_load(options)
  next_switch_after_ms = get_scene_duration_ms(current_scene_index)
  obslua.timer_add(switch_scene, next_switch_after_ms)
end
it works perfectly!
I thought it could be related to calling user-defined functions in script_load, but this is not the case since get_scene_duration_ms (which is also user-defined) works without any problem.
Am I perhaps missing something? I'm running the currently latest version of OBS (29.0.0), I have encountered this behavior both on Windows and on MacOS.
You can find the full script at this link.
Thanks in advance!
 
Top