TestExpr

Revision as of 05:41, 15 August 2014 by imported>Odessa (Created. Source OBSE docs and personal testing)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
< [[::Category:Functions|Category:Functions]]

A function added by the New Vegas Script Extender.

Description

TestExpr is passed any expression and returns True (1) if it succeeds, or False (0) if it fails- meaning it generates an NVSE Error, and the error message is suppressed from appearing in the console.

This is an alternative to validating one's data, which might require multiple lines. Essentially it is saying; "I know this line of code might fail, so tell me if it does and I will handle it. In this case the error is normal, so don't alert the user".

Comparable to the 'try', 'catch/except' found in some programming languages. Added by NVSE 4.1.

Syntax

(Result:bool) TestExpr AnyLine:expression

Example

Simple example, instead of bothering to check a key exists with Ar_HasKey:

if TestExpr SomeArray[SomeKey]
    ; SomeArray has SomeKey
else
    ; SomeArray does not have SomeKey
endif

Suppose you want to keep a tally of the number of times the player encounters every NPC of a particular name:

array_var aTally
ref rActor
string_var actor_name

let aTally := Ar_Construct "stringmap"
; ...

let actor_name := $rActor

if TestExpr aTally[actor_name] += 1
   ; do nothing. The increment worked because the actor_name is in the array already
else
    aTally[actor_name] := 1 ; * first time we met actor_name, so create the entry
endif

Suppose you are worried a ref variable might be empty for some reason, so your functions would fail:

ref rActor
int iStrengthStat

if TestExpr iStrengthStat := rActor.GetAV "Strength"
    ; do nothing. The function worked, so rActor must be valid
else
    ; rActor isn't valid, so don't proceed further yet
    return
endif

Notes

  • You can assign variables using ':=', '+=', etc, using any NVSE aware command (such as TestExpr, Eval, While...), not just with Let.

See Also