![Unreal Engine 4 Game Development Quick Start Guide](https://wfqqreader-1252317822.image.myqcloud.com/cover/561/36698561/b_36698561.jpg)
上QQ阅读APP看书,第一时间看更新
Working With Object/Actor References
As with traditional programming, we need to avoid errors in our code that can occur from trying to access null variables. In Blueprints, there are a few ways in which we can make sure a variable is valid before using it:
- Depending on the context, we may just need true or false (uncommon, but may be required in an if statement before a function call, for example).
- The second method uses an IsValid function call with two outputs. Both of these can be found by typing valid in the search box.
- The third method, which I prefer, is to use a Validated Get, which can be created by right-clicking on a normal Get node and clicking on Convert to Validated Get. This is essentially the same as the second method, but keeps the code cleaner:
![](https://epubservercos.yuewen.com/65884A/19470378308806306/epubprivate/OEBPS/Images/c8f47ca4-103d-4dec-a71d-19583621205d.png?sign=1738950472-2um797MDuQaZ4q1OpG8Sso94FRobiJsl-0-89caeb0d731df5522d202fe0a88e58e3)
Using a validated GET is the same as this snippet of code:
if ( MyActorReference != null )
{
// This is the Is Valid output.
}
else
{
// This is the Is Not Valid output.
}
The Is Valid output ensures that the MyActorReference variable isn't null, and you won't get any errors trying to access its variables and functions:
![](https://epubservercos.yuewen.com/65884A/19470378308806306/epubprivate/OEBPS/Images/2cbf6340-6cce-4b8c-a990-1d5875ea3566.png?sign=1738950472-VcOeqK8dsNtE4uGMuBqqz6Knoyd7XMSU-0-be4e7fa6bd4774b775e04c098edf6582)