Best way to check hit result for switches

Hi All,

This is a very simple question but I’m looking for the most efficient way of checking the hit result of a line trace to give me the actors name and then to use that to move to a different room. Basically “teleport” spheres that once clicked will move the player to that room.

Currently it all works but I am doing the terrible thing of checking the hit result name against a “contains” substring and using the boolean result on a branch, I’ll end up with a long branch tree however and I’m sure this can’t be right.

Any help or a point in the right direction would be most appreciated.

Here’s how I would do it.

  1. Create a custom Actor blueprint (derive it from Actor), calling it ‘BP_TeleportTargetActor’ or something like that.
  2. When doing the hit trace, attempt to cast the hit actor to your BP_TeleportTargetActor class.
  3. If the cast succeeds, you can get use the GetActorLocation node to get the exact location of the BP_TeleportTargetActor that was hit by the line trace (or, you could just use the hit result’s Location variable to get the exact location where the trace struck the actor)
  4. If the cast fails, do nothing, because the trace didn’t hit a relevant (teleport) actor

And that’s pretty much it. I guess if you need to teleport to a location slightly different than the actual location of your BP_TeleportTargetActor, you could always add a public Vector variable to the BP_TeleportTargetActor blueprint which stores the exact teleport destination for that room, and just access this variable after the cast node in your tracing blueprint.

Hopefully this idea will work for you, or at least helps you along your way!

Thanks for the fast reply.

I’m quite happy with the set up at the moment in that I have several 360 spheres with each room, in each room there are spheres that act as my switches to each room, so when I click it checks the line trace, gets the objects name and proceeds to check if that string contains one of many substrings with that big branch tree I have, if it finds one it moves the character to the center of the desired room.

I see swtiches can work, Swtich on String and so forth but because my strings are so simmilar I might run in to trouble? for instance “Teleport_Basillica_Detail_1” and “Teleport_Basillica_Detail_2”

If you follow something like what I suggest, you won’t need to use strings at all.

Basically, just imagine each room in your level having a custom blueprint actor in it — or more than one, if need be. (These could be the spheres that you’re using right now, just blueprinted to be a specific blueprint class for this purpose.)

You would place these custom blueprint actors in your level like you might any other actor — just drag 'em into your level, and move 'em into place. In your case, you’ll probably put them near the doorway to each room, or wherever you want the player to see and interact with them to initiate a teleport.

If your tracing blueprint script happens to hit one of these custom blueprint actors — which you will easily know by using the Cast node — then you will already have almost all the information you need.

You won’t need to look over a collection of strings to find the right destination — the blueprint actor that was hit by the line trace will contain all the necessary info! (Such as teleport destination location, which can be stored in a public vector variable that is adjustable for every such blueprint actor in your level.)

If I’m still not making sense, just let me know, and I’ll post a screenshot example of what I mean as soon as I can. But, once I get my idea across, I think you’ll find that it greatly simplifies what you’re trying to do!

I understand it in theory but I’d greatly appreciate a screenshot if you could. I am sure you know better than me so It’d be a good thing to see and learn as I feel the potential fix I have isn’t so elegant.

No rush on the example though, free help is very welcome.

I believe the below example is what the_batch was getting at. basically you create a actor that will be the teleporter. you give the teleporter actor a variable which represents the target location and make the variable public so you can set it on a per instance basis. then you create a custom event (teleport) which sets the players location. in this case i used a variable of type transform so i could work with the rotation as well as the location. i also showed how to set the orientation of the camera for a third person situation.

a few things to note: by using a actor you are able to create the script once and reuse the same actor in many places so you dont need to create the same script each time you want a transporter. also since the variable is public you can set each transporter to send the character to a different location for each one. these in turn make things much simpler and eliminates the spaghetti.

@ThompsonN13, that’s pretty much exactly what I was thinking. Thanks for posting that and helping out!

@CGough, the real magic of this approach is the Cast To node, which re-interprets the struck actor as a more specific sub-class — in our case, the custom blueprinted actor that I was calling ‘BP_TeleportTargetActor’ in my first response, and which is called simply ‘teleporter’ in the screenshots above.

If the cast node succeeds in casting the hit actor as the desired type, then we can call up custom events (as shown above), access custom variables, or do any number of other things. And all while keeping the blueprint scripting very minimal and clean!

Hopefully it makes more sense now. Check out this linked article on the official docs about Casting in Blueprints and this Blueprint Casting Example if you’re still new to the idea of casting.

Happy developing!

@the_batch @ThompsonN13 Thanks to both of you, it was late at night when I posted so sorry for the late reply but I’ll be sure to look at this tonight as it’s obviously a much stronger way to do this and I’ll actually learn more so that’s great.

I’ll be sure to mark as answered and upvote when I figure this out later today.

Many thanks again.

Update

Thanks to the both of you that worked perfectly. The only change I had to make was sending the Teleport Transform to my Pawn Blueprint as I’m using the Occulus Go and my BP_Pawn is in the scene, no doubt that method is wrong as well but for the meanwhile it works just dandy passing the Transform around and happy days I’m shooting around my rooms without a massive broken branch tree like before.