Download

Cast node vs get component

Im having a hard time to understand what the cast node is needed for. I thought it is used to get access to propertys of components of actors like Gameobject.Transform in that other engine for example. But I found out that I could use get component by class as well, to access something.
I the example below I attached a camera component to a blueprint actor to change something there and it turns out that both ways work(the sample makes not much sense)
But what is the difference, what is the benefit of cast, is there a performance difference?

?

If you connect the execution wire and compile it, the bottom example will give you a warning (a blue note on the cast node) saying there’s no need to cast. GetAllActorsOfClass node returns the correct data type already, no need to cast here.

Casting is not a form of communication. To simplify it greatly; in BPs the Cast To means as much as: take this **Generic Object **and attempt to treat it as if it was of a Very Specific Class. If it’s not, the cast fails - this object is not what you think it is. If the object *is *indeed of the specified class (NewBlueprint1), then you know you’re dealing with the intended class and can manipulate its unique data.

I feel like I butchered this explanation somehow.

You sure can. If there’s only 1 - that node returns the 1st one only. But since you’re already dealing with the correct class in the top example, there’s no need for this. You can directly pull the component reference from that object (just like in the bottom example). No need to fiddle with the class filter here and no need to cast.

edit: the biggest issue in here is that you’re using the GetAll node in the first place here. It’s ok (yet hacky) if you’re absolutely sure you’ll always have just 1 NewBlueprint1 object.

*GetAll *(there’s more than 1) nodes are great if you really want to get ALL of something - as in, clean up a level of unwanted objects, or maybe apply a global debuff to all monsters of the specific class in the entire dungeon. Although I’ve yet to run into a situation where’s I’d need that node for anything game related - it’s a great debug tool, though.

1st…
Yes… and what happens if an Actor won´t have the needed Component? Or if the Component has a modified Class without the needed Variable? - Error…
While you get a Fallback Executer in Casting (Cast Failed) you are defenseless when using GetComponentByClass on unknown Classes. So, you need to be reaaaaaally sure, you know nothing on this component and actor will change.

2nd…
When you get multiple Components of the Class, you will only get the first one in List and won´t have access to the followings, since GetComponentByClass only returns the first encountered Component of the actor…
Let´s say, your Actor has two Cameras, you will only be able to make changes on the first one in the Component List…
To prevent that, use “GetComponentsByClass”, which returns an array of all encounters of that class… which again leads into a casting for each component, to make sure it is valid and the same classtype…

Ps.: Totally passed over the GetAllActorsOfClass… so… **Everynone **is right here…

Thank you all so much for the explanation, after some experience with that other engine translating workflows to UE is challenging.