what is wrong in this lua instruction? :minutes=5; seconds=30; offset=1572;…
- User
what is wrong in this lua instruction?
:minutes=5; seconds=30; offset=1572; fps=comp:GetPrefs("Comp.FrameFormat.Rate") or 24; return string.format("%d", time) and (time-offset) + string.format("%02d:%02d", math.floor(((time-offset)/fps+(minutes60+seconds))/60), math.floor(((time-offset)/fps+(minutes60+seconds))%60));
- Assistant
There are a few issues with the provided Lua instruction:
-
Misuse of the
string.formatfunction: Thestring.formatfunction is supposed to format a string, and the result should typically be used directly or assigned to a variable. Your use of theandoperator after thestring.formatfunction is nonstandard and may not work as intended, becausestring.format("%d", time)returns a string, and the result of theandoperation relies on both operands being true/truthy for the operation to succeed.
-
- User
only the string concatenation part is an issue
- Assistant
If the main issue is with string concatenation in your Lua script, let's focus on that part specifically. Lua uses the
..operator to concatenate strings. Based on your original code, you seem to attempt two separatestring.formatoperations concatenated with logical operators, which isn't the right approach for concatenation.Here's a corrected version of the concatenation part:
local minutes = 5 local seconds = 30 local offset = 1572 local fps = comp:GetPrefs("Comp.FrameFormat.Rate") or 24 local time = 3000 -- Example time value; replace with your actual logic -- Calculate the adjusted time local adjustedTime = (time - offset) / fps + (minutes * 60 + seconds) -- Format and concatenate the time string local formattedOutput = string.format("%d", time - offset) .. " " .. string.format("%02d:%02d", math.floor(adjustedTime / 60), math.floor(adjustedTime % 60)) return formattedOutputIn this corrected snippet:
- I've defined a placeholder
timevariable for the sake of demonstration. Replace it with your actual logic for computing .
- I've defined a placeholder
- User
this now works:
:minutes=5; seconds=30; offset=1572; fps=comp:GetPrefs("Comp.FrameFormat.Rate") or 24; formattedOutput = string.format("%d", time) .." "..string.format("%02d:%02d", math.floor(((time-offset)/fps+(minutes60+seconds))/60), math.floor(((time-offset)/fps+(minutes60+seconds))%60)); return formattedOutput;
but I want that string.format("%d", time) to be empty string if (time-ofset) = 0
- Assistant
To achieve that behavior, you can use a conditional expression to check if
(time - offset)is zero, and then decide whether to use an empty string or the formatted string. Here is how you can modify your code to implement this logic:minutes = 5 seconds = 30 offset = 1572 fps = comp:GetPrefs("Comp.FrameFormat.Rate") or 24 -- Replace time with your actual time variable or calculation local time = 3000 -- Example time; replace with your actual logic -- Conditionally format the first part local timeDifference = time - offset local formattedTimeDiff = timeDifference == 0 and "" or string.format("%d", timeDifference) -- Compute the rest of the output as before local totalSeconds = (timeDifference / fps) + (minutes * 60 + seconds) local formattedTime = string.format("%02d:%02d", math.floor(totalSeconds / 60), math.floor(totalSeconds % 60)) -- Concatenate the results with a space local formattedOutput = formattedTimeDiff .. " " .. formattedTime return formattedOutputKey changes:
- Added a
timeDifferencevariable to store the result of .
- Added a