无法从coroutine内部访问全局表

0

【以下的问题经过翻译处理】 我使用了Lumberyard 1.8.0.1和1.9.0.1进行了测试。 当我在协程中注释掉--System.Log时,总是会出现以下错误。 [Warning] [Script Error] Wrong parameter type. Function System.Log() expect parameter 1 of type String (Provided type String) 似乎我无法访问任何全局表,无论是系统提供的还是自定义的。 这个问题有没有解决方法?

  -- Scripts/main.lua
function OnInit()
local i = 0
System.Log("main.lua:OnInit before " .. i)
local co = coroutine.create(
function(f)
i = i + 1
-- System.Log("main.lua:OnInit in co " .. i) -- Error if comment out
end
)
coroutine.resume( co, co )
System.Log("main.lua:OnInit after " .. i)
end
profile picture
专家
已提问 5 个月前12 查看次数
1 回答
0

【以下的回答经过翻译处理】 Legacy Lua 使用与component entity Lua 系统不同的 Lua 上下文,因此它们无法相互通信。 Lua 脚本系统是在加载关卡之前创建并初始化的,因此您应该能够尽早使用它。

如果您想在加载关卡之前运行 Lua,则需要编写一些 C++ 来创建带有 Lua 组件的实体,或者生成一个动态切片,其中包含带有 Lua 组件的实体(推荐)。 以下是文档页面的链接,其中讨论了在编辑器和 C++ 中创建和生成动态切片 http://docs.aws.amazon.com/lumberyard/latest/developerguide/dynamic-slices.html

我建议使用项目配置器 (http://docs.aws.amazon.com/lumberyard/latest/userguide/gems-system-gems.html) 创建一个 Gem,该配置器具有一个系统组件,可以在其 Activate 方法中执行此操作或 make 您的系统组件有一个 ISystemEventListener,这样如果您的 Lua 逻辑需要旧游戏框架完成设置,您就可以侦听 ESYSTEM_EVENT_GAME_POST_INIT_DONE 系统事件。

profile picture
专家
已回答 5 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则