AviSynth / AviSynthPlus

AviSynth with improvements
http://avs-plus.net
981 stars 76 forks source link

Issues with var-exist check #385

Closed SuggonM closed 8 months ago

SuggonM commented 10 months ago

May I get more descriptive examples of Default, Defined and VarExists functions?

At this point, I've explored all the possible ways to make a variable check using them, but none seemed to work. Either the variable exists and remains as-is untouched, or the variable doesn't exist and throws the error I don't know what '<variable>' means., which kindof defeats the point of var-exist check.

Following is the code I'm using. On the "client" side, all the desired variables are passed as global - otherwise omitted. On the "author" side, it checks if the variables exist - otherwise they default to 50.

global pad_l = 500
global pad_b = 200
pad()

function pad() {
    i = ImageSource("./source/img_%02d", start=0, end=100, fps=20, pixel_type="RGB32")
  # default(pad_t, 50)
  # pad_t = Defined(pad_t) ? NOP : 50
  # pad_t = VarExists(pad_t) ? NOP : 50
  # ...and so on for other three variables...
    i = i.AddBorders(pad_l, pad_t, pad_r pad_b, color=$00000000)
    return i
}

I'm also using the latest version of AviSynth+ and AvsPmod. But looking at the documentations and few other decade-old forums, they seemed to be working fine on the users' side (for example,-EDIT)).

flossy83 commented 10 months ago

Yes I was a little bit confused by it too.

If (!VarExist("MyVar")) that means MyVar hasn't been created at all. The lowest form of nothingness.

If (VarExist("MyVar")) there are 2 possibilities:

  1. MyVar has been created, but hasn't been set to anything. Kind of like a null pointer I guess. In this case Defined(MyVar)==false. This is what convenience function Default() checks for. If you are inside your own custom function and you want to check if the user passed an optional argument, this is the check you want. By the way Defined(MyVar)==false is equivalent to MyVar==Undefined().

  2. MyVar has been created, and has been set to something like a string, int etc. The highest form of somethingness.

Operators like == != > < etc. can only compare two of the same type which are Defined(). So you can't compare an Undefined() with a string or an int, or another Undefined(). Plus, if your custom function has a generic "val" type argument then you need to use IsInt(), IsString() etc. to check you're comparing two of the same type.

flossy83 commented 10 months ago

It seems you'd want this: pad_t = VarExist("pad_t") && IsInt(pad_t) ? pad_t : 50

edit: I should mention the IsInt(pad_t) would normally throw an error if pad_t does not exist, but short circuit evaluation is probably avoiding that.