local CubeClass = UE4.UClass.Load("/Game/Cube_Blueprint.Cube_Blueprint") local World = self:GetWorld() local Cube_Ins = World:SpawnActor(CubeClass,self:GetTransform(),UE4.ESpawnActorCollisionHandlingMethod.AlwaysSpawn, self, self)
if UE4.UKismetSystemLibrary.DoesImplementInterface(Cube_Ins,UE4.UMyInterface) then print(fmt("{1} inheritanced {2}",CubeClass,UE4.UMyInterface)) end
// This class does not need to be modified. UINTERFACE(BlueprintType,MinimalAPI) classUINetGameInstance : public UIBaseEntityInterface { GENERATED_BODY() };
local GameInstance = UE4.UFlibGameFrameworkStatics.GetNetGameInstance(self); local findRet1,findRet2 = GameInstance:FindSubsystem("TouchController")
指定类和函数名调用
也可以直接通过指定实现该接口的类型名字来调用,就像函数指针,需要把调用该函数的对象传递进去:
1 2
local GameInstance = UE4.UFlibGameFrameworkStatics.GetNetGameInstance(self); local findRet1,findRet2 = UE4.UNetGameInstance.FindSubsystem(GameInstance,"TouchController")
指定接口的类和函数名调用
以及通过接口的类型调用(因为接口也是UClass,接口中的函数也都标记了UFUNCTION):
1 2
local GameInstance = UE4.UFlibGameFrameworkStatics.GetNetGameInstance(self); local findRet1,findRet2 = UE4.UINetGameInstance.FindSubsystem(GameInstance,"TouchController")
获取UClass
lua中获取uclass可以用于创建对象,其方法为:
1
local uclass = UE4.UClass.Load("/Game/Core/Blueprints/AI/BP_AICharacter.BP_AICharacter_C")
Load的路径是该类的PackagePath。
如,在lua中加载UMG的类然后创建并添加至视口:
1 2 3 4 5
functionLoadingMap_C:ReceiveBeginPlay() local UMG_C = UE4.UClass.Load("/Game/Test/BPUI_TestMain.BPUI_TestMain_C") local UMG_TestMain_Ins = UE4.UWidgetBlueprintLibrary.Create(self,UMG_C) UMG_TestMain_Ins:AddToViewport() end
local Object = LoadObject("/Game/Core/Blueprints/AI/BT_Enemy")
比如加载某个材质球给模型:
1 2 3 4
function Cube3_Blueprint_C:ReceiveBeginPlay() local MatIns = LoadObject("/Game/TEST/Cube_Mat_Ins") UE4.UPrimitiveComponent.SetMaterial(self.StaticMeshComponent,0,MatIns) end
local World = self:GetWorld() local WeaponClass = UE4.UClass.Load("/Game/Core/Blueprints/Weapon/BP_DefaultWeapon.BP_DefaultWeapon") local NewWeapon = World:SpawnActor(WeaponClass, self:GetTransform(), UE4.ESpawnActorCollisionHandlingMethod.AlwaysSpawn, self, self, "Weapon.BP_DefaultWeapon_C")
NewObject
lua中调用NewObject以及动态绑定:
1
local ProxyObj = NewObject(ObjClass, self, nil, "Objects.ProxyObject")
local UMG_C = UE4.UClass.Load("/Game/Test/BPUI_TestMain.BPUI_TestMain_C") local UMG_TestMain_Ins = UE4.UWidgetBlueprintLibrary.Create(self,UMG_C) UMG_TestMain_Ins:AddToViewport()
// in class UPROPERTY() FGameInstanceDyDlg GameInstanceDyDlg;
在lua中绑定,可以绑定到lua的函数:
1 2 3 4 5 6 7
local GameInstance = UE4.UFlibGameFrameworkStatics.GetNetGameInstance(self); GameInstance.GameInstanceDyMultiDlg:Add(self,LoadingMap_C.BindGameInstanceDyMultiDlg)
-- test bind dynamic multicast delegate lua func functionLoadingMap_C:BindGameInstanceDyMultiDlg(InString) UE4.UKismetSystemLibrary.PrintString(self,InString) end
/** * Perform a latent action with a delay (specified in seconds). Calling again while it is counting down will be ignored. * * @param WorldContextWorld context. * @param Duration length of delay (in seconds). * @param LatentInfo The latent action. */ UFUNCTION(BlueprintCallable, Category="Utilities|FlowControl", meta=(Latent, WorldContext="WorldContextObject", LatentInfo="LatentInfo", Duration="0.2", Keywords="sleep")) staticvoidDelay(UObject* WorldContextObject, float Duration, struct FLatentActionInfo LatentInfo );
在lua中可以通过协程(coroutine)来实现:
1 2 3 4 5 6 7 8 9
functionLoadingMap_C:DelayFunc(Induration) coroutine.resume(coroutine.create( function(WorldContectObject,duration) UE4.UKismetSystemLibrary.Delay(WorldContectObject,duration) UE4.UKismetSystemLibrary.PrintString(WorldContectObject,"Helloworld") end ), self,Induration) end
functionLoadingMap_C:DelayFunc(Induration) coroutine.resume(coroutine.create(LoadingMap_C.DoDelay),self,self,Induration) end
functionLoadingMap_C:DoDelay(WorldContectObject,duration) UE4.UKismetSystemLibrary.Delay(WorldContectObject,duration) UE4.UKismetSystemLibrary.PrintString(WorldContectObject,"Helloworld") end
/** * Load a primary asset into memory. The completed delegate will go off when the load succeeds or fails, you should cast the Loaded object to verify it is the correct type. * If LoadBundles is specified, those bundles are loaded along with the asset */ UFUNCTION(BlueprintCallable, meta=(BlueprintInternalUseOnly="true", Category = "AssetManager", AutoCreateRefTerm = "LoadBundles", WorldContext = "WorldContextObject")) static UAsyncActionLoadPrimaryAsset* AsyncLoadPrimaryAsset(UObject* WorldContextObject, FPrimaryAssetId PrimaryAsset, const TArray<FName>& LoadBundles);
functionCube_Blueprint_C:ReceiveBeginPlay() local Map = UE4.FPrimaryAssetId("Map:/Game/Test/LoadingMap") local AsyncActionLoadPrimaryAsset = UE4.UAsyncActionLoadPrimaryAsset.AsyncLoadPrimaryAsset(self,Map,nil) AsyncActionLoadPrimaryAsset.Completed:Add(self,Cube_Blueprint_C.ReceiveLoadedMap) AsyncActionLoadPrimaryAsset:Activate() end
functionCube_Blueprint_C:ReceiveLoadedMap(Object) UE4.UGameplayStatics.OpenLevel(self,"/Game/Test/LoadingMap",true) end
functionLoadingMap_C:ReceiveBeginPlay() UpdateUILoopCount = 0; UpdateUITimerHandle = UE4.UKismetSystemLibrary.K2_SetTimerDelegate({self,LoadingMap_C.UpdateUI},0.3,true) end
functionLoadingMap_C:UpdateUI() if UpdateUILoopCount < 10then print("HelloWorld") UpdateUILoopCount = UpdateUILoopCount + 1 else UE4.UKismetSystemLibrary.K2_ClearAndInvalidateTimerHandle(self,UpdateUITimerHandle) end end
-- using 1 functionTimerMgr:UpdateTimer(DeltaTime) local profile_tag = UE4.FProfileTag("TimerMgr_UpdateTimer") -- dosomething... profile_tag:End() end
-- using 2 functionTimerMgr:UpdateTimer(DeltaTime) local profile_tag = UE4.FProfileTag() profile_tag:Begin(("TimerMgr_UpdateTimer")) -- dosomething... profile_tag:End() end
-- using 3 functionTimerMgr:UpdateTimer(DeltaTime) UE4.FProfileTag.StaticBegin(("TimerMgr_UpdateTimer")) -- dosomething... UE4.FProfileTag.StaticEnd() end