最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

lua - Trying to make an OBS based timer that calculates distance x time - Stack Overflow

programmeradmin5浏览0评论

I'm coding with Lua for this one, I'm not very experienced with this specific language. The concept is the user is able to set a certain time and distance, and the counter will increase based on that time (ex. "Every two minutes, the distance increases by 1") with both the time and distance capable of being altered to fit the needs of the stream. The script is intended for a charity stream in which the streamer will walk for a certain time and track the distance for the viewers, so it needs to be capable of updating itself without user intervention once started, but still able to be paused and stopped if he needs to take a break. I was messing with it for hours, but wasn't able to fully figure out the issue. I mainly know some basic python so from what I do know of Lua, I can't navigate where the bugs are coming from. I did get the base code from an opensource github script, but it was for multiple different timers. I deleted out all the ones I didn't need and tried to update the timer count script for my specific needs.

I ran multiple tests on my own PC, but when I sent the script to the streamer, there was consistent bugs from causing his client to crash, not updating the distance when the timer reaches the requested time, or the distance attempting to count the seconds to the minute as well. After the bugs, I moved the script to my secondary PC, where I was suddenly getting the same bugs as well. Also an odd bug, where if you attempt to adjust the distance (ex "distance 2 every one minute") it rapidly accelerates the count. I tried different things like how it was calling the timer, how it was calculating the distance, and putting preventions against crashing. I don't see where the issue is so it's hard for me to know exactly what to target.

In the end, the timer needs to be started by the user. This works fine. The timer needs to also be capable of being paused, then started again. There's a bug somewhere here when being paused and started again, it keeps counting anyway when paused, but picking up pace when clicking start again. The timer needs to be able to adjust its count based on user settings. Counting seconds, then once reaching 60 seconds, updating the distance and continuing to count into minutes, then hours. It does not need to reach days. But after every 60 seconds, the timer needs to check the distance and update accordingly based on the user desire. If distance or time is not set by the user, it will default to 1 KM every 1 minute. There is a bug here somewhere, where the distance attempts to count along with the seconds, giving 1 distance every 1 second. If the user alters this, the distance will adjust and count by seconds again (ex. "For every 1 minute, add 5 distance" will simply cause the distance to count up by 5 every second) When the user is done and stops the timer, it should fully reset, as opposed to pausing it.

obs = obslua
distance = 0          
time_elapsed = 0     
interval = 9 * 60 
distance_increment = 1    
timer_started = false      
last_time = 0  
text_source_name = ""
timer_interval = 1000 

function script_description()
    return "Increments a distance counter by a specified amount every set interval. Displays Time and Distance."
end

function script_properties()
    local props = obs.obs_properties_create()
    obs.obs_properties_add_int(props, "interval", "Interval (minutes)", 1, 1440, 1)
    obs.obs_properties_add_int(props, "distance_increment", "Distance Increment", 1, 100, 1)
    obs.obs_properties_add_text(props, "text_source", "Text Source Name", obs.OBS_TEXT_DEFAULT)
    obs.obs_properties_add_button(props, "start_button", "Start", start_button)
    obs.obs_properties_add_button(props, "stop_button", "Stop", stop_button)
    obs.obs_properties_add_button(props, "reset_button", "Reset", reset_button)
    return props
end

function script_update(settings)
    interval = obs.obs_data_get_int(settings, "interval") * 60
    distance_increment = obs.obs_data_get_int(settings, "distance_increment")
    text_source_name = obs.obs_data_get_string(settings, "text_source")
    obs.script_log(obs.LOG_INFO, "Interval set to: " .. interval .. " seconds")
    obs.script_log(obs.LOG_INFO, "Distance increment set to: " .. distance_increment)
    obs.script_log(obs.LOG_INFO, "Text source name: " .. text_source_name)
end

function start_button()
    if not timer_started then
        last_time = os.time()
        obs.timer_add(timer_callback, timer_interval)  
        timer_started = true
        obs.script_log(obs.LOG_INFO, "Timer started.")
    else
        obs.script_log(obs.LOG_INFO, "Timer already running.")
    end
end

function stop_button()
    if timer_started then
        obs.timer_remove(timer_callback) 
        timer_started = false
        obs.script_log(obs.LOG_INFO, "Timer stopped.")
    else
        obs.script_log(obs.LOG_WARNING, "Timer not running.")
    end
end

function reset_button()
    distance, time_elapsed = 0, 0
    update_text_source()
    obs.script_log(obs.LOG_INFO, "Distance and Time reset.")
end

function timer_callback()
    if not timer_started then return end

    local current_time = os.time()
    local time_diff = current_time - last_time
    last_time = current_time
    time_elapsed = time_elapsed + time_diff

    if time_elapsed >= interval then
        distance = distance + distance_increment
        time_elapsed = time_elapsed - interval 
        obs.script_log(obs.LOG_INFO, "Distance updated to: " .. distance .. " km after interval reached.")
    end
    
    update_text_source()
end

function update_text_source()
    local source = obs.obs_get_source_by_name(text_source_name)
    if source then
        local settings = obs.obs_data_create()
        local minutes, seconds = math.floor(time_elapsed / 60), time_elapsed % 60
        local time_string = string.format("Time: %d:%02d\nDistance: %d km", minutes, seconds, distance)
        obs.obs_data_set_string(settings, "text", time_string)
        obs.obs_source_update(source, settings)
        obs.obs_data_release(settings)
        obs.obs_source_release(source)
    else
        obs.script_log(obs.LOG_WARNING, "Text source not found.")
    end
end
发布评论

评论列表(0)

  1. 暂无评论