简体   繁体   English

Lua if 语句在 for 循环中使用 function

[英]Lua if statement using function in for loop

I'm tyring to make kind of macros by using scripts of WoW itself.我很想通过使用 WoW 本身的脚本来制作宏。 And I heard that the script of WoW is based on Lua.而且听说魔兽的脚本是基于Lua的。

Though I've used Obj-C,Swift and C++ for several years, Embarrassingly it was quite hard to write down some code with Lua.(eventhough it was quite short.)虽然我已经使用 Obj-C、Swift 和 C++ 好几年了,但令人尴尬的是,用 Lua 写一些代码非常困难。(尽管它很短。)

I'm trying to return boolean value by function for if statement in for loops.我正在尝试通过 function 为 for 循环中的 if 语句返回 boolean 值。

If it was Obj-C , it would be like below.如果是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];
}

And A Lua code I tried is below我试过的 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

What did i miss?我错过了什么? and How Should it be?应该如何? I hope someone could help me from this difficulty...我希望有人能帮助我摆脱这个困难......

You're probably seeing an error message like您可能会看到一条错误消息,例如

'end' expected (to close 'if' at line...) near 'end' 预期(关闭 'if' 在行...)附近

That's because you have a superfluous do in your code that "steals" the end you provided for the if .那是因为您的代码中有一个多余的do “窃取”了您为if提供的end So you're short one end per if statement.因此,您对每个end语句都很短。

Remove do after then . then删除do

Lua 5.4 Reference Manual: 3.3.4 Control Structures Lua 5.4 参考手册:3.3.4 控制结构

stat::= if exp then block { elseif exp then block} [ else block] end stat::= if exp then block { elseif exp then block} [ else block] end

Aside from that your code is syntactically correct.除此之外,您的代码在语法上是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM