简体   繁体   中英

Lua if statement using function in for loop

I'm tyring to make kind of macros by using scripts of WoW itself. And I heard that the script of WoW is based on 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.)

I'm trying to return boolean value by function for if statement in for loops.

If it was Obj-C , it would be like below.

-(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

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

That's because you have a superfluous do in your code that "steals" the end you provided for the if . So you're short one end per if statement.

Remove do after then .

Lua 5.4 Reference Manual: 3.3.4 Control Structures

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

Aside from that your code is syntactically correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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