How do I cast to a child actor without knowing the actors's name?

She doesn’t want to let me do it. I tried as a plain Actor Object Reference as well.

Again, how you set up the reference completely depends on how your overall system if going to be. There are so many ways to do it, however I can say for sure that your method is not the way to go about it, as you’d be getting EVERY weapon on the map and looping through them…

For testing purposes you can simply spawn a sword, grab the output, and use that to access the weapon data. Using this method you won’t actually need to cast to get the variables… However you will if you have a nonspecific actor reference, like a trace actor output.

(ignore double print)


ffff2.png

Your variable is still WeaponBP type from when you set it before, try changing it to just Actor Object Reference. This will allow the variable to be anything that is an actor.

To access it’s functions and variables you will need to use a Cast to WeaponBP

I tried changing the EquippedWeapon Variable to an Actor Object Reference and I could not use my child component to set EquippedWeapon it didn’t seem to matter if it was a reference of type WeaponBP or a reference of Type Actor. I kept getting that incompatible. She just did not want to let me stick it in.

Maybe I would have more control if I spawned the item as you mentioned that you had done earlier. This is what Vanlacke is suggesting also.

I will try setting up everything again, but this time try spawning it instead.

Oh, I think I know why. The child component isn’t an actor, it’s a actor component. When I’m at home I will play with actor components and see what can be done. I actually want to test this to handle 4 weapons, different than the way I am doing it now.

When you spawn the actor you will need an AttachActorToActor or AttachActorToComponent. Or in the weapon itself you need to Event Overlap attach actor to actor/component, or on Event Begin Play. It all depends on how you want weapons to work

The way I have it is a script spawns all the weapons in their selected loadout, places them in an array and sets them hidden in game. Then it goes to the first index and attaches it to the player, sets it visible and sets EquippedWeapon. It’s similar to how ShooterGame does it in C++

Yes, good observation. I changed it. And when I change the type to ActorComponent I can connect the real object to the variable. But I can’t drag a wire off of the variable and access it’s functionality, and I can’t cast it to WeaponBP

Here you go, it was simple and this is actually awesome because it gives me an idea. Drag “Get Child Actor” off the Component and you can plug that into your cast node

2 Likes

YES DEDRICK IT WORKS BEAUTIFULLY Thank You Thank You Thank You!
I tested it, works perfectly. I can switch the weapon.
I can call on its functions. And I don’t need to know what weapon it is.
I can have as many weapons as I want
with different shapes, sizes, particles, properties, variables
and I don’t need to maintain a long switch statement, an array or even an EquippedWeapon variable.
I don’t need to spawn a long list of weapons for each actor and hide them.
I just let whoever is holding the weapon decide which one to use and everything works.
Exactly what I wanted to do. Thank YOU again

  •  My only remaining concern is how the garbage cleanup is handled when you switch a weapon
    Does that node SetChildActorClass destroy the old one from memory???
    If not I need to take out the garbage.  *
    

Other than that one unanswered question, I love the design.
Elegant, powerful, adaptable and easily expandable.
In the end this is all that was left:

a base class for any weapon
a child class for the specific weapon
and then this setup in the PlayerBP (or whoever is holding the weapons blueprint.)

Player Construction Script

Player Event Graph

I believe the old actor is destroyed which is less than ideal in the case of anything that uses ammo or durability but not the end of the world, you can save all those variables right before you swap weapons and reload them on construct.

Kind of a weird workaround, this is why I prefer the spawning method

Check out this tutorial on how to attach weapons to different sockets and such

What is cool about that is all your weapons show on your person and swap when you switch weapons

I do not believe that you will break the (memory) bank unless you are switching around 1,000,000’s of weapons on 1,000,000’s of clients at the same time, numerous times, over a long time.