UChildActorComponent Call a Function

Hi
I have a character and wont to attach weapons(they are AActors) to it and call a function i made in the weapons code.

I tried to use UChildActorComponent but the function wasn’t called so maybe you could help me

This is the .h file in my character

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Fight, meta = (AllowPrivateAccess = "true"))
	class UChildActorComponent* WaponRComp; 
class AParentWapon* WaponR;

And this is the .cpp file(in the BeginPlay)

WaponRComp->SetChildActorClass(WeaponClass);
	WaponRComp->CreateChildActor();
	WaponR = Cast<AParentWapon>(WaponRComp);    

I also have a function that calls the function from the wapon when, I Pressed the LeftMouseButton

void AMyPlayer::Attack1Start()
{
	
	if (WaponR)
	{
		WaponR->Attack1();
	}
}

But WaponR is invalid.

if you have a Idea how I make it work, I would be happy to hear It

Thanks Tobias

I’m not entirely sure what you’re trying to accomplish by creating the component before the actor class, but part of your problem is that you’re casting a component to an actor, which shouldn’t work since they have no parent-child relationship.

Perhaps try this: WaponR = Cast(WaponRComp->GetChildActor());

Again, I’d really look into why you’re creating the component->actor relationship backwards.

Thanks for your answer : )

I want to say that my UChildActorComponent Should only be From the class AParentWapon

Do you know how I can do this?

And your cast doesn’t work because you aren’t saying to which class you want to cast.

WaponR = Cast(WaponRComp->GetChildActor());

But if I do it so:

WaponL = Cast<AActor>(WaponLComp->GetChildActor());

it works(I just casted it to an actor to make the test easier).

Thanks for your help but I still want to know if there isn’t an easier way to make this.

Sorry about not providing the proper cast. You should try to cast it to the weapon’s class type, or its parents. And the better way to do it would be to create the weapon, then get its component. I’m still not quite sure why you’re doing it this way.

Sorry but I don’t know how I create the weapon and then get its component.

Could you show me a code example please?

Just spawn the weapon actor in the world, and in the constructor created the desired component. Spawning Actors in Unreal Engine | Unreal Engine 5.1 Documentation

Thanks a lot it workes :slight_smile:

this is the .h file

UPROPERTY(EditDefaultsOnly, Category = Weapon)
	TSubclassOf<class AParentWapon> WeaponClass;

class AParentWapon* WaponR;

and .cpp file

  FActorSpawnParameters SpawnParams;
    SpawnParams.Owner = this;
    SpawnParams.Instigator = Instigator;
    WaponR = GetWorld()->SpawnActor<AParentWapon>(WeaponClass, GetActorLocation(), GetActorRotation(), SpawnParams);
    		
    WaponR->AttachToComponent(FpCamera, FAttachmentTransformRules::SnapToTargetNotIncludingScale);

Glad you got it working!

Getting the actor from the component makes sense when the WeaponClass is a blueprint which you may want to be able to change in the editor, so this should be initiated in the constructor.

Getting the component from actor may work using the post-constructor, since WeaponClass is still not initiated in the Constructor, but still you need to provide the class in the blueprint.

So, in this case, it makes no sense to do the extra job to init the component from actor in post-constructor, when it’s cleaner (and much more common) to init the actor from component in the constructor.

This approach is common because of the flexibility of being able to change the class in the editor and see the result instantly in the editor. I recommend this approach even if you don’t ever plan to change the class, because, usually, you will want to change it, for various reasons.

Thanks for your ideas, I didn’t thought this way : D
With post-constructor you mean BeginPlay?
Creating an actor in the Component is an interesting idea. But because it is the player I want to possess it’s needed to tell the Controller that he controls this pawn and if I want to create two components it wouldn’t work.

But in general this a good idea.

Before BeginPlay, for example: http://api.unrealengine.com/INT/API/Runtime/Engine/GameFramework/AActor/PostInitProperties/index.html

Thanks : D
Just for curiosity:
where dod you learn al the things about unreal?
Do you look at its source code?

I try to learn from any source I can, in this particular case I knew from my experience that it’s logic to have a function or two between constructor and BeginPlay. Then I googled for “ue4 c++ actor post constructor”. Looking into code also make a lot of sense.

Ah thanks for your help : D
have a nice day.