【UE4笔记】使用UFUNCTION将蓝图函数改写为C++函数《将蓝图转化为C++》 官方视频教程笔记

函数改写步骤

  1. 查看原有函数的返回值,在C++类头文件中添加相同返回值的同名函数(蓝图类型对应的C++类型见下文)

  2. 在刚刚写好的函数上面新增一行,填写UFUNTCTION预编译指令

    	UFUNCTION(BlueprintCallable, BlueprintPure)
    	FVector GetMaxGrabLocation() const;
    

    其中,BlueprintCallable是必选项,否则无法在蓝图中调用该函数
    BlueprintPure是在蓝图中,将其作为纯函数解析。即使用时不会出现执行引脚。作用与在蓝图函数中勾选“纯函数”一致

  3. 编写函数体
    这里可以根据原有的蓝图函数函数体编写,可能需要引入库。详见博文

  4. 在蓝图中删除原有的蓝图函数

蓝图变量类型对应的C++类型

普通数据类型

在这里插入图片描述
蓝图简化了C++的一些类型。比如蓝图中只有float没有double,整型只有int32和int64没有uint

工具类型(结构体)


C++中结构体以F开头

对象类型

在这里插入图片描述
继承自Object的在C++中均以U开头,除非其继承自Actor,此时以A开头

完整代码

.h

#pragma once

#include "CoreMinimal.h"
#include "Components/SceneComponent.h"
#include "Grabber.generated.h"

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent), Blueprintable )
class BLUEPRINTSTOCPP_API UGrabber : public USceneComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UGrabber();

protected:
	// Called when the game starts
	virtual void BeginPlay() override;

	UFUNCTION(BlueprintCallable, BlueprintPure)
	FVector GetMaxGrabLocation() const;

public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

protected:
	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	float MaxGrabDistance = 100.0f;
		
};

.cpp

#include "Grabber.h"
#include "Kismet/KismetMathLibrary.h" //为了编写函数体引入的库

// Sets default values for this component's properties
UGrabber::UGrabber()
{
	// 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 UGrabber::BeginPlay()
{
	Super::BeginPlay();

	// ...
	
}

FVector UGrabber::GetMaxGrabLocation() const
{
	return GetComponentLocation() + UKismetMathLibrary::GetForwardVector(GetComponentRotation()) * MaxGrabDistance;
}


// Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	// ...
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UE4中,可以通过蓝图调用C++全局函数。首先,需要在C++代码中声明并实现全局函数。具体的步骤如下: 1. 打开UE4的项目代码文件夹,并找到对应的.h头文件和.cpp源文件。 2. 在.h头文件中声明全局函数。例如,可以在声明文件的末尾添加如下代码: ``` #pragma once #include "CoreMinimal.h" UCLASS() class MYPROJECT_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary { GENERATED_BODY() public: UFUNCTION(BlueprintCallable, Category = "MyFunctions") static void MyGlobalFunction(); }; ``` 3. 在.cpp源文件中实现全局函数。例如,可以在源文件的末尾添加如下代码: ``` #include "MyBlueprintFunctionLibrary.h" void UMyBlueprintFunctionLibrary::MyGlobalFunction() { // 在此处实现全局函数的逻辑 // 例如,可以在这里添加打印语句 UE_LOG(LogTemp, Warning, TEXT("调用了全局函数!")); } ``` 4. 保存并编译C++代码,确保函数成功添加到UE4项目中。 接下来,在蓝图中调用该全局函数的步骤如下: 1. 打开UE4蓝图编辑器。 2. 在蓝图中选择要调用全局函数的节点。 3. 在蓝图编辑器右侧的详情面板中,找到“蓝图调用”节点。 4. 从“蓝图调用”节点的输出引脚拖出连接线,并将其链接到“执行入口”节点。 5. 在连接线上右键点击并选择“Convert to Previous C++ Function Call”(转换为C++函数调用)。 6. 在“函数调用”节点的类下拉菜单中选择“UMyBlueprintFunctionLibrary”。 7. 在函数下拉菜单中选择要调用的全局函数,例如“MyGlobalFunction”。 8. 保存蓝图并点击运行即可调用全局函数。 通过以上步骤,就可以在UE4使用蓝图调用C++全局函数了。调用全局函数可以方便地实现一些复杂的逻辑和功能,提高UE4项目的灵活性和扩展性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值