【发布时间】:2026-01-20 15:45:01
【问题描述】:
我一直在尝试在我的游戏中进行互动,但我做错了,我不知道在哪里!我将在下面包含一堆代码并省略不相关的部分。
我在引擎中创建接口。生成的类称为IInteract。它只包含一个名为 void Interact(ACharacterBase* Caller) 的函数。 ACharacterBase 只是我项目的默认棋子。这是 IInteract 的头文件。
#pragma once
#include "STRGZR/Characters/CharacterBase.h"
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "Interact.generated.h"
// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UInteract : public UInterface
{
GENERATED_BODY()
};
/**
*
*/
class STRGZR_API IInteract
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
void Interact(ACharacterBase* Caller);
};
现在,我创建了另一个名为 AInteractable 的类,并在 AInteractable 中实现了 IInteract。然后我从界面声明了 Interact(ACharacterBase* Caller) 。这是 AInteractable 的代码。
#pragma once
#include "Interact.h"
#include "STRGZR/Characters/CharacterBase.h"
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Interactable.generated.h"
UCLASS()
class STRGZR_API AInteractable : public AActor, public IInteract
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AInteractable();
protected:
UFUNCTION(BlueprintCallable)
void Interact(ACharacterBase* Caller);
public:
};
这是 void Interact(ACharacterBase* Caller) 的定义。
#include "Interactable.h"
// Sets default values
AInteractable::AInteractable()
{
}
void AInteractable::Interact(ACharacterBase* Caller)
{
Destroy();
}
我想我的第一个问题是,我应该在 AInteractable 和 中的交互功能上在哪里使用 virtual 和 override 关键字互动?
现在,我设置键绑定,以便在按下交互键时调用 ACharacterBase 中的 OnInteract。这是 ACharacterBase 的代码文件。
#include "GameFramework/CharacterMovementComponent.h"
#include "STRGZR/Interactable/Interact.h"
#include "CharacterBase.h"
// Sets default values
ACharacterBase::ACharacterBase()
{
LineTraceLength = 2000.f;
}
// Called to bind functionality to input
void ACharacterBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
// Set up gameplay key bindings
check(PlayerInputComponent);
// Bind Interact event
PlayerInputComponent->BindAction("Interact", IE_Pressed, this, &ACharacterBase::OnInteract);
}
FHitResult ACharacterBase::LineTraceForward()
{
FHitResult HitResult;
FCollisionQueryParams CollisionQueryParams;
FVector CharacterLocation;
FRotator CharacterRotation;
GetActorEyesViewPoint(CharacterLocation, CharacterRotation);
FVector Start = CharacterLocation;
FVector End = CharacterLocation + (CharacterRotation.Vector() * LineTraceLength);
ActorLineTraceSingle(HitResult, Start, End, ECC_Visibility, CollisionQueryParams);
return HitResult;
}
void ACharacterBase::OnInteract()
{
AActor* InteractedActor = LineTraceForward().GetActor();
IInteract* Interface = Cast<IInteract>(InteractedActor);
if (Interface)
{
UE_LOG(LogTemp, Warning, TEXT("Cast successful"))
}
}
这也是 ACharacterBase 的头文件。
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "CharacterBase.generated.h"
UCLASS()
class STRGZR_API ACharacterBase : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
ACharacterBase();
protected:
UFUNCTION(BlueprintCallable, Category = "Interaction")
FHitResult LineTraceForward();
UFUNCTION(BlueprintCallable, Category = "Input")
void OnInteract();
public:
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
我不知道为什么,但演员在这里失败了:
AActor* InteractedActor = LineTraceForward().GetActor();
IInteract* Interface = Cast<IInteract>(InteractedActor);
if (Interface)
{
UE_LOG(LogTemp, Warning, TEXT("Cast successful"))
}
我的第二个问题是为什么这个演员不起作用,有没有更好的方法在 AInteractable 上调用 Interact(ACharacterBase* Caller)?
感谢您的帮助!
【问题讨论】:
标签: c++ unreal-engine4