Casting failed, can't figure out why.

I’m fairly new to unreal engine and have come to really it. A lot of systems seem very natural and intuitive. There’s just one thing I can’t really wrap my head around. Its not the nature of casting or what it does and why it does it. instead, it’s simply these two cast to functions that I’m having a lot of trouble with.

The main purpose of this blueprint here is to set a sprite, that is attached to an actor, to hidden if no trace is blocked, and to not be hidden if a trace is blocked. I know the trace is working since the “PrintString” function is printing whatever object it hits on screen.

I have two returns from by branch that lead to the casts. True for if the ray trace is blocked, and false if it is not. The true adds the “Hit actor” to an array and continues onto the “Cast to HookPointUI”, which is one of the two casts I’m having trouble with. If it is false it continues onto a “ForEachLoop” that completes one “Cast To” function and then clears the array.
The problem is that both “Cast To” functions aren’t working. I’ve set the trace channel of the “LineTraceByChannel” function to be “HookPointUI”, which is something directly related to the “Cast To HookPointUI”. Both the target and the object reference are actors or have parent actors.

Can someone help? I’ve no idea what the problem is. It’s probably something plain as day that I haven’t, just yet, wrapped my head around.

Hi, yes I see you’re having some logic problems there.

About casting, a lot of people are confused by it:

When you get the hit on an actor, at that point, it’s just an ‘actor’, which is basically the most generic classification you can have. At that point you want to know, ‘is this actor a HookPointUI?’, because the actor could be anything.

The way to find out is by casting. What casting does is give you a chance to find out what kind of actor it actually is. So if the cast fails, it’s just not a HookPointUI. If the cast succeeds, it is a HookPointUI, and you can then pull a pin off from the cast and call events / set variables etc inside that instance of HookPointUI.

What you’re going with those arrays, actually I have no idea. You might need to describe what you want to have happen for me to fill you in on that bit.

Also ( I can tell from looking at your graph ), those arrays must be of type Actor. Which is a bit mad if you actually want to populate them with HookPointUIs. So, directly after getting a hit, cast to HookPointUI, then you can use an array of HookPointUI and avoid having to cast every time you go near the array :slight_smile:

( BTW, please the quotes or like button if you answer, that’s how other folks know you’ve updated the thread. )

Thanks for the quick response!

This node tree was actually something I ripped from one of my older projects as it suddenly stopped working and I had no idea why. So if it looks a little out of place, I can understand that. I simply wanted to replicate what i had working in the last project, in this project. And then clean up the logic afterward and make sure it still worked every step of the way.

To reiterate, the purpose of this function is to set a sprite, that is attached to HookPointUI, to be hidden if there is no trace blocked and to not be hidden if there is. Essentially projecting a sprite from a socket in the same rotation as the camera onto a surface and setting a HookPointUI actor there. In comes the arrays:

In my last project I had it so that when the trace did get blocked, the HookPointUI actor would take the vector data and transform its location to the impact location, alongside being unhidden. The benefit of the arrays is that it successfully stored the vector information and left the HookPointUI actor and Sprite at the last location where the trace was blocked. I thought this would be useful for future endeavors where you could grapple to something your still not looking at, as long as the location of that grapple was stored in the array and the character was still in range of the HookPoint

In addition, your response is very quick and easy the understand, Thank you. And for it, I think I now understand the purpose of the object input in the Cast To.

If you know any better ways of projecting the sprite, that would be very useful.

As for the casting: How would I be able to give the HookPointUI actor information on where or not to be Hidden or Unhidden?

As it stands, the cast is asking: “is this actor a HookPointUI” when, instead, I want it to take the object data that was traced and be simply asking “Is this an actor.” There’s probably some logic wizardry that has to happen in between. But I had it working in my last projecting and I have no idea what changed or why it changed. Tbh that probably contributes to my confusion a lot.

If you want to put sprites at locations and make them visible or not, probably the easiest way to do it is to spawn a BP at that location. The BP can display the sprite ( or not ), and stay hidden at that location if need be.

You can then use a broadcast ( event dispatcher ) to talk to all of the BP sprites at the same time. Things get much easier if you put the logic into the things you’re spawning, rather than trying to control it all from a central point. Although sometimes you need central control. I don’t know if that helps.

If you want to know if the hit has found an actor, just cast to actor, that’s it. Casting is how you check the type of something you’ve gotten hold of. I’m assuming that everything coming back from a trace would be an actor and you don’t need to check that, but I might be wrong…

been a while but thanks :). Your explanations for the way these things worked helped, and are still helping me, immensely.

Also, I’ve changed it so that the trace records an impact point and projects a widget to the screen instead of in world space. Hindsight is bliss.