Navigation Menu

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

关于静态导出的函数(含非const引用参数)调用方式 #25

Closed
Junho2009 opened this issue Aug 22, 2019 · 2 comments
Closed

Comments

@Junho2009
Copy link

对于含非const引用参数的函数,如:
void GetPlayerBaseInfo(int32& Level, float& Health, FString& Name);

据“Programming_Guide”里面介绍,如果上面的函数是UFunction,则在lua中可以这样调用:
local Level, Health, Name = self:GetPlayerBaseInfo()

但如果以上函数是通过静态导出的,如使用ADD_FUNCTION宏导出的,则不能这样调,而需要:
local Level, Health, Name
Level, Health, Name = self:GetPlayerBaseInfo(Level, Health, Name)
不单参数必须填写(否则报参数个数不对的问题),还需要在返回的地方接住这些引用被修改后的值。

请问有更好的方法吗?

@rowechien
Copy link
Collaborator

静态导出的函数调用时确实有参数个数的检查。还是以这个函数为例,如果这些非const引用都是纯输出,例如:
void GetPlayerBaseInfo(int32 &Level, float &Health, FString &Name)
{
Level = 7;
Health = 77;
Name = "Marcus";
}
这种情况返回值和你传入的值是没有任何关系的,所以你也可以这样写:local Level, Health, Name = self:GetPlayerBaseInfo(0, 0, "")
但是,非const引用参数除了看成输出,也可以把它看成输入,例如:
void GetPlayerBaseInfo(int32 &Level, float &Health, FString &Name)
{
Level += 7;
Health += 77;
Name += "Marcus";
}
这种情况返回值和你的输入是有直接关系的。
所以lua里具体怎样调用是需要看函数具体怎样写的。

@rowechien
Copy link
Collaborator

至于UFUNCTION为何可以不用填入参数,是因为它带有反射信息,参数类型,参数大小等等信息都有,这种情况下就有更灵活的方式。但静态导出的函数,这些信息并没有,或者获取的成本太高,所以限制多一些。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants