[UE4]自定义LOG

本文介绍了如何在C++中使用DECLARE_LOG_CATEGORY_EXTERN和DEFINE_LOG_CATEGORY宏定义,以实现高效的Log日志管理。从头文件声明到源文件定义,逐步详解了在项目中设置自定义日志类别的重要步骤。

在头文件声明

DECLARE_LOG_CATEGORY_EXTERN(LogYourLog, Log, All);

在源文件定义

DEFINE_LOG_CATEGORY(LogYourLog);
### 自定义 Unreal Engine 5 中 PathFollowingComponent 的实现 在 Unreal Engine 5 (UE5) 中,`PathFollowingComponent` 是导航系统的核心组件之一,负责控制 AI 角色沿着计算好的路径移动。要自定义 `PathFollowingComponent` 的行为,可以通过继承该类并重写其核心函数来实现特定功能。 以下是创建自定义 `PathFollowingComponent` 实现的方法: #### 创建自定义 PathFollowingComponent 类 通过 C++ 编程语言扩展 UE5 功能是最常见的做法。首先,在项目中新建一个派生自 `UPathFollowingComponent` 的类[^1]。 ```cpp #include "NavigationSystem/PathFollowingComponent.h" UCLASS(ClassGroup = Custom, meta = (BlueprintSpawnableComponent)) class YOURPROJECT_API UCustomPathFollowingComponent : public UPathFollowingComponent { GENERATED_BODY() public: UCustomPathFollowingComponent(); protected: virtual void BeginPlay() override; private: // 覆盖父类中的关键方法以修改默认行为 virtual ENavigationRequestResult::Type MoveToLocationImpl(AActor* MovingActor, const FVector& Destination, float AcceptanceRadius, FNavPathSharedPtr* OutPath = nullptr, bool bAllowPartialPath = false, TWeakObjectPtr<UNavLinkProxy> InOverrideLink = nullptr) override; }; ``` #### 修改路径跟随逻辑 为了改变角色如何沿路径移动的行为,可以覆盖 `MoveToLocationImpl` 方法,并在此处加入自己的算法或调整参数设置[^2]。 ```cpp ENavigationRequestResult::Type UCustomPathFollowingComponent::MoveToLocationImpl( AActor* MovingActor, const FVector& Destination, float AcceptanceRadius, FNavPathSharedPtr* OutPath, bool bAllowPartialPath, TWeakObjectPtr<UNavLinkProxy> InOverrideLink) { // 添加自定义逻辑前的操作 UE_LOG(LogTemp, Warning, TEXT("Custom logic applied before moving to location.")); // 调用基类实现完成实际的路径规划与执行 return Super::MoveToLocationImpl(MovingActor, Destination, AcceptanceRadius, OutPath, bAllowPartialPath, InOverrideLink); } ``` #### 注册新 Component 到 Navigation System 为了让引擎知道使用的是新的 `PathFollowingComponent` 版本,需确保此组件被正确注册到导航系统中。通常情况下,这一步由 Behavior Tree 或其他高级 AI 控制器自动处理;但如果手动管理,则需要显式指定使用的组件类型[^3]。 ```cpp // 在 Pawn 或 Character 初始化时替换默认组件 void APawnWithCustomAI::InitializeAI() { if (!HasAuthority()) { return; } UNavigationSystemV1* NavSys = FNavigationSystem::GetCurrent<UNavigationSystemV1>(GetWorld()); if (NavSys && !NavSys->IsRunning()) { NavSys->Init(); } // 替换原有的 Path Following 组件为自定义版本 UCustomPathFollowingComponent* CustomPFComp = NewObject<UCustomPathFollowingComponent>(this, UCustomPathFollowingComponent::StaticClass(), NAME_None, RF_Transient | RF_DuplicateTransient); SetPathFollowingComponent(CustomPFComp); // 启动初始目标位置请求作为测试案例 GetPathFollowingComponent()->MoveToLocation(GetActorLocation() + FVector(0.f, 0.f, 100.f), 10.f); } ``` 以上代码展示了如何构建以及集成一个新的 `PathFollowingComponent` 进入现有游戏对象之中[^4]。 --- ### 注意事项 - **性能优化**:当引入复杂运算至路径更新过程中,请务必考虑运行效率问题。 - **调试工具**:利用蓝图可视化调试或者日志输出确认每步动作是否按预期发生。 - **兼容性验证**:更改基础框架可能影响依赖于标准 API 的第三方插件运作状况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值