Fulqrum Publishing Home   |   Register   |   Today Posts   |   Members   |   UserCP   |   Calendar   |   Search   |   FAQ

Go Back   Official Fulqrum Publishing forum > Fulqrum Publishing > IL-2 Sturmovik: Cliffs of Dover > FMB, Mission & Campaign builder Discussions

Reply
 
Thread Tools Display Modes
  #1  
Old 12-18-2011, 01:09 AM
41Sqn_Banks 41Sqn_Banks is offline
Approved Member
 
Join Date: Oct 2007
Posts: 644
Default Destroy all aircaft

Edit: Even better code to destroy all aircraft
Code:
if (GamePlay.gpArmies() != null && GamePlay.gpArmies().Length > 0)
{
    foreach (int armyIndex in GamePlay.gpArmies())
    {
        if (GamePlay.gpAirGroups(armyIndex) != null && GamePlay.gpAirGroups(armyIndex).Length > 0)
        {
            foreach (AiAirGroup airGroup in GamePlay.gpAirGroups(armyIndex))
            {
                if (airGroup.GetItems() != null && airGroup.GetItems().Length > 0)
                {
                    foreach (AiActor actor in airGroup.GetItems())
                    {
                        if (actor is AiAircraft)
                        {
                            AiAircraft aircraft = actor as AiAircraft;
                            aircraft.Destroy();
                        }
                    }
                }
            }
        }
    }
}

Last edited by 41Sqn_Banks; 12-18-2011 at 12:43 PM.
Reply With Quote
  #2  
Old 12-18-2011, 03:47 AM
Octocat Octocat is offline
Approved Member
 
Join Date: Dec 2011
Posts: 22
Default

The problem is likely not in aircrafts, but in the side actors such as smoke, debris and craters from bombs, which creates the game itself, and does not remove properly.

My variant of function:

Code:
private void DestroyAircrafts(IGamePlay game)
{
  foreach (int army in game.gpArmies())
    foreach (var group in game.gpAirGroups(army))
      foreach (var item in group.GetItems())
        ((AiAircraft)item).Destroy();
}

Last edited by Octocat; 12-18-2011 at 04:18 AM.
Reply With Quote
  #3  
Old 12-18-2011, 04:15 AM
Octocat Octocat is offline
Approved Member
 
Join Date: Dec 2011
Posts: 22
Default

Also, small remark:

Generally, it is better to use explicit conversion, because in case Foo foo = something as Foo; if something is not Foo, operator "as" return null reference, and in the file log.txt you can get uninformative message "Object reference not set to an instance of an object."

If you use explicit cast Foo foo = (Foo)something; if something is not Foo, cast operator throw an exception immediately, and in the log.txt you get more informative "Unable to cast object of type 'Bar' to 'Foo'".


I also want to say thank you for the source of DCE. Nice work.

Last edited by Octocat; 12-18-2011 at 04:20 AM.
Reply With Quote
  #4  
Old 12-18-2011, 04:40 AM
FG28_Kodiak FG28_Kodiak is offline
Approved Member
 
Join Date: Dec 2009
Location: Swabia->Bavaria->Germany
Posts: 884
Default

Quote:
Originally Posted by 41Sqn_Banks View Post
However it doesn't work very reliable. I'm getting some "not destroyed actor" entries in the log. Does anyone know a more reliable way to remove all aircrafts?
If the entry in log looks like
NOT Destroyed actor: e40nRumWubHfA3gPCiI.h7LahLmVcZuiZk94t4i
it has nothing to do with the airplanes, it's a bug since one of the latest patches. The number of 'not destroyed actors' varies on the map in use, channel has 9 not destroyed actors others have 6 or 13. It is cumulative, after each mission the number grows (until restart the game), so hope for the next patch.

Last edited by FG28_Kodiak; 12-18-2011 at 05:37 AM.
Reply With Quote
  #5  
Old 12-18-2011, 08:43 AM
41Sqn_Banks 41Sqn_Banks is offline
Approved Member
 
Join Date: Oct 2007
Posts: 644
Default

Quote:
The problem is likely not in aircrafts, but in the side actors such as smoke, debris and craters from bombs, which creates the game itself, and does not remove properly.
The problem is that the not destroyed aircraft just sit on an airfield with empty tanks and guns and didn't move at all

Quote:
Originally Posted by Octocat View Post
Also, small remark:

Generally, it is better to use explicit conversion, because in case Foo foo = something as Foo; if something is not Foo, operator "as" return null reference, and in the file log.txt you can get uninformative message "Object reference not set to an instance of an object."

If you use explicit cast Foo foo = (Foo)something; if something is not Foo, cast operator throw an exception immediately, and in the log.txt you get more informative "Unable to cast object of type 'Bar' to 'Foo'".


I also want to say thank you for the source of DCE. Nice work.
That's why there is a "if(something is Foo)" before the cast to prevent any exception But I agree if I'd forget the check the cast exception gives better feedback.

As a note to your example:

Code:
private void DestroyAircrafts(IGamePlay game)
{
  foreach (int army in game.gpArmies())
    foreach (var group in game.gpAirGroups(army))
      foreach (var item in group.GetItems())
        ((AiAircraft)item).Destroy();
}
The functions of IGamePlay that return an array of something do not return an empty array but return null if there is nothing to return. In this case your code will throw a reference null exception. It's better to check the return value before using them in the foreach loop, e.g.:

Code:
if (GamePlay.gpAirGroups(army) != null && GamePlay.gpAirGroups(army).Length > 0)

About the initial problem: Looks like I have to look a bit closer to see what causes this.
Reply With Quote
  #6  
Old 12-18-2011, 08:48 AM
41Sqn_Banks 41Sqn_Banks is offline
Approved Member
 
Join Date: Oct 2007
Posts: 644
Default

Oh wait I had an idea:

If I destroyed the Aircraft this should change the content of the getItems() array. This will confuse the foreach loop and it will skip some aircraft.
Obviously the aircrafts are destroyed with a small delay, that's why it is working for some aircraft and some not.

Solution: Store the aircraft to destroy in a seperate list first. Then loop over this list and destroy the aircrafts in it. This way even if the content of getItems() is changed, the content of the list stays the same

I'll check this and report back.
Reply With Quote
  #7  
Old 12-18-2011, 09:29 AM
41Sqn_Banks 41Sqn_Banks is offline
Approved Member
 
Join Date: Oct 2007
Posts: 644
Default

Quote:
Originally Posted by 41Sqn_Banks View Post
Oh wait I had an idea:

If I destroyed the Aircraft this should change the content of the getItems() array. This will confuse the foreach loop and it will skip some aircraft.
Obviously the aircrafts are destroyed with a small delay, that's why it is working for some aircraft and some not.

Solution: Store the aircraft to destroy in a seperate list first. Then loop over this list and destroy the aircrafts in it. This way even if the content of getItems() is changed, the content of the list stays the same

I'll check this and report back.
This was the solution. I updated the code in the first post. I think this topic is something for Ataros scripting collection
Reply With Quote
  #8  
Old 12-18-2011, 11:39 AM
Octocat Octocat is offline
Approved Member
 
Join Date: Dec 2011
Posts: 22
Default

I am absolutely sure that group.GetItems() does not return a direct reference to an internal list of aircraft, but makes a copy. So the trick with the list is not needed.
Reply With Quote
  #9  
Old 12-18-2011, 12:42 PM
41Sqn_Banks 41Sqn_Banks is offline
Approved Member
 
Join Date: Oct 2007
Posts: 644
Default

Yes it returns a copy. However as I used a "for" loop I got the updated copy of the list for every iteration of the loop. Hence the list has changed for every iteration and the indices didn't match anymore to the updated list.

I changed the code to use a "foreach" loop. It's now the same code as yours, only added checks against null references.

Last edited by 41Sqn_Banks; 12-18-2011 at 12:45 PM.
Reply With Quote
  #10  
Old 12-18-2011, 03:01 PM
KG26_Alpha KG26_Alpha is offline
Approved Member
 
Join Date: Jan 2008
Location: London
Posts: 2,805
Default

Ok

Whilst its nice of you to be putting these scripts up for general use...........

Quote:
Originally Posted by 41Sqn_Banks View Post
Edit: Even better code to destroy all aircraft
Perhaps for the sake of those that don't understand why you would want to have to use such a script,
you can in future put an explanation of the scripts use and reason of its need.

I understand clearing old aircraft frees up the missions resources and clears old planes from airfields etc etc.

Not everyone will know that though.

Thxz
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:41 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright © 2007 Fulqrum Publishing. All rights reserved.