A very quick & dirty tutorial on how to launch a random sub-mission from inside your main mission file. Firstly, there are many ways to do this, here's one of my methods. I try to make my code 'utilitarian'. ie. I construct it in such a way that it is flexible & can be reused from mission to mission with having to alter too much of the code itself. For example, I have many random mission I want to generate from my main mission. I put the main mis file & the main script file into a directory on their own. Then I create a series of sub-directoriess below the main directory. My red sub-missions will be in a folder called 'red', by blue submission will be in a folder called 'blue' etc. There's no correct way to do this I just recommend a subfolder organisation that makes sense for you.
1. GLOBAL VARIABLES - I like to setup a few global variables at the start of my scripts & reference that global variable inside the script inself. That way I can just change global variable parameter without having to alter any of the remaining script lines. Note the lines in the // global variables section. They define the path to the CLOD missions folders & where in those folders my actual mission is located. Note also that this is a full-path.
2. RANDOMNESS - To start generating random outcomes, you'll need to initialise a 'random' class somewhere at the start of your script. Note the Random random = new Random(); line. This initiates a new random class call "random". We can use this class later in the script to find random missions.
3. ONAIRCRAFT KILLED - We want to generate a random mission when an aircraft is 'killed' (destroyed). So the code to find a random mission to launch has to go inside the OnAircraftKilled game event. We could put this code anywhere inside other game events like OnAircraftTookoff etc, but for this tutorial we use the OnAircraftKilled event. Let's go through that code line by line.
The List<string> .... line looks in a sub-directory called 'red' & makes a list of all mission files in that sub-directory. Note how I use a function called GetFilenamesFromDirectory that can recieve a mission filename 'mask'. That way I can have (say) fighter & bomber missions in the same red subdirectory but only make a list of (in this case) just the missions with the word 'bomber' in the mission name. So my bomber missions will be named 'bomber1.mis, bomber2,mis etc.
The string RandomMission = .... line will pick one mission from the list of missions. Note this string will be the FULL-PATH to that mission.
The GamePlay.gpPostMissionLoad .... line will load that submission into the game.
Code:using System; using System.Collections; using maddox.game; using maddox.game.world; using part; using System.Collections.Generic; using System.IO; public class Mission : AMission { // global variables private static string userdocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // DO NOT CHANGE private static string CLOD_PATH = userdocpath + @"/1C SoftClub/il-2 sturmovik cliffs of dover - MOD/"; // DO NOT CHANGE private static string FILE_PATH = @"missions/Multi/Dogfight/OperationHomePlateV3.0/"; // mission install directory (CHANGE AS NEEDED) Random random = new Random(); public override void OnAircraftKilled(int missionNumber, string shortName, AiAircraft aircraft) { base.OnAircraftKilled(missionNumber, shortName, aircraft); List<string> RandomMissions = GetFilenamesFromDirectory(CLOD_PATH + FILE_PATH + "/red", "bomber"); // bomber missions from the red directory if (RandomMissions.Count > 0) { string RandomMission = RandomMissions[random.Next(RandomMissions.Count)]; GamePlay.gpPostMissionLoad(RandomMission); } } public List<string> GetFilenamesFromDirectory(string dirPath, string mask = null) { List<string> list = new List<string>(); string[] filenames = Directory.GetFiles(dirPath, "*" + mask + "*.mis"); list = new List<string>(filenames); return list; } }
Bookmarks