View Full Version : Using Global Variables & ScriptClip
shakana
31st January 2014, 06:38
Sample Code
function a(bool isGoodFrame)
{
goodFrame = isGoodFrame ? current_frame: goodFrame
c = Subtitle(String(goodFrame))
}
global goodFrame = 0
source = avisource("testclip.avi")
ScriptClip(source, "a(isGoodFrame)")
ConditionalReader(source, "dataFile.txt", "isGoodFrame", false)
In short, this sample code reads a file indicating if a frame looks good or not. It sets goodFrame to the frame number of the last good frame.
How do I get goodFrame to hold its value across frames. It always gets set back to 0.
Gavino
31st January 2014, 11:26
The basic problem is that the goodFrame you are setting in the function is a local variable, so the global variable is never updated and remains zero. The assignment should be:
global goodFrame = isGoodFrame ? current_frame: goodFrame
However, there are other errors in the posted script which would stop it getting that far, so I assume it is not what you used originally.
In the posted script, ScriptClip() is ignored, since ConditionalFilter is called on the original source.
Also, the function a has no clip input and does not return a result.
In addition - unless you are using GRunT - current_frame must be passed explicitly to the function, since it is a local variable in the standard Avisynth run-time.
Here is a fully working version of your script:
function a(clip c, bool isGoodFrame, int current_frame)
{
global goodFrame = isGoodFrame ? current_frame: goodFrame
return Subtitle(c, String(goodFrame))
}
global goodFrame = 0
source = avisource("testclip.avi")
source = ScriptClip(source, "a(isGoodFrame, current_frame)")
ConditionalReader(source, "dataFile.txt", "isGoodFrame", false)
# alternatively, replace last two lines by:
# ScriptClip(source, "a(isGoodFrame, current_frame)")
# ConditionalReader("dataFile.txt", "isGoodFrame", false)
shakana
31st January 2014, 14:27
The basic problem is that the goodFrame you are setting in the function is a local variable, so the global variable is never updated and remains zero. The assignment should be:
global goodFrame = isGoodFrame ? current_frame: goodFrame
However, there are other errors in the posted script which would stop it getting that far, so I assume it is not what you used originally.
In the posted script, ScriptClip() is ignored, since ConditionalFilter is called on the original source.
Also, the function a has no clip input and does not return a result.
In addition - unless you are using GRunT - current_frame must be passed explicitly to the function, since it is a local variable in the standard Avisynth run-time.
Here is a fully working version of your script:
function a(clip c, bool isGoodFrame, int current_frame)
{
global goodFrame = isGoodFrame ? current_frame: goodFrame
return Subtitle(c, String(goodFrame))
}
global goodFrame = 0
source = avisource("testclip.avi")
source = ScriptClip(source, "a(isGoodFrame, current_frame)")
ConditionalReader(source, "dataFile.txt", "isGoodFrame", false)
# alternatively, replace last two lines by:
# ScriptClip(source, "a(isGoodFrame, current_frame)")
# ConditionalReader("dataFile.txt", "isGoodFrame", false)
Perfect! I didn't realize that I must declare goodFrame as global in multiple place.
thanks.
Gavino
31st January 2014, 19:12
It's not so much 'declaring' it global in multiple places.
It's that every assignment to a global variable (even if all in the same scope) must include the keyword 'global'. Otherwise, the variable to be written will be taken as local, as global and local variables of the same name can co-exist.
vBulletin® v3.8.11, Copyright ©2000-2025, vBulletin Solutions Inc.