这里写目录标题
1)、直接引用
在PlayerCharacter蓝图中,添加一个变量,类型为NPCCharacter的引用。在交互事件中,调用NPCCharacter的ShowDialog方法。
// PlayerCharacter.h
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Interaction")
class ANPCCharacter* NPCReference;
// PlayerCharacter.cpp
void APlayerCharacter::Interact()
{
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = GetInstigator();
FTransform SpawnTransform;
SpawnTransform.SetLocation(GetActorLocation() + FVector(100.0f, 0.0f, 0.0f)); // Spawn 100 units in front of the player
NPCReference = GetWorld()->SpawnActor<ANPCCharacter>(ANPCCharacter::StaticClass(), SpawnTransform, SpawnParams);
if(NPCReference)
{
NPCReference->ShowDialog();
}
}
//NPCCharacter.cpp
void ANPCCharacter::ShowDialog()
{
UE_LOG(LogTemp, Warning, TEXT("ShowDialog "));
}
//NPCCharacter.h
public:
void ShowDialog();
2)、蓝图接口
创建一个蓝图接口IInteractionInterface,添加一个名为ShowDialog的方法。在NPCCharacter中实现此接口,并实现ShowDialog方法。在PlayerCharacter中,通过GetInterface方法获取接口实例,并调用ShowDialog方法。
// InteractionInterface.h
class UE521TEST_API IInteractionInterface
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Interaction")
void ShowDialog(


3917

被折叠的 条评论
为什么被折叠?



