View unanswered posts | View active topics It is currently Tue Apr 23, 2024 12:30 pm



Reply to topic  [ 4 posts ] 
 Strange 'if' expression error 
Author Message
User avatar

Joined: Sun May 31, 2009 1:04 am
Posts: 308
Reply with quote
Post 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?


Tue Jun 02, 2009 4:22 am
Profile
User avatar

Joined: Mon Jul 16, 2007 9:50 am
Posts: 1512
Location: Tallahassee, FL
Reply with quote
Post 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?


Tue Jun 02, 2009 5:18 am
Profile YIM
User avatar

Joined: Sun May 31, 2009 1:04 am
Posts: 308
Reply with quote
Post 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).


Tue Jun 02, 2009 5:46 am
Profile
User avatar

Joined: Fri Jan 26, 2007 3:22 am
Posts: 1451
Reply with quote
Post 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.


Tue Jun 02, 2009 11:31 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.072s | 15 Queries | GZIP : Off ]