PDA

View Full Version : Script "best practice" organisation and questions



SoW Reddog
Oct-15-2013, 04:56
As I get more into the coding behind my mission, I'm finding that I struggle to organise my code. Is there a "best practice" to have say all declarations at the top, all overridden functions, then all custom functions? Is it hobson's choice?

Also, is it possible to load code from a secondary file? I'm presuming it must be but I haven't figured out how although I've only had a cursory look. I don't mean from a submission, but a completely separate cs?

Lastly, when a submission loads in, do any AI aircraft in it now "show" in the main mission, ie are they referable from the main mission .cs? Do they appear in AllAIGroups (pseudocode function obviously!) for example?

No.401_Wolverine
Oct-15-2013, 16:44
First question: I think in a lot of situations when people add code brought together from different sources, it gets all jumbled up simply because it gets added one after the other. Mostly, I believe you want to make your declarations and value definitions as early as you can and all sub-routines below that if possible. I'm not exactly an authority on that though, so someone else please correct me.

Second question: I believe you can. As an example, I would suggest looking for FG26_Kodiak's large volume of tutorials on trigger missions (one of which I believe handles importing script into one mission from another mission's .cs file which is essentially I think what you're looking at). I know that Moggel's RDF system is done in separate .dll files from the .cs file for the mission as well.

Third question: Absolutely yes. If you use your new ATAG mission as an example, if you load in other .mis raid files, those aircraft will be picked up by the RDF system in the main map's .cs file. So the new actors imported from the second .mis get picked up by the .cs code from the first one. However, if you are trying to add code into the script that references these objects directly (ie, doesn't call a request to locate objects as the RDF does - it scans the current running game for any objects of that type, it doesn't need to refer to them by name) then I'm not sure if that will work. You'd have to define those objects ahead of time I think - Kodiak's tutorial may hint at the way this works. I haven't seen it in a while.

Salmo
Oct-15-2013, 19:41
As I get more into the coding behind my mission, I'm finding that I struggle to organise my code. Is there a "best practice" to have say all declarations at the top, all overridden functions, then all custom functions? Is it hobson's choice?
There is no 'standard'. Most codrs put their declarations at the beginning of the script, after that it's hobsons choice. I try to add plenty of comments to the code which helps focus my thoughts on what's happening & where.


Also, is it possible to load code from a secondary file? I'm presuming it must be but I haven't figured out how although I've only had a cursory look. I don't mean from a submission, but a completely separate cs?
Yes, it's possible to load code by using postmissionload to load a 'blank' sub-mission. The code from the sub-mission will run, but it's problematic. For example, HUD message, voices, and several other aspects of the code don't seem to work when run from a submission. For this reason, most people put all their code into the main mission (the mission that starts the battle).


Lastly, when a submission loads in, do any AI aircraft in it now "show" in the main mission, ie are they referable from the main mission .cs? Do they appear in AllAIGroups (pseudocode function obviously!) for example?
Yes.

SoW Reddog
Oct-16-2013, 04:57
Thanks guys.

How does AiAirGroups work? If I loaded the exact same submission (1 flight of 9 bombers) 3 times in a row, would the AiAirGroup for those bombers be 27 aircraft large, or would there be 3 airgroups?

What I'm trying to get to is whether it'd be possible to trigger a message to Blue players when bombers turn for home. Since I'm going to be using random spawns, targets and flightpaths, I can't use a timeout system to trigger the message at the right time. What I'm thinking is just checking for when the AiAirgroup heading changes to be Southerly and then fire the message.

I'm not at home at the moment to try it out.

Salmo
Oct-16-2013, 16:19
<snip>How does AiAirGroups work? If I loaded the exact same submission (1 flight of 9 bombers) 3 times in a row, would the AiAirGroup for those bombers be 27 aircraft large, or would there be 3 airgroups?<snip>
No. Same airgroup loaded 3 times fromm 3 submission will be treated as 3 seperate airgroups. Try Console.Write(airgroup.Name()) & you'll see they all have diferent names starting with different mission number.


What I'm trying to get to is whether it'd be possible to trigger a message to Blue players when bombers turn for home.
Two thoughts here ...

1. Maybe use OnTick method to record in a C# List AiAirgroups that have bombs, when a stored airgroup no longer has bombs they are assumed to be heading home. (See sample code below)

2. Maybe use the OnTick method & a C# dictionary to store the airgroup's distance from home, then when the distance is less than the last stored entry they are assumed to be heading home?



// pseudo code syntax may not be right ....
List<aiairgroup> AiWithBombs = new List<aiairgroup();

public override void OnTick
{
if (Tick.Count > (32 * 60) /pseudocode for every 1 minute
{
for each (AiAirgroup ag in GamePlay.Airgroups())
{
if (AiWithBombs.Contains(ag))
{
if (!ag.HasBombs) // they've dropped their bombs
{
// your custom code here
}
}
else
{
if (ag.hasBombs) AiWithBombs.Add(ag);
}
}

}
}

SoW Reddog
Oct-17-2013, 07:26
Good ideas Salmo. I was thinking adding the airgroup to list on spawn, checking it's heading every minute or so, as soon as it's say 135-315 then they're heading home, trigger the message, remove from list?

Will have to look at the method of this
you'll see they all have diferent names starting with different mission number. and how easy it is to parse what it will be. Presumably because of this, if a player happened to choose the same regiment etc and spawn in, then they'd have an airgroup completely separate? I'd quite like to have the correct codes for the right aircraft but that is probably semantics.

Salmo
Oct-17-2013, 20:42
I was thinking adding the airgroup to list on spawn, checking it's heading every minute or so, as soon as it's say 135-315 then they're heading home, trigger the message, remove from list?
Probably not the most efficient way to achieve your outcome. In any event, you'll find that the I_Compass method that reads the compass heading is broken, so you'd have to get the aircraft heading by a much more round-about methodology.


Will have to look at the method of this and how easy it is to parse what it will be.
Something like ...
string[] part = AiAirgroup.Name().Split(':');
int MisNumber = Convert.ToInt32(part[0]);


Presumably because of this, if a player happened to choose the same regiment etc and spawn in, then they'd have an airgroup completely separate?
Correct. The game assigned a 'unique' key to each airgroup by combining the mission number with the airgroupID.