LUA 以错误的顺序执行代码 - 可能与桌面模拟器相关
我对 LUA 还很陌生,仍在学习中。到目前为止,我还没有遇到任何异步功能,但我的代码似乎是这样表现的。实在是太奇怪了。我有以下函数:
function chgAttacker(attacker)
local stats = getModelstats(attacker)
Attacker = stats
local WS = stripTextFormatting(stats[2])
local BS = stripTextFormatting(stats[3])
local Attack = stats[7]
local weaponStrength, armorPen = getWeapons(attacker)
UI.setAttribute("model_BS", "text", BS) -- set BS skill on UI
UI.setAttribute("model_WS", "text", WS) -- set WS skill on UI
UI.setAttribute("model_attacks", "text", Attack) -- set attacks skill on UI
end
如果我删除包含 getWeapons(attacker)
的行(我将在接下来解释),该函数将按照您的预期工作。它非常基本,它只是获取一个字符串,解析它然后将其存储在 stats
变量中。最后 3 UI
行然后根据这些统计数据更新我的 UI 中的 3 个值。当我说这非常有效时,你必须相信我。
问题开始的地方是,当我在上面添加 getWeapons(attacker)
时,它还更新这些 UI
值(实际上,它会将它们重置为某些默认值)。但是,最后 3 行显然意味着覆盖任何 getWeapons(attacker)
设置。但事实并非如此。假设起始 UI BS
值为 1,getweapons()
将其更新为 2,然后 UI.setAttribute("model_BS", "text", BS )
将其更新为 3;这正是我所观察到的:它从 1 开始,然后(非常简单地)更新为 3,然后更新为 2。
就好像它以异步方式运行一样。有没有人以前见过这个或者可以建议我如何让 LUA 等待直到 getweapons()
函数完成?
编辑:我自己修复了它,我正在使用内置等待函数......但看起来,它实际上并没有等待。我删除了这个,现在它可以工作了。真的很奇怪,因为我必须首先添加它,因为 UI 更新得不够快。
I am fairly new to LUA and still learning. So far I haven't came across any async features, but my code seems to be behaving this way. It is really bizarre. I have the following function:
function chgAttacker(attacker)
local stats = getModelstats(attacker)
Attacker = stats
local WS = stripTextFormatting(stats[2])
local BS = stripTextFormatting(stats[3])
local Attack = stats[7]
local weaponStrength, armorPen = getWeapons(attacker)
UI.setAttribute("model_BS", "text", BS) -- set BS skill on UI
UI.setAttribute("model_WS", "text", WS) -- set WS skill on UI
UI.setAttribute("model_attacks", "text", Attack) -- set attacks skill on UI
end
The function works as you might expect if I remove the line which has getWeapons(attacker)
(which I'll explain next). It is very basic, it just gets a string, parses it then stores it in the stats
variable. The last 3 UI
lines then update 3 values in my UI based those stats. You'll have to believe me when I say, that this works perfectly.
Where the problem starts is when I add the getWeapons(attacker)
above, it also updates these UI
values (well it actually resets them to some defaults). But, the last 3 lines are then obviously meant to overwrite whatever getWeapons(attacker)
set. But it doesn't. Let's say the starting UI BS
value is 1, and getweapons()
updates it to 2, then UI.setAttribute("model_BS", "text", BS)
updates it to 3; here is exactly what I observe: it starts as 1, then (very briefly) updates to 3, then updates to 2.
It is as if it is behaving in an async manner. Has anyone seen this before or can advise how I can make LUA wait until the getweapons()
function finishes?
EDIT: Just fixed it myself, I was using the builtin wait function...But it seems, it doesn't actually wait. I removed this and now it works. Really strange because I had to add this in the first place because the UI couldn't update quick enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是我在
tagetweapons()
中使用了名为wait
的桌面模拟器函数(未显示)。下面是如何使用它的示例:由于其他原因,我将其设置为 2 个刻度 - 更新 UI 时至少需要一个刻度,因此我需要上述等待。然而,这会产生负面影响,使其表现得像异步调用。消除等待后,它按预期工作。
The issue was that I was using a Tabletop Simulator function called
wait
insidetagetweapons()
(not shown). Example of how to use it below:I had this set for 2 ticks for other reasons - when updating the UI it takes at least one tick, so I needed the above wait. However, this had the adverse affect of making it behaving like an async call. After removing the waits it worked as expected.