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

I am working on a weapon changing system for Melee.
I would like to let the players choose between many different weapons that have different sizes and shapes and hitboxes.
And I would like to easily be able to add new weapons in the future without rewriting every actor that can use the weapon’s BluePrint.

I plan on making a separate BluePrint for each weapon that has the mesh, the hitbox, some sockets for particles, and some variables for speed damage, etc
I like having each weapon in its own self contained BluePrint as opposed to one that stores them all.

I want to toggle the weapon’s hitbox on and off, and spawn particles from sockets based on animNotify events
I am not sure what is best way to pass the right information from the AnimBluePrint back to certain Components of the Weapon’s BluePrint.
I know how to do it if I call it by name, but…

I want to avoid a long complicated switch statement with 20 outcomes that says
if he is carrying the Mace then cast to MaceBP,
if he is carrying the VorpalSword then cast to VorpalSwordBP,

Is there a way to send information to whatever Child Actor is the one that is currently in use without explicitly naming it?
and then I need to find a specific component like the Collision Component, or the socket from where I want to spawn Particles and then make something happen.

Can anyone show me an example how I can do this or point me in the right direction?

Thanks

What you need is an EquippedWeapon variable on your PlayerCharacter. So when you spawn or swap weapons, you set the Actor Reference variable to equal whatever you spawned/swapped.

That way you only need to cast your PlayerCharacter, as PlayerCharacter - > Get EquippedWeapon -> Get/Set/Play whatever you need to do

@Dedrick

Thanks, I would like the Equipped Weapon to be a BluePrint. What type of Variable should I use?

You have 2 choices, Actor Reference or specifically the WeaponClass Actor Reference. So, where you select the variable type, start typing in your base weapon class (the class all weapons are based off of despite type).

It will give you 4 choices in a drop down, choose Reference

This assumes you started with one base weapon class and built all children/childrenofchildren off that class

That is a very good idea.
I am still working on the first weapon.
and then I saw that I really needed to plan this out or I would have a Big Ball of Mud (https://en.wikipedia.org/wiki/Big_ball_of_mud)

but I will try making some child classes of a general weapon base class

The way I have weapons set up right now, I have my base class which handles whether something is hitscan(linetrace) or projectile, firing logic, reload logic, ammo count, mesh, collision, audio etc. Everything a gun will have without any specifics to the gun itself.

From there you make children for each weapon and fill in the variables you made on the parent class. The parent class is blank or it can use placeholder values for testing

Basically break your weapons down into “What does every weapon have no matter what it is” and that’s what you make your base class. From there you can make a Sword class, make children of the sword class for each sword.

It’s kind of like the movie Inception.

The end result is when you want to make a new weapon, you just choose the class, make a child, fill in the blanks

1 Like

Sounds like a great plan. I will start building it.
I am still a little confused about how to get it’s components.
But when I think more about it, I kind of prefer a function within the weapon class toggling the hitbox or particles
over having the animBP or the PlayersBP do those things.
Just to keep things encapsulated

Oh No I am stuck. I set up my Parent/Child Class structure and the variable. But I can’t set the variables value.

I made a base Class called WeaponBP and a Child of that class WeaponBP_Hatchet
I set up a variable called EquippedWeapon The Variable is a Reference to an object of the type WeaponBP

I want to be able to set the value of my EquippedWeapon Variable to reflect the appropriate child class in this case WeaponBP_Hatchet
But if I try to plug them in directly it says they are incompatable.

How can I set this value?

On your SpawnActor From Class node, right click the return value and “Promote to Variable”.

It should have worked the other way though, I will have to check my loadout/weapon switch script when I get home because I spawn 3 different classes with the same parent and the variable works

You just want to gain access to child_BP variables through casting?

I didn’t spawn it. I was finally able to set the Variable. But I don’t like the way I did it at all.

Here is what I did:

  1. I setup my Player Character BluePrint with a Utility->ChildActor component by using that Big Green +ADDCOMPONENT button
  2. On the PlayerCharacter Construction script, I dragged out a reference to the ChildActor component
  3. Dragged a wire off of the ChildActor component and used a SetChildActorClass node. I set the weapon there. Works fine.

I read these 2 answer threads

They both said GetAllActorsOfClass and do a ForEachLoop
I tried it and it worked. I was able to set my EquippedWeapon the variable.
Now I can access it without knowing it’s name, turn the hit box on and off, change the variables
I changed the weapon to a different one and everything still worked fine.
It did everything I wanted to do. I can access it without knowing it’s name.
But this does not seem like an elegant solution.
Getting all elements of an array seemed like a bad idea.

In fact, I hate it. To me it has a bad smell.

Especially because now if I have more than one instance of the weapon, And I executed that piece of code, I
would need another system to track which character’s weapon is which element in the Array. and then get and set
it just feels wrong, and that I will make a mess if I build upon it.

There has to be a better way.

Can someone please show me a better way?

Again… Do you just want to access a child actors variables?

If so, all you need to do is cast to the parent. Of course you will need a reference to the child, and that all depends on how the player gets the weapon in the first place

@VanLacke

Yes, in principle that might just be the elegant solution that I am looking for. I need to access the ChildComponent’s functions and variables through casting.

In that case, yes, simply casting to the parent BP will work.

e.g… Item_Sword_BP -> cast to Item_Master_BP… Will give you all the variables/functions associated with the parent, but the values that the child has. That way, all you need is a reference to whatever you have equipped, and using that reference you can have any weapon and use the exact same code to get its variables :slight_smile:

I am not sure what the best way to give him the weapon is. (Spawning vs always having something)
I started out by equipping him with an object that is the weapons base class.
I then put a starting weapon in the construction script
and was planning on changing this around with the SetChildActor Node.

Will this solve my problem?

If this will work, I already like this much better than the Array.
Because I can just do the same thing for the enemies.

Trying it now

OK I tried it but without having that EquippedWeapon Variable
it seems that I am back to the same problem I started with.

If I drag a reference to the ChildComponent onto the event graph,
it doesn’t want to let me access the functions of my base class.
I can access the functions of the derived class if I name it explicitly
but this is exactly what I was trying to avoid.

It worked with the variable I just didn’t like that I had to get an array

Can you show me with a screenshot?

You should be using an actor variable that you set. Again, this depends on how your system is set up. For example, it can be set on pickup.

Pickup weapon -> set variable -> cast

Sure. I probably should have done that earlier.
Still figuring out how to screen capture on a Mac.
I Just switched over for this project.

This is in the PlayerCharacter’s construction Script

And this is in the PlayerCharacter’s Event Graph

I setup the Weapon ChildComponent to be of the WeaponBP which is a base class but not a specific weapon
I setup EquippedWeapon Variable to be a reference to an Object of WeaponBP type (the base class)
I then set the ChildActor component’s Class in the construction script, that is where I can name the weapon.
(The weapon is a child BluePrint class of WeaponBP)
And then when I call a function on the EquippedWeapon Variable it works on the child and I don’t need to know what the child is.
Which is exactly what I wanted to do.
It works like that, but I want to avoid using that array.
I am afraid it will make problems.

If I try to drag over a reference to Weapon into the graph and then drag a wire off of it, it won’t let me access the functions in the weapon child unless I explicitly name it like call this function as WeaponBP_Hatchet or WeaponBP_Cleaver if I try to just call the function in the base class the wire will not attach.

In that case you should be able to drag the child component out and Set Equipped Weapon after you do Set Child Component. Unhook the GetActors/array part for now

But try it as a plain Actor Object Reference. I’m curious if the cast to WeaponBP will work in your Anim Blueprint, technically it should because the actor object reference is set to a child of WeaponBP.

I have never done weapons as child components so I don’t know the outcome of how the cast would work, you might need an Is Valid node before the cast in the anim blueprint or you may get errors