How to cast a child actor component to its original class from outside the parent actor?

Hello!
Please I need a hand to understand what I am doing wrong and how to do this because I didn’t get it even reading other posts and watching some related videos.

I have an actor BP that is an elevator and it has another actor called Puerta (Door) as a component. I have been trying to access to that component from outside the BP using an interface but unsuccessfully. I can’t get to cast it.
I keep receiving a message saying: ‘BP Puerta Ascensor Ch’ does not inherit from ‘Scene Component’ ( Cast To BP_PuertaAscensorCh would always fail).

I’ve also tried the ‘Get Child Actor’ as I read in other posts but at that instance I don’t have that node available.

So how is the right way to make it?

This is the parent actor:

My blueprint from outside. There I am trying to read a variable of the child actor:

The attempt to get child actor when I found the desired object

Hi,
Cast it to a “ChildActorComponent”, then call GetChildActor.

Ok! Done.
Now what do I do?

Because I still can’t cast the Actor “Puerta” from the child actor that I got, so I still have no acess to the public properties of the child.

Could you please share a snapshot of the Blueprint Editor with Puerta selected? and in the details panel , make it show the Class and Template part of the details.

You should be able to Cast that ChildActor to Puerta if everything in the template bit is correct.

Sure, here is
The only weird thing I see there is the “-1” at the end of the template name but it doesn’t seem to be relevant. Could that be the reason why the cast to fails?
Maybe I need to destroy the actor and make it again from scratch

That looks ok - yes, the -1 fine. I just tried making the same routine here and it works perfectly - is this what you did and it fails?

Pretty much. There are a few differences, maybe it’s something of that…


My comparison is not strict equaly and the blueprint is not placed in the constructor event but a custom event in the BP interface because I must handle it from outside the parent.

I am calling the event from a widget embedded in an object on the wall (the elevator call button called ‘ControlAscensorPB’ on the ground floor, 1st floor, etc.). And everything works perfect but that cast… honestly I am lost with this issue :sweat:
As you said, it shoud work… but not. Cast fails all the time. No access to the child. :frowning:

What happens if you PrintString the object it does have as a child?

Ok I just printscreened the object and class… and the editor keeps adding weird thigs to the end. A “2021” in the name of the object and a “_C” in the class name :confused: I swear I didn’t add anything of that to the names. WTF is doing this editor? lol

Oh - that’s showing that the cast IS working - the _C is normal (class) and the 2021 is to ensure the name is unique in your scene.

So if that cast is working ok - perhaps it is finding multiple objects with that name, and failing to cast the other?

Maybe rather than loop through testing the name - you could add a “Tag” to the Component and then use “GetComponentsByTag” instead?

image

That would need to be plugged into the “Ascensor”.

There is no other object with the name “Puerta” but I tried getting it by tag anyway. Same result :frowning: the 2nd cast fails.


tag

So there is no way to use actors as components? Should I put all actors directly to the maps?
If no choice then it would be a shame because I wanted to make a reusable full functional actor for all my maps. I’m an old school programmer so I like to reuse my classes haha. I expected this to be simpler.

Thanks anyway for the quick replies and all the help you are giving to me RecourseDesign, much appreciated sir!!

1 Like

You’re welcome!

Maybe another way to do it is to create a function in Ascensor that you call from the Interface, that function could then directly use the ChildActor component (drag onto your blueprint) and call GetChild() then cast from there?

1 Like

Ok I did it as you suggested, I created an event in the actor ‘Ascensor’ and I handle the components from inside when I call it. And touché! At least now the cast works well when I push the button playing on the host (it’s a multiplayer game) but fails on the client. This could be probably caused by a replication wrongly set but it’s a big advance!!

Thank you sou much RD, you rock!! :smiley:

1 Like