Log in

View Full Version : How to make check is variable defined?


Dreamject
18th April 2019, 17:42
I wanna import some core file, but I want to made core file selfish.

Like
if I import core file, coremode=0
if I load core file, coremode=1, so use variable in corefile.


How can I make this?

Wilbert
18th April 2019, 17:46
http://avisynth.nl/index.php/Internal_functions/Defined#Defined

Dreamject
18th April 2019, 17:46
Thanks man :D

Dreamject
18th April 2019, 18:07
:mad:
If I write

zet=9
what=Defined(zet)

I get what=true

If I write just

what=Defined(zet)

Potplayer says me "I do not know what 'zet' means"

Is that potplayer's error?

StainlessS
18th April 2019, 18:53
Same link as Wilbert posted.

VarExist

VarExist(name) AVS+
Tests if the variable exists or not. Note: if variable exists, it returns true regardless of the "defined" state of the variab

Note

Defined

Defined(var)
Tests if var is defined. Can be used inside Script_functions to test if an optional argument has been given an explicit value.
More formally, the function returns false if its argument (normally a function argument or variable) has the void ('undefined') type, otherwise it returns true.


blankClip

zet=9 # try Comment me out

what=VarExist("zet")

Subtitle(String(what))


EDIT:

Is that potplayer's error?
PotPlayer just plays clip that Avisynth created.

Dreamject
18th April 2019, 19:18
It's works, thanks, I didn't know I must use ""

StainlessS
18th April 2019, 19:35
It's works, thanks, I didn't know I must use ""

I forgot to use Double quotes too when first testing that bit of script, it often happens to me so dont kick yourself too hard.

The Wiki dont define what it means by "var" or "name", perhaps it should.

hello_hello
18th April 2019, 20:25
You can do something like this for backwards compatibility, as only Avisynth+ has VarExist().

Try{ IsZet = defined(Zet) }
catch(err_msg){ IsZet = false }

or some other variation

Try{ Zet = Zet }
catch(err_msg){ Zet = undefined() }

StainlessS
18th April 2019, 20:38
Also, just to be a bit pedantic,

catch(err_msg){ Zet = undefined() }
Undefined in v2.60+ only.

RT_Stats(), nearly any version.
RT_Undefined()
Returns same as undefined() in v2.6. Defined(RT_Undefined()) would return false.

StainlessS
18th April 2019, 21:15
Function MyGlobaVarExist(String Name) {
try { Eval(name) ex=true} catch(err) { ex=false }
return ex
}

blankclip

Fred = "blurt"
#Global Fred = "fart"

name="Fred"
try { Eval(name) existFred=true} catch(err) { existFred=false }

GlbExistFred=MyGlobaVarExist(name)

S1="Local(inline) Fred =" + String(existFred)
S2="Global Fred =" + String(GlbExistFred)
S=S1+"\n"+S2

subtitle(s,lsp=0)

https://i.postimg.cc/L5Vv235J/test.jpg (https://postimages.org/)

EDIT: Variation (same result as above)

Function MyGlobaVarExist(String Name) {
try { Eval(name) ex=true} catch(err) { ex=false }
return ex
}

blankclip(width=196,height=64)


Fred = undefined # exists but Undefined (Comment out both assignments for non exist)
#Global Fred = undefined

name="Fred"
try { Eval(name) existFred=true} catch(err) { existFred=false }

GlbExistFred=MyGlobaVarExist(name)

S1="Local(inline) Fred =" + String(existFred)
S2="Global Fred =" + String(GlbExistFred)
S=S1+"\n"+S2

subtitle(s,lsp=0)

Dreamject
18th April 2019, 22:09
%) :devil:

hello_hello
20th April 2019, 03:53
Also, just to be a bit pedantic,

catch(err_msg){ Zet = undefined() }
Undefined in v2.60+ only.

RT_Stats(), nearly any version.

I was looking at another script today and I thought I'd bumped across an interesting way of undefining variables, then I realised it's what RT_Undefined() does. So any function that doesn't return anything returns "undefined"? Now I understand what you were referring to. And there was I thinking RT_Undefined() must be some clever and complicated function. :)

function VeryUndefined(){}

try{ Fred=Fred }catch(err){ Fred=VeryUndefined() }
Name=Fred
What=Defined(Fred)

StainlessS
20th April 2019, 09:10
Function SyntheticUndefined(var "DontSupllyAnyValueAtAllToThisOptionalVariableWithAVeryLongName") { return DontSupllyAnyValueAtAllToThisOptionalVariablewithAVeryLongName }


Above untested [just done clean setup], must call without any named or unnamed arg (only of any use [if at all] in v2.58, as v2.6+ has Undefined).

EDIT: Yes, good one HH, just realized what your function does, and does it better than above rubbish [probably].

Gavino
20th April 2019, 11:58
So any function that doesn't return anything returns "undefined"?
That's right. See User defined script functions (http://avisynth.nl/index.php/User_defined_script_functions):
If a function body has no (explicit or implicit) return, a void value (ie a value of the 'undefined' type) is returned.
I remember adding that bit myself following this discussion (back in 2008!): Result of NOP()
(You will see there that stickboy had already written a function Undefined() way back then ;))

hello_hello
20th April 2019, 15:57
Ah.... so nop() returns zero. You do learn something new every day.

subtitle(string(nop(), "%.20f"))