UE5蓝图与C++通信方式大全

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(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值