Log in

View Full Version : technical question about avisynth variables


bREAkDoWN
29th April 2006, 14:40
Let's take the following code:


directshowsource("blabla.avi",fps=25)
converttoyv12()
global temp=0

ScriptClip("""Subtitle("temp: "+String(temp)) """)

ScriptClip("f1()")
ScriptClip("f2()")

function f1(clip c) {

c=FrameEvaluate(c,"temp=temp+1")
return(c)
}

function f2(clip c) {

c=FrameEvaluate(c,"temp=temp+2")
return(c)
}


This way everything works as supposed, and temp is incremented by 3 on every frame.

If i add "global" into frameevaluate in both functions: c=FrameEvaluate(c,"global temp=temp+1"), everything still works as expected.

I suppose that the right way should be the one without "global", because temp has already been initialized as global in the main script.

However, i started to have some doubts when i discovered by chance that if i put global into the frameevaluate of only one of the two functions, something strange will happen. In fact, on script execution, the first frame will show 3, which is the correct sum, but stepping ahead frame by frame, only the frameevaluate without global will contribute to the sum.


In other words:


directshowsource("blabla.avi",fps=25)
converttoyv12()
global temp=0

ScriptClip("""Subtitle("temp: "+String(temp)) """)

ScriptClip("f1()")
ScriptClip("f2()")

function f1(clip c) {

c=FrameEvaluate(c,"temp=temp+1")
return(c)
}

function f2(clip c) {

c=FrameEvaluate(c,"global temp=temp+2")
return(c)
}


will output:
3 4 5 6 7 instead of 3 6 9 12 15

Can someone explain why does this happen?

foxyshadis
29th April 2006, 17:45
"global temp=temp+2"

You're redefining the variable in the context of the FrameEvaluate, so it exists only within the scope of that frameevaluate and the, er, more global global is ignored. Removing "global" you end up with 3 added each frame. =D

bREAkDoWN
2nd May 2006, 20:51
"global temp=temp+2"

You're redefining the variable in the context of the FrameEvaluate, so it exists only within the scope of that frameevaluate and the, er, more global global is ignored. Removing "global" you end up with 3 added each frame. =D

First of all, thank you for your attention..
Then.. :) i'm afraid i didn't understand very well. It's ok that with the global statement within frameevaluate i redefine the variable within the scope of frameevaluate, but for example, it's still unclear to me why if i put global within both frameevaluates then the changes made to the variables are seen in the main part of the script, outside of the functions.
So, global within both frameevaluate works well, global omitted from both works well too, while global within only one doesn't. This still sounds a bit strange to me.