; Add this to your Papyrus MainQuest script

; Start a polling timer when screenshot begins
Function Printscreen(String path, String imageType, Float compression, String Mode, float GIF_MultiFrame_Duration)
    Debug.Trace("Starting latent screenshot: " + path + ", " + imageType)
    if(Menu)
        Debug.Trace("Menu control is active, hiding HUD")
        HideHud()
    else
        Debug.Trace("Menu control is inactive, HUD will remain visible")
    endif
    
    ; Start the latent function
    String startResult = Printscreen_Formula_script.TakePhoto(path, imageType, compression, Mode, GIF_MultiFrame_Duration)
    
    if (startResult == "Started")
        ; Start polling for completion
        StartTimer(0.5, 1)  ; Poll every 0.5 seconds using timer ID 1
    else
        ; Failed to start
        Debug.Notification("Failed to start screenshot: " + startResult)
        IsLatentScreenshotActive = false
        if (Menu)
            ShowHud()
        endif
    endif
EndFunction

; Timer event for polling
Event OnTimer(int aiTimerID)
    if (aiTimerID == 1)  ; Screenshot polling timer
        String result = Printscreen_Formula_script.Get_Result()
        
        if (StringUtil.Find(result, "CALLBACK_") == 0)
            ; We got a callback result - process it
            if (result == "CALLBACK_SUCCESS")
                OnScreenshotCompleted("PrintScreenComplete", "Success", 0.0, None)
            elseif (result == "CALLBACK_CANCELLED") 
                OnScreenshotCompleted("PrintScreenComplete", "Cancelled", 0.0, None)
            elseif (StringUtil.Find(result, "CALLBACK_ERROR:") == 0)
                String errorMsg = StringUtil.Substring(result, 15)  ; Remove "CALLBACK_ERROR:" prefix
                OnScreenshotCompleted("PrintScreenComplete", errorMsg, 0.0, None)
            endif
        elseif (result == "Running")
            ; Still running, continue polling
            StartTimer(0.5, 1)
        else
            ; Unexpected result, treat as error
            OnScreenshotCompleted("PrintScreenComplete", result, 0.0, None)
        endif
    endif
EndEvent

; Your existing completion handler
Function OnScreenshotCompleted(String eventName, String strArg, Float numArg, Form akSender)
    Result = strArg
    Debug.Trace("Screenshot completed with result: " + Result)
    
    if(Result == "Success")
       Shots = shots + 1
        Debug.Notification("Screenshot #" +Shots + " saved successfully!")
    else
        Debug.Notification("Screenshot failed: " + Result)
        OnScreenshotFailed() 
    endif
    
    if(IsLatentScreenshotActive && Menu)
        showHUD()
    endif
    
    IsLatentScreenshotActive = false
EndFunction