#unreal

When should you use Cast or CastChecked?

A simple guideline is use CastChecked when you either…

  • Know that the actor is of that particular type (maybe you used a function which uses a TSubclassOf to filter actors)
  • Assume the actor is of the particular type - in other words, the subsequent code fails if it isn’t, and you don’t plan on checking using an if whether the cast succeeded or not.

Isn’t assuming the actor is of a certain type a bad idea?

Often we can be reasonably certain that the objects in our code are of some specific types. In these cases it can be safe to make assumptions like this. We might not be 100% sure, but we must be able to trust the other parts of the code work correctly, and in these types of situations we should be able to assume the actors are of the types they should be.