本文要记录两种方式蓝图调用C++函数:
一、蓝图调用Actor类的函数 1.1 创建一个基于Actor类:
1.2 创建后,内容浏览器中添加C++类,并自动打开VS在新建的类下添加如下代码:
代码的方法为获取本机IP
.h
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #pragma once #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "MyActor.generated.h" UCLASS ()class C_MP_LOBBY_API AMyActor : public AActor{ GENERATED_BODY () public : AMyActor (); protected : virtual void BeginPlay () override ; public : virtual void Tick (float DeltaTime) override ; UFUNCTION (BlueprintCallable) FString ShowIP () ; };
.cpp
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #include "MyActor.h" #include "Windows/AllowWindowsPlatformTypes.h" #include "Windows/PreWindowsApi.h" #include <winsock2.h> #include "Engine/GameEngine.h" #include "Windows/PostWindowsApi.h" #include "Windows/HideWindowsPlatformTypes.h" AMyActor::AMyActor () { PrimaryActorTick.bCanEverTick = true ; } void AMyActor::BeginPlay () { Super::BeginPlay (); } void AMyActor::Tick (float DeltaTime) { Super::Tick (DeltaTime); } FString AMyActor::ShowIP () { WSADATA Wsadata; char name[255 ]; if (!WSAStartup (MAKEWORD (2 , 0 ), &Wsadata)) { if (!gethostname (name, sizeof (name))) { hostent* host = gethostbyname (name); if (host != nullptr ) { FString a = inet_ntoa (*(struct in_addr*)*host->h_addr_list); return (TEXT ("%s" ), a); } } } return TEXT ("0.0.0.0" ); }
1.4 编写代码后,进行编译 1.5 调用Actor类的两种方法 1) 关卡蓝图中调用将创建的c++类拖拽到场景中,世界大纲便会出现此Actor
打开关卡蓝图,获到Actor类,并调用Show IP函数,如下:
2) 创建蓝图类并调用创建一个蓝图类(在内容浏览器右键),选取父类为当前的c++Actor类
起名比如BP_Actor,双击打开,编写如下:
效果同上
二、蓝图调用蓝图函数库注意:此方法创建出的子类中的方法必须为静态!
2.1 创建一个基于蓝图函数库:
2.2 创建后,内容浏览器中创建了蓝图函数库C++类,并自动打开VS编写代码
注意:函数声明前需加static
.h代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #pragma once #include "CoreMinimal.h" #include "Kismet/BlueprintFunctionLibrary.h" #include "MyBlueprintFunctionLibrary.generated.h" UCLASS ()class C_MP_LOBBY_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary{ GENERATED_BODY () UFUNCTION (BlueprintCallable) static FString ShowIP () ; };
.cpp代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include "MyBlueprintFunctionLibrary.h" #include "Windows/AllowWindowsPlatformTypes.h" #include "Windows/PreWindowsApi.h" #include <winsock2.h> #include "Engine/GameEngine.h" #include "Windows/PostWindowsApi.h" #include "Windows/HideWindowsPlatformTypes.h" FString UMyBlueprintFunctionLibrary::ShowIP () { WSADATA Wsadata; char name[255 ]; if (!WSAStartup (MAKEWORD (2 , 0 ), &Wsadata)) { if (!gethostname (name, sizeof (name))) { hostent* host = gethostbyname (name); if (host != nullptr ) { FString a = inet_ntoa (*(struct in_addr*)*host->h_addr_list); return (TEXT ("%s" ), a); } } } return TEXT ("0.0.0.0" ); }
编写后进行编译
2.3 使用蓝图函数库C++类这种方法使用函数比较简单,因为蓝图函数库是全局的,只要在蓝图中直接引用函数即可
提示:如果出现“未能找到类型或命名空间名 unrealbuildtool 是否缺少using指令或程序集引用”错误
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 using UnrealBuildTool; public class MyProject : ModuleRules{ public MyProject (ReadOnlyTargetRules Target) : base(Target) { PublicDependencyModuleNames.AddRange (new string[] { "Core" , "CoreUObject" , "Engine" , "BlueprintGraph" }); } }
再删除Intermediate和Binaries目录后,重新编译