Octaaf
Octaaf

Reputation: 175

Why is "Cast to BP_Ladder" failing all the time?

I am having trouble with my Unreal Engine 4 project. I'm very new to this and i don't really understand what's going on. I have made ladder functionality so the character can climb up the ladder and stand on the Static Mesh that is on top. But when i want to go down the ladder i want to trigger the "Allow Down" function on Actor "BP_Ladder" but the cast is failing everytime. what is causing the casts to fail?

I've looked around and other people with cast failed problems have mostly had the names wrong but my ladder is called "BP_Ladder" and that is what i'm casting to so that leaves me really confused.

My BP_Dude blueprint (this is being called every tick) my BP_Dude blueprint

My BP_Ladder blueprint (this is what i'm trying to trigger) My BP_Ladder blueprint

The goal for this function is that the collision of the static mesh will turn off and then allow me to move down the ladder like normal.

I'd really appreciate your help with this, its my first couple of days using unreal engine and the Epic Games tutorials i followed didn't show all of the blueprints so everyone was left helpless with half broken blueprints.

Upvotes: 0

Views: 4894

Answers (2)

Big Meanie
Big Meanie

Reputation: 26

On Begin Overlap is only triggered when you START an overlap, so if you have some logic that sets your "can climb" to false you will need to move outside of the collider and back into it again for another Begin Overlap event.

Upvotes: 1

Rab
Rab

Reputation: 510

Understanding Casts

In its simplest form, if your Actor can be cast to BP_Ladder, then it will return the casted object on the output pin.

Just to ensure you know how a cast works, I have to point out that you can't cast a given type to some unrelated arbitrary type; it has to be "castable" to the cast destination. So if you think that the Actor object returned by your overlap is genuinely a BP_Ladder, or a blueprint class that derives from BP_Ladder, then you should be OK. But that has to be the case; otherwise it will fail every time.

https://docs.unrealengine.com/en-us/Engine/Blueprints/UserGuide/CastNodes

Sorry if I'm being patronising, but if a cast isn't working 9/10 it hasn't been used correctly.


Debugging

OK that out of the way, if you think you are genuinely casting to the correct type and it's still failing, then you'll need to debug your blueprint with the objective of finding what type is being given to the cast node which results in the failure.

  • Select the cast object in your blueprint.
  • Press F9 to create a breakpoint; a red circle should appear on the top left of your cast box
  • Run your game in PIE mode (Combo next to play, New Editor Window (PIE))
  • Put the game in a scenario where your cast will fire (I presume touch the ladder)
  • The breakpoint should trigger; your game window will go grey and you'll get a large red arrow pointing down towards your cast box where you placed the breakpoint
  • Hover over the Object pin on the input side of the cast box. It should show a alt-text containing details of the variable.
  • Inside the alt-text box, look for Current value; this should show you the current object type that you are about to cast. You need to ensure that this value is what you expect.

https://docs.unrealengine.com/en-US/Engine/Blueprints/UserGuide/Debugging

Example

I've taken a screenshot of a working game; in this you will see:

  • a Breakpoint (the red circle) is shown on the function node (the box)
  • the Execution node (red arrow) is Create Rolling Stock
  • the Current value of the variable is DepotComponent...Depot

Example Blueprint breakpoint triggered


Final thoughts

There's a couple of gotchas when using blueprints; one of them in this case could be that you can have two classes with the same name (although they might have different internal "script names" - effectively a fully qualified path). When you debugging an object variable, make sure you match the entire path to the location of your asset in your content folder; this will ensure that you are indeed attempting to cast to the object you think you really are.

Another classic gotcha is "hot reloads". Sometimes the editor needs to reload a module after an on-the-fly build but a class conflict occurs internally; this results in the new version of the class getting a different name (prefixed with HOTRELOADED). Watch out for this; it can cause you to be dealing with two distinct types when casting and you don't even realise. A real sanity-sapper. If in doubt, close and re-open the editor; it fixes it every time.

Upvotes: 2

Related Questions