Log in

View Full Version : Can I stop Import() from inheriting the imported script's global variables?


zambelli
7th April 2023, 20:32
I would like to use the Import() function to call a script which returns video as a result. However, I don't want any global variables defined in the child script to be visible to the parent script (or vice versa, though that one is less of a problem).

For example, let's say that there exist 2 scripts - video1.avs and video2.avs - and both define a global variable named x within their respective scopes. Both scripts return video as result.
Let's say my main script imports both scripts and assigns their returned videos to variables:

global x = "foo"
v1 = Import("video1.avs")
v2 = Import("video2.avs")
Interleave(v1, v2).Subtitle(x)

I want the value of x in the Subtitle() function to be "foo" regardless of what value the child scripts assign to x in their own scope.

I tried wrapping Import() in a function thinking that it might restrict the imported script's variables to the scope of the function, but that didn't change the behavior.


function LoadAvsSource(string "srcScriptPath")
{
vid = Import(srcScriptPath)
return vid
}
global x = "foo"
v1 = LoadAvsSource("video1.avs")
v2 = LoadAvsSource("video2.avs")
Interleave(v1, v2).Subtitle(x)

Same result.

Is there a way keep the imported script's scope isolated from the parent script?

Selur
7th April 2023, 20:36
Wouldn't it kind of break the point of having global variables if those are not global?

zambelli
9th April 2023, 21:38
Wouldn't it kind of break the point of having global variables if those are not global?
Sure, but I'm not asking to change the default behavior, I'm asking if there's a way to override it in special cases where sharing the imported script's scope might not be desirable.

johnmeyer
10th April 2023, 01:47
Gavino was the expert on AVISynth global variables, but he hasn't posted since last October.

Gavino
10th April 2023, 18:44
I can't try it out at the moment, but I seem to recall that AviSource() is able to open a script file. This is not usually a good idea for efficiency as (unlike Import()) it involves creating a whole new instance of AviSynth itself.

However, if it works, it would provide a workaround for this use case as the separate AviSynth instance would have its own global variables.
So try replacing the second Import(...) by AviSource(...).