Ue4 BatteryCollector 教程笔记

H

UFUNCTION(BlueprintNativeEvent)

void EventName();

virtual void EventName_Implementation();

EventName事件发生时会调用这个函数

CPP

void ClassName::EventName_Implementation()

这个宏是UE4的强制内联函数,用于优化,返回Mesh的指针给Pickup

FORCEINLINE class UStaticMeshComponent *GetMesh() const { return PickupMesh;}

//BlueprintPure 此函数不会以任何方式影响其从属对象,并且可在蓝图或关卡蓝图图表中执行。

UFCUNTION相关

https://docs.unrealengine.com/latest/CHN/Programming/UnrealArchitecture/Reference/Functions/index.html

UFUNCTION(BlueprintPure, Category = "PickUp")

UFUNCTION(BlueprintCallable, Category = "PickUp")

UFUNCTION(BlueprintNativeEvent)

UFUNCTION(BlueprintImplementableEvent,Category="Power")

UPROPERTY相关

https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Reference/Properties/index.html

//这个属性就是可以让你在编辑器中修改组件,因为目前版本不加这个可能会无法修改吧,到时候试试看

UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="Power",Meta=(BlueprintProtected="true"))

UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category="Pickup",meta=(AllowPrivateAccess ="true"))

class UStaticMeshComponent *PickupMesh;

———————————————————————————————

总结一下,需要与蓝图交互的函数与变量会使用UFUNCTION,UPROPERTY宏,不需要的这不需要使用这些宏。

案例中使用的随机数相关代码

包含头文件

#include "Kismet/KismetMathLibrary.h"

FMath::FRandRange(float inMin,float inMax)

HUD部分

之前需要在项目中的CS文件中添加UMG模块之后,在对应的CPP文件中添加,不过我不太建议在C++文件中做HUD操作

PrivateDependencyModuleNames.AddRange(newstring[]{"Slate","SlateCore"});

#include "Blueprint/UserWidget.h"

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Power", Meta = (BlueprintProtected = "true"))

TSubclassOf<class UUserWidget> HUDWidgetClass;

HUD实例

UPROPERTY()

class UUserWidget *CurrentWidget;

if (HUDWidgetClass!=nullptr)

{

        CurrentWidget = CreateWidget<UUserWidget>(GetWorld(), HUDWidgetClass);

        if (CurrentWidget!=nullptr)

        {

                    CurrentWidget->AddToViewport();

        }

}