Skip to content

Commit

Permalink
增加:UUnLuaLatentAction用于包装异步行为,支持SetTickableWhenPaused
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyanghuang-tencent committed Nov 8, 2021
1 parent 4939b91 commit 33cbc2d
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "LuaContext.h"
#include "LuaFunctionInjection.h"
#include "DefaultParamCollection.h"
#include "UnLua.h"
#include "UUnLuaLatentAction.h"

/**
* Function descriptor constructor
Expand Down Expand Up @@ -419,8 +421,18 @@ void* FFunctionDesc::PreCall(lua_State *L, int32 NumParams, int32 FirstParamInde
Property->InitializeValue(Params);
if (i == LatentPropertyIndex)
{
const int32 ThreadRef = *((int32*)Userdata);
if(lua_type(L, FirstParamIndex + ParamIndex) == LUA_TUSERDATA)
{
// custom latent action info
FLatentActionInfo Info = UnLua::Get<FLatentActionInfo>(L, FirstParamIndex + ParamIndex, UnLua::TType<FLatentActionInfo>());
if(Info.Linkage == UUnLuaLatentAction::MAGIC_LEGACY_LINKAGE)
Info.Linkage = ThreadRef;
Property->CopyValue(Params, &Info);
continue;
}

// bind a callback to the latent function
int32 ThreadRef = *((int32*)Userdata);
FLatentActionInfo LatentActionInfo(ThreadRef, GetTypeHash(FGuid::NewGuid()), TEXT("OnLatentActionCompleted"), (UObject*)GLuaCxt->GetManager());
Property->CopyValue(Params, &LatentActionInfo);
continue;
Expand Down
71 changes: 71 additions & 0 deletions Plugins/UnLua/Source/UnLua/Private/UUnLuaLatentAction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Tencent is pleased to support the open source community by making UnLua available.
//
// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License. You may obtain a copy of the License at
//
// http://opensource.org/licenses/MIT
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and limitations under the License.

#include "UUnLuaLatentAction.h"

#include "LuaContext.h"

void UUnLuaLatentAction::OnCompleted(int32 InLinkage) const
{
if (Callback.IsBound())
Callback.Execute(InLinkage);
}

FLatentActionInfo UUnLuaLatentAction::CreateInfo(const int32 Linkage)
{
return FLatentActionInfo(Linkage, GetTypeHash(FGuid::NewGuid()), TEXT("OnCompleted"), this);
}

FLatentActionInfo UUnLuaLatentAction::CreateInfoForLegacy()
{
Callback.BindDynamic(this, &UUnLuaLatentAction::OnLegacyCallback);
return CreateInfo(MAGIC_LEGACY_LINKAGE);
}

bool UUnLuaLatentAction::GetTickableWhenPaused()
{
return bTickEvenWhenPaused;
}

void UUnLuaLatentAction::SetTickableWhenPaused(bool bTickableWhenPaused)
{
bTickEvenWhenPaused = bTickableWhenPaused;
}

void UUnLuaLatentAction::Tick(float DeltaTime)
{
UWorld* World = GetWorld();
if (World)
{
GetClass()->ClassFlags |= CLASS_CompiledFromBlueprint;
World->GetLatentActionManager().ProcessLatentActions(this, DeltaTime);
GetClass()->ClassFlags &= ~CLASS_CompiledFromBlueprint;
}
}

bool UUnLuaLatentAction::IsTickable() const
{
return bTickEvenWhenPaused;
}

TStatId UUnLuaLatentAction::GetStatId() const
{
return GetStatID();
}

void UUnLuaLatentAction::OnLegacyCallback(int32 InLinkage)
{
Callback.Unbind();
GLuaCxt->ResumeThread(InLinkage);
}
64 changes: 64 additions & 0 deletions Plugins/UnLua/Source/UnLua/Public/UUnLuaLatentAction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Tencent is pleased to support the open source community by making UnLua available.
//
// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License. You may obtain a copy of the License at
//
// http://opensource.org/licenses/MIT
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and limitations under the License.

#pragma once

#include "CoreMinimal.h"
#include "UUnLuaLatentAction.generated.h"

DECLARE_DYNAMIC_DELEGATE_OneParam(FUnLuaLatentActionCallback, int32, InLinkage);

UCLASS()
class UUnLuaLatentAction : public UObject, public FTickableGameObject
{
GENERATED_BODY()

public:
const static int32 MAGIC_LEGACY_LINKAGE = 0x01DC0DE;

UPROPERTY()
FUnLuaLatentActionCallback Callback;

UFUNCTION()
void OnCompleted(int32 InLinkage) const;

UFUNCTION(BlueprintCallable)
FLatentActionInfo CreateInfo(int32 Linkage = 0);

UFUNCTION(BlueprintCallable)
FLatentActionInfo CreateInfoForLegacy();

UFUNCTION(BlueprintCallable)
bool GetTickableWhenPaused();

UFUNCTION(BlueprintCallable)
void SetTickableWhenPaused(bool bTickableWhenPaused);

// Begin Interface FTickableGameObject

virtual void Tick(float DeltaTime) override;

virtual bool IsTickable() const override;

virtual TStatId GetStatId() const override;

// End Interface FTickableGameObject

UFUNCTION()
void OnLegacyCallback(int32 InLinkage);

private:
UPROPERTY()
uint8 bTickEvenWhenPaused:1;
};
6 changes: 6 additions & 0 deletions TPSProject.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Quat/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Tickable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=tostring/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=UCLASS/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=UNLUA/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

1 comment on commit 33cbc2d

@xuyanghuang-tencent
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.