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.CPUBrain = {};
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?