Data Realms Fan Forums http://45.55.195.193/ |
|
Strange 'if' expression error http://45.55.195.193/viewtopic.php?f=73&t=15130 |
Page 1 of 1 |
Author: | Kyred [ Tue Jun 02, 2009 4:22 am ] |
Post subject: | Strange 'if' expression error |
NOTE: If you want to skip right to the problem, skip over this part and read the stuff after the ** While testing some lua script, I ran into a weird logic error. I was trying to write a script that would iterate though all the actors (via MovableManager.Actors) at the beginning of a mission and add all the brains to the list 'self.CPUBrain'. For whatever reason, MovableManager.Actors came up empty in the StartActivity() function. So I moved the code over to UpdateActivity() instead. To prevent this piece of code from running at every update, I added an if statement which checked to see if the list self.CPUBrain was empty (if # self.CPUBrain == 0). Whenever an enemy brain is killed, it is removed from self.CPUBrain. So, to make sure this code doesn't run again after all the brains are dead, I added the boolean variable self.brainSetup, which will always remain true expect at the very beginning of the game. ** I was testing the outcomes of the 'if' statement and noticed that whenever self.brainSetup was set to 'true' within the execution block, the if statement stopped executing code within the block. The 'for' loop within the statement is completely skipped. When I try this code below, the console only outputs "PRINT: Test" For the record, the values of CPUBrain and brainSetup prior to the if statement are:
self.brainSetup = false; Code: if # self.CPUBrain == 0 and not self.brainSetup then print("Test"); local index = 1; self.brainSetup = true; for actor in MovableMan.Actors do print("Test2"); if actor:HasObjectInGroup("Brains") then print("Brain check passed."); self.CPUBrain[index] = actor; index = index + 1; end end end When I tried moving self.brainSetup = true to the bottom, the same thing happened. Still skipping the 'for' loop. Code: if # self.CPUBrain == 0 and not self.brainSetup then print("Test"); local index = 1; for actor in MovableMan.Actors do print("Test2"); if actor:HasObjectInGroup("Brains") then print("Brain check passed."); self.CPUBrain[index] = actor; index = index + 1; end end self.brainSetup = true; end Yet when I remove self.brainSetup from the code, it works perfectly. Code: if # self.CPUBrain == 0 and not self.brainSetup then print("Test"); local index = 1; for actor in MovableMan.Actors do print("Test2"); if actor:HasObjectInGroup("Brains") then print("Brain check passed."); self.CPUBrain[index] = actor; index = index + 1; end end end So is it normal in lua for an if statement to cut itself off like that when one of its expressions becomes false during the execution block? Or am I missing something else that's going on? |
Author: | Darlos9D [ Tue Jun 02, 2009 5:18 am ] |
Post subject: | Re: Strange 'if' expression error |
Are you sure there's actors around when you test this? And whats with the # sign in your if statement? |
Author: | Kyred [ Tue Jun 02, 2009 5:46 am ] |
Post subject: | Re: Strange 'if' expression error |
Darlos9D wrote: Are you sure there's actors around when you test this? And whats with the # sign in your if statement? Yes, I know there are actors around. And the # sign returns the length of the self.CPUBrain list (ie. how many brains there are). |
Author: | Daman [ Tue Jun 02, 2009 11:31 am ] |
Post subject: | Re: Strange 'if' expression error |
Kyred wrote: Darlos9D wrote: Are you sure there's actors around when you test this? And whats with the # sign in your if statement? Yes, I know there are actors around. And the # sign returns the length of the self.CPUBrain list (ie. how many brains there are). print(# self.CPUBrain) sometime. It'll probably give you an error. It's pretty dumb not to prefix the operator to the variable. Change it to #self.cpubrain. I don't see why you even need brainsetup. Just return if there are no brains. |
Page 1 of 1 | All times are UTC [ DST ] |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |