Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c++定义USTRUCT给lua使用,在C++中通过RPC调用lua代码时报错 #589

Closed
yfcyt opened this issue Mar 26, 2024 · 4 comments
Closed
Labels
bug Something isn't working

Comments

@yfcyt
Copy link

yfcyt commented Mar 26, 2024

报错日志:
LogProperty: Error: Struct type unknown for property 'StructProperty /Engine/Transient.Struct_0:TransientLuaProperty'; perhaps the USTRUCT() was renamed or deleted?

我写的代码:
image

image

image

image

这里通过const TArray& 传递PlayerDatas到lua,会提示上面的报错

请问下,这个正确的用法是怎么样的?

@yfcyt yfcyt changed the title c++定义USTRUCT给lua使用,在C++中通过RPC,调用lua代码时报错 c++定义USTRUCT给lua使用,在C++中通过RPC调用lua代码时报错 Mar 26, 2024
@zjhongxian
Copy link
Collaborator

image
是这里报错的?可有堆栈和相关变量信息?

@yfcyt
Copy link
Author

yfcyt commented Mar 27, 2024

image 是这里报错的?可有堆栈和相关变量信息?

谢谢大佬的回复!
是大佬截图的地方报的错,堆栈信息涉及一些商业隐私,不太方便截上来,我完整的描述一下我的情景:

我用的版本是:sluaunreal-2.1.2

我想要定义一个UStruct结构体FPlayerData,从C++传递(const TArray&)到Lua中,想在lua中访问从C++传递过来的FPlayerData数组

1.定义FPlayerData
USTRUCT(BlueprintType)
struct FPlayerData
{
GENERATED_BODY()

public:

UPROPERTY()
FString PlayerName;
UPROPERTY()
FString PlayerId;

};

namespace NS_SLUA
{
DefTypeName(FPlayerData);
DefDeduceType(FPlayerData, Struct);
}

2.定义一个PlayerController,提供一个服务端调用客户端执行的RPC函数
UCLASS()
class UNREALPROJ_API AXXXPlayerController : public APlayerController
{

    //调用Lua代码
    template<class ...ARGS>
    void CallLuaFunc(const char* FuncName, ARGS&& ...args);

   //将数据同步到客户端
    UFUNCTION(Client, Reliable, Category = "XXXX")
    void ClientUpdatePlayer(const TArray<FPlayerData>& PlayerDatas);

}

void AXXXPlayerController::ClientUpdatePlayer_Implementation(const TArray& PlayerDatas)
{
CallLuaFunc("UpdatePlayer", PlayerDatas);
}

template<class ...ARGS>
void AXXXPlayerController::CallLuaFunc(const char* FuncName, ARGS && ...args)
{
auto LuaWidget = Cast(HUDWidget);
if (LuaWidget)
{
auto Self = LuaWidget->GetSelfTable();
if (Self.isValid()) Self.callField(FuncName, Self, std::forward(args)...);
}
}

代码调用过程:

在Server端通过ClientUpdatePlayer将const TArray& PlayerDatas传递到ClientClientUpdatePlayer_Implementation

最终将PlayerDatas通过callField方法传递到了Lua的UpdatePlayer方法中

发现在Lua中无法使用PlayerDatas数据,并且报上面的错误日志

请问一下,应该如何正确的传递自定义的USTRUCT并通过const TArray& 传递到lua层?

@zjhongxian
Copy link
Collaborator

OK,我明天抽空构造一下现场看看

@zjhongxian zjhongxian added the bug Something isn't working label Mar 29, 2024
@yfcyt
Copy link
Author

yfcyt commented Apr 1, 2024

OK,我明天抽空构造一下现场看看

好的,谢谢大佬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants