Lua if语句在for循环中使用函数
Lua if statement using function in for loop
我正在尝试使用 WoW 本身的脚本制作一些宏。
而且听说wow的脚本是根据Lua.
虽然我已经使用 Obj-C、Swift 和 C++ 多年,但令人尴尬的是,用 Lua 写下一些代码非常困难。(尽管它很短。)
我正在尝试 return 通过函数为 for 循环中的 if 语句计算布尔值。
如果是Obj-C,就会像下面这样。
-(void) script {
Bool on = NO;
NSArray* zone = [@"Feralas",@"The hinterlands",@"Duskwood"];
for(int i = 0 ; i < 3 ; i ++) {
if([self clearCheck:i]) {
NSLog(@"stone on! -> %@",zone[i]);
on = YES;
}
}
if (!on) {
NSLog(@"No Stone Today..");
}
}
-(Bool) clearCheck:(int)index {
return [C_QuestLog IsQuestFlaggedCompleted:index+44329];
}
下面是我试过的 Lua 代码
local o,z=false,{"Feralas","The hinterlands","Duskwood"}
function f(x) return C_QuestLog.IsQuestFlaggedCompleted(44329+x) end
for i=1,3 do
if f(i) then do
print("stone on! -> ",z[i])
o=true
end
end
if not o then do
print("no stone today..")
end
我错过了什么?应该如何?
我希望有人能帮助我解决这个困难...
您可能会看到类似
的错误消息
'end' expected (to close 'if' at line ...) near
那是因为您的代码中有一个多余的 do
,它“窃取”了您为 if
提供的 end
。所以你每个 if 语句都少了一个 end
。
删除 then
之后的 do
。
Lua 5.4 Reference Manual: 3.3.4 Control Structures
stat ::= if exp then block {elseif exp then block} [else block] end
除此之外,您的代码在语法上是正确的。
我正在尝试使用 WoW 本身的脚本制作一些宏。 而且听说wow的脚本是根据Lua.
虽然我已经使用 Obj-C、Swift 和 C++ 多年,但令人尴尬的是,用 Lua 写下一些代码非常困难。(尽管它很短。)
我正在尝试 return 通过函数为 for 循环中的 if 语句计算布尔值。
如果是Obj-C,就会像下面这样。
-(void) script {
Bool on = NO;
NSArray* zone = [@"Feralas",@"The hinterlands",@"Duskwood"];
for(int i = 0 ; i < 3 ; i ++) {
if([self clearCheck:i]) {
NSLog(@"stone on! -> %@",zone[i]);
on = YES;
}
}
if (!on) {
NSLog(@"No Stone Today..");
}
}
-(Bool) clearCheck:(int)index {
return [C_QuestLog IsQuestFlaggedCompleted:index+44329];
}
下面是我试过的 Lua 代码
local o,z=false,{"Feralas","The hinterlands","Duskwood"}
function f(x) return C_QuestLog.IsQuestFlaggedCompleted(44329+x) end
for i=1,3 do
if f(i) then do
print("stone on! -> ",z[i])
o=true
end
end
if not o then do
print("no stone today..")
end
我错过了什么?应该如何? 我希望有人能帮助我解决这个困难...
您可能会看到类似
的错误消息'end' expected (to close 'if' at line ...) near
那是因为您的代码中有一个多余的 do
,它“窃取”了您为 if
提供的 end
。所以你每个 if 语句都少了一个 end
。
删除 then
之后的 do
。
Lua 5.4 Reference Manual: 3.3.4 Control Structures
stat ::= if exp then block {elseif exp then block} [else block] end
除此之外,您的代码在语法上是正确的。