好的,所以在UE4上我试图制作一个UChildActorComponent作为玩家的武器。但是当我去APlayerWeapon类中调用一个函数时,它说:
错误:'UChildActorComponent'中没有名为'Shoot'的成员
这是我用来初始化武器的代码:
void ABaseCharacter::CreatePlayerWeapon()
{
//Create the weapon actor as a UChildActorComponent
Weapon = CreateDefaultSubobject<UChildActorComponent>(TEXT("PlayerWeapon"));
Weapon->SetChildActorClass(WeaponClass);
Weapon->CreateChildActor();
//Attach it at the socket called "GunSocket" that I created on the right hand
Weapon->SetupAttachment(GetMesh(), FName("GunSocket"));
}
然后在SetupPlayerInputComponent函数中,我使用它来尝试将“Shoot()”绑定到空格键(在游戏设置中设置为输入):
InputComponent->BindAction("Shoot", IE_Pressed, this, Weapon->Shoot());
以下是ABaseCharacter头文件中的相关代码行:
UPROPERTY(VisibleDefaultsOnly)
class UChildActorComponent* Weapon;
UPROPERTY(VisibleAnywhere, Category = "Base Character")
TSubclassOf<class APlayerWeapon> WeaponClass;
然后是APlayerWeapon类的相关代码行:
void APlayerWeapon::Shoot()
{
UE_LOG(LogTemp, Error, TEXT("Shooting"));
}
tldr;如果武器当前是ChildActorComponent,如何从APlayerWeapon访问Shoot()函数?
找出如何解决这个问题。您只需要获取子actor,然后转换为子actor类。然后你就可以调用这个函数了。
((APlayerWeapon *)Weapon->GetChildActor())->Shoot();