我的目标是从“玩家角色演员对象”的代码访问“门演员对象”的功能,例如打开或关闭门。
如果有人可以解决我的问题或知道类似或更好的解决方案,那将很好。
这是来自“门”的代码:
#include "DefaultDoorRotator.h"
#include "FPSCharacter.h"
void ADefaultDoorRotator::OnOverlapBegin(UPrimitiveComponent* OverlapComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, int32 OtherBodyIndex, bool bFromSweepm, const FHitResult& SweepResult)
{
if (player_controled_door)
{
if (OtherActor != nullptr)
{
if ((OtherActor->GetClass()->GetDefaultObject<AFPSCharacter>()!=0))
{
OtherActor->GetClass()->GetDefaultObject<AFPSCharacter>()->set_interaction_object(ActionComponent);
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Registered Door on Player!"));
}
}
}
}
播放器上的内容:
#include "FPSCharacter.h"
#include "DefaultDoorRotator.h"
USceneComponent* object_for_interaction;
UFUNCTION()
void set_interaction_object(USceneComponent* obj)
{
if (obj != 0)
{
object_for_interaction = obj;
}
}
void AFPSCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
PlayerInputComponent->BindAction("CharacterInteract", IE_Pressed, this, &AFPSCharacter::Interaction_start);
}
void AFPSCharacter::Interaction_start()
{
want_to_interact = true;
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Want to interact!"));
if (object_for_interaction != 0)
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Begin to interact!"));
object_for_interaction->GetClass()->GetDefaultObject<ADefaultDoorRotator>()->ToggleDoor();
}
}
在可行的情况下,应该运行门演员对象的功能。
答案 0 :(得分:0)
这是一个奇怪的示例,因为您可以通过在玩家角色按下绑定键时检查重叠的actor是否有门来实现相同的行为。但是,我假设您想知道当UE4 api向您传递AActor指针时,通常如何在其他特定类型的actor上调用函数。
UE4具有模板化的Cast函数,如果转换失败,该函数将返回null。通常,这就是您要用来在这里尝试做的事情。例如,
if (OtherActor != nullptr)
{
if ((OtherActor->GetClass()->GetDefaultObject<AFPSCharacter>()!=0))
{
OtherActor->GetClass()->GetDefaultObject<AFPSCharacter>()->set_interaction_object(ActionComponent);
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Registered Door on Player!"));
}
}
成为
AFPSCharacter* character = Cast<AFPSCharacter>(OtherActor);
if (character)
{
character->set_interaction_object(ActionComponent);
}