tsubclassof 获取PlayerControllerClass 对象

本文详细解释了Unreal Engine中TSubclassOf的概念及其用途,它是一种特殊的UClass模板,仅允许特定基类的子类。此外,还介绍了如何使用GetDefaultObject()获取已定义类对象的主副本及其中的默认变量。

TSubclassOf is a special template of UClass which only accepts specific subclasses of some base class. So it's still a UClass which is just a class specifier which you can spawn things from, but it does not have the variables or functions of the declared class. however, you can GetDefaultObject<T>() which is a master copy of the defined class object and contains the default variables which you can use before spawning that object.

 


tsubclassof是一种特殊的模板,UClass只接受特定的一些基类的子类。所以,它仍然是一个UCLASS这是一类可以产生的东西,但它没有在类声明的变量或函数。然而,你可以getdefaultobject ()是定义的类对象的主副本和包含默认的变量,您可以使用该对象在产卵。


凑合翻译看吧。


例子:

(自定义的controller类) * ctrl  =  GameMode->PlayerControllerClass.getdefaultobject();

.h文件:#pragma once #include "CoreMinimal.h" #include "Components/ActorComponent.h" #include "Kismet/GameplayStatics.h" #include "Blueprint/UserWidget.h" #include "PlayerInteractComponent.generated.h" class UUserWidget; class APlayerController; class UWHoverTipWidget; class UWidgetBlueprintLibrary; // 交互事件委托 DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnPlayerInteracted, AActor*, InteractActor); DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnPlayerInvestigated, AActor*, InvestigatedActor); UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) class GARYSCOMBATSYSTEMCPP_API UPlayerInteractComponent : public UActorComponent { GENERATED_BODY() public: // Called every frame UPlayerInteractComponent(); virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; UPROPERTY(EditDefaultsOnly,BlueprintReadWrite,Category="Intercat") float LineTraceDist=2000.0f; UPROPERTY(EditDefaultsOnly,BlueprintReadWrite,Category="Intercat") AActor* HoveringActor=nullptr; UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="Intercat") bool bEnableInteractTrace=true; UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="Intercat") bool bPrintDebugInfo=false; UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="Intercat") TEnumAsByte<ECollisionChannel> TraceChannel=ECC_Visibility;//碰撞通道 UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="UI") TSubclassOf<UUserWidget> HoverTipWidget;//悬停提示UI UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="Intercat") float PawnInteractractDist=350.0f;//交互距离 UPROPERTY(BlueprintAssignable, Category="Intercat") FOnPlayerInteracted OnBeginPlayEvent; //蓝图事件绑定,委托 UPROPERTY(BlueprintAssignable, Category="Intercat") FOnPlayerInteracted OnPlayerInteracted; UPROPERTY(BlueprintAssignable, Category="Intercat") FOnPlayerInteracted OnPlayerInvestigated; protected: // Called when the game starts virtual void BeginPlay() override; private: UFUNCTION() void CreateAndShowWidget(); void CreateHoverTipUI(); void UpdateHoverUI(AActor* NewHoverActor); // 更新悬停 UI UUserWidget*CreatedWidget; public: UFUNCTION(BlueprintCallable) void TickInteract(); bool PerformLineTrace(FHitResult& OutHitResult); // 执行射线检测 bool IsValidInteractable(AActor* ActorToCheck); // 检查 Actor 是否可交互 }; .cpp文件:// Fill out your copyright notice in the Description page of Project Settings. #include "Engine/Engine.h" #include "Components/PlayerInteractComponent.h" #include "Engine/World.h" #include "Blueprint/UserWidget.h" #include "Engine/CollisionProfile.h" #include "Kismet/GameplayStatics.h" #include "Blueprint/WidgetBlueprintLibrary.h" #include "Components/StaticMeshComponent.h" #include "Components/WidgetComponent.h" // Sets default values for this component's properties UPlayerInteractComponent::UPlayerInteractComponent() { // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features // off to improve performance if you don't need them. PrimaryComponentTick.bCanEverTick = true; // ... } // Called when the game starts void UPlayerInteractComponent::BeginPlay() { Super::BeginPlay(); void CreateHoverTipUI(); CreateAndShowWidget(); } void UPlayerInteractComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) { Super::TickComponent(DeltaTime, TickType, ThisTickFunction); TickInteract(); // 每帧执行TickInteract逻辑 } void UPlayerInteractComponent::CreateAndShowWidget() { APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0); if (HoverTipWidget&&PlayerController) { CreateWidget=HoverTipWidget<UUserWidget>(PlayerController, HoverTipWidget); } } void UPlayerInteractComponent::CreateHoverTipUI() { } void UPlayerInteractComponent::TickInteract() { if (!bEnableInteractTrace) return; FHitResult HitResult; if (PerformLineTrace(HitResult)) { AActor* HitActor = HitResult.GetActor(); if (IsValidInteractable(HitActor)) { UpdateHoverUI(HitActor); // 调试打印(蓝图的“打印字符串”) if (bPrintDebugInfo) { FString ActorName = HitActor ? HitActor->GetName() : TEXT("None"); UE_LOG(LogTemp, Warning, TEXT("Hovering Actor: %s"), *ActorName); GEngine->AddOnScreenDebugMessage(-1, 0.5f, FColor::Cyan, FString::Printf(TEXT("Hovering: %s"), *ActorName)); } // 触发交互事件(蓝图的“OnPlayerInteracted”) if (HitActor != HoveringActor) { OnPlayerInteracted.Broadcast(HitActor); HoveringActor = HitActor; } } else { UpdateHoverUI(nullptr); HoveringActor = nullptr; // 调试打印(无有效对象) if (bPrintDebugInfo) { UE_LOG(LogTemp, Warning, TEXT("No valid hover actor")); GEngine->AddOnScreenDebugMessage(-1, 0.5f, FColor::Red, TEXT("No hover actor")); } } } else { UpdateHoverUI(nullptr); HoveringActor = nullptr; // 调试打印(射线检测无命中) if (bPrintDebugInfo) { UE_LOG(LogTemp, Warning, TEXT("Line trace no hit")); GEngine->AddOnScreenDebugMessage(-1, 0.5f, FColor::Red, TEXT("No line trace hit")); } } } bool UPlayerInteractComponent::PerformLineTrace(FHitResult& OutHit) { APlayerController* PC = UGameplayStatics::GetPlayerController(GetWorld(), 0); if (!PC) return false; FVector Start; FRotator Rotation; PC->GetPlayerViewPoint(Start, Rotation); FVector End = Start + Rotation.Vector() * LineTraceDist; FCollisionQueryParams Params; Params.AddIgnoredActor(GetOwner()); return GetWorld()->LineTraceSingleByChannel(OutHit, Start, End, TraceChannel, Params); } bool UPlayerInteractComponent::IsValidInteractable(AActor* Actor) { if (!Actor) return false; float Distance = FVector::Dist(GetOwner()->GetActorLocation(), Actor->GetActorLocation()); return Distance <= PawnInteractractDist; } void UPlayerInteractComponent::UpdateHoverUI(AActor* NewHoverActor) { if (!HoverTipWidget)return; if (NewHoverActor && HoverTipWidget) { HoverTipWidget.SetVisibility(ESlateVisibility::Visible); } else if (HoverTipWidget) { HoverTipWidget->SetVisibility(ESlateVisibility::Collapsed); } } 这是一个PlayerInteractComponent虚幻引擎的交互组件,现在需要把蓝图组件迁移到C++组件中里面有一个TickInteract函数方法和一些变量已经在C++中写好了,但是TickInteract函数方法里面的蓝图还没有迁移,现在先迁移事件图表里面的beingPlay事件和tick事件being play事件就是创建用户控件添加一个对应的类(UW_HoverTip),然后提升为变量改名为HoverTipWidget,接着就是添加到视口,设置他的可视性。在我这份代码里面如何实现呢
07-15
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值