PDA

View Full Version : Merging Scripts?


WhiskeyBravo
09-13-2012, 07:28 PM
Hi all,

I have almost zero knowledge of C# scripting but by examining the various examples found around these parts I've been able to analyse them and reverse engineer them somewhat to use them in my missions.

One thing I would like to do is to take parts from different scripts and merge them into a single script. However, my knowledge of the syntax in non-existent.

Can anybody give a brief outline on how to merge two or more scripts please?

And a big thanks to those out there who produce scripts for us, especially the ones who provide well commented code. It can make all the difference for noobs like me.

Thanks again for any help.

WB.

SlipBall
09-13-2012, 08:18 PM
Interesting question...so now we wait :D

hc_wolf
09-14-2012, 05:03 AM
In Short.... Yes you can merge scripts as long as you use just one class and public or private void. so you have to stip out the meat and merge it with a current script.

or you can try and take your 2 scripts and put the data from both inder each public/private void. To get you started here is a basic list that you could use to merge your codes under.



public class Mission : AMission
public class PilotList
private int deathLimit;
private double penaltyforleavetime;
private double penaltyfordeathtime;
public PilotList(int DeathLimit, double PenaltyForDeadTime, double PenaltyForLeaveTime)
public bool CheckPilot(Player player, AiActor actor)
public List<PilotDetails> CurrentPenaltyPlayers()
public void AddDeathToPlayer(Player player)
public int GetNumberOfDeaths(Player player)
public void RemovePilot(Player player)
public void ClearList()
public class PilotDetails
public Player Pilot { get; set; }
public AiActor LastUsedActor { get; set; }
public bool HasLeavedPenalty { get; set; }
public bool HasDeathPenalty { get; set; }
public int NumberOfDeaths { get; set; }
public DateTime endPenaltyTime { get; set; }
public TimeSpan PenaltyRestTime { get; set; }
public bool IsLastActorAirborne()
public override void OnMissionLoaded(int missionNumber)
private void RemoveStationary(int missionNumber)
public override void OnActorTaskCompleted(int missionNumber, string shortName, AiActor actor)
private bool IsActorDestroyable(AiActor actor)
private bool isAiControlledPlane(AiAircraft aircraft)
private int NumberOfPlayers(int Army)
private string SplitName(string tmpstring)
private void checkLanded(AiAircraft aircraft)
private void damageAiControlledPlane(AiActor actorMain)
private void destroyAiControlledPlane(AiAircraft aircraft)
private void destroyGroundActor(AiGroundActorType type, int army)
private void destroyPlane(AiAircraft aircraft)
private void DoDamageToAirplane(AiAircraft aircraft)
private void explodeFuelTank(AiAircraft aircraft)
private void sendChatMessageTo(int army, string msg, object[] parms)
private void sendScreenMessageTo(int army, string msg, object[] parms)
public bool LandedAtEnemyAirfield(AiAircraft aircraft)
public override void Inited()
public override void OnActorCreated(int missionNumber, string shortName, AiActor actor)
public override void OnActorDead(int missionNumber, string shortName, AiActor actor, List<DamagerScore> damages)
public override void OnActorDestroyed(int missionNumber, string shortName, maddox.game.world.AiActor actor)
public override void OnAircraftCrashLanded(int missionNumber, string shortName, AiAircraft aircraft)
public override void OnAircraftDamaged(int missionNumber, string shortName, AiAircraft aircraft, AiDamageInitiator initiator, part.NamedDamageTypes damageType)
public override void OnAircraftLanded(int missionNumber, string shortName, AiAircraft aircraft)
public override void OnAircraftTookOff(int missionNumber, string shortName, AiAircraft aircraft)
public override void OnBattleStarted()
public override void OnBattleStoped()
private void damagePlayerGroup(AiActor actorMain)
public override void OnPlaceEnter(Player player, AiActor actor, int placeIndex)
public override void OnPlaceLeave(Player player, AiActor actor, int placeIndex)
public override void OnPlayerConnected(Player playerDediServer)
public override void OnPlayerDisconnected(Player player, string diagnostic)
public override void OnTickGame()



Sorry I am on way to Airport and do not have time to go into this in great detail. You are asking quite a big question.

salmo
09-14-2012, 06:43 AM
Hi all,

I have almost zero knowledge of C# scripting but by examining the various examples found around these parts I've been able to analyse them and reverse engineer them somewhat to use them in my missions.

One thing I would like to do is to take parts from different scripts and merge them into a single script. However, my knowledge of the syntax in non-existent.

Can anybody give a brief outline on how to merge two or more scripts please?

And a big thanks to those out there who produce scripts for us, especially the ones who provide well commented code. It can make all the difference for noobs like me.

Thanks again for any help.

WB.

Your question raises an issue that I have canvased previously. There is no easy method to use self-contained scripts to perform specific tasks as components of a main mission. Calling different scripts on the fly from your mission using the GamePlay.gpLoadSectionFile or GamePlay.gpPostMissionLoad commands without having to merge different scripts into one main script. You should be able to build script library to perform specific tasks, & then be able to call those scripts from your mission.

bolox
09-14-2012, 07:29 AM
yes it's possible- but it can be frustrating- you really need to learn a little about coding (in my experience- I knew nothing when CoD came out but have had to learn how to cut and paste/write very simple stuff to run campaigns(offline))
http://www.csharp-station.com/Tutorial/CSharp/Lesson01
might be a good start- first 7 chapters will stand you in good stead.

DON'T try doing this with windows notepad- get visual express or at the least notepad++. The ability to see the pairs of brackets is invaluable. This was the thing that really foxed me at first.
The line numbering is also rather useful to see where the error is WHEN you get it wrong. CoD's fmb compiler at lest gives you the line/character where the error lies

Make sure any variables used have been declared- these are the bits usually at top of code
public class Mission : AMission
{
AiAircraft PlayerPlane;
bool end = false;
int lnd = 0;

KEEP TRYING:-P it takes a while to begin to see what you should be doing, you've learned to 'read' the code so you are half way there

WhiskeyBravo
09-14-2012, 10:41 AM
Thank you gentlemen for your replies. I have already installed Visual C# 2010 Express after following Kodiaks excellent tutorials at the top of this forum.

I've also started on the C# tutorials mentioned in the post by bolox above - great find.

I have some experience of Pascal programming from almost 30 years ago!! so the concept of programming is not totally alien to me.

All I need now is twice as many hours in a day so I can learn C# AND mission building in the FMB.

Still, keeps me off the streets! :)

Thanks again,

WB.

WhiskeyBravo
09-14-2012, 11:29 AM
By way of example, how would I combine the following two scripts:

using System;
using maddox.game;
using maddox.game.world;
using System.Collections.Generic;

public class Mission : AMission
{

public override void OnTickGame()
{

// loads the 1st sub-mission in 10 min and repeates it every 60 min.
if (Time.tickCounter() % 108000 == 18000) // 108000 = 60 min repeat. 18000 = 10 min delay.
// pls. note!!! the 1st figure above must be always larger than 2nd!
{
GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/My_mis1/mission1.mis");

// prints message on screen after mission load
GamePlay.gpHUDLogCenter("Hello, world! Mission1.mis loaded!");

// prints message on screen in 10 minutes / 600 seconds
double initTime = 0.0;
Timeout(initTime += 600, () =>
{
GamePlay.gpHUDLogCenter("10 minutes into the 1st mission! Wow! It works!!!");
});

// prints message on screen in 5 minutes / 300 seconds
Timeout(initTime += 300, () =>
{
GamePlay.gpHUDLogCenter("Wholy s.. it works!!!");
});

}

// loads the 2nd sub-mission, etc. the same way
if (Time.tickCounter() % 108000 == 54000) // 108000 = 60 min repeat, 54000 = 30 min delay.
{
GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/My_mis1/mission2.mis");
GamePlay.gpHUDLogCenter("Mission2.mis loaded!");
double initTime = 0.0;
Timeout(initTime += 600, () =>
{
GamePlay.gpHUDLogCenter("Mission2 10 min message!");
});

Timeout(initTime += 300, () =>
{
GamePlay.gpHUDLogCenter("Mission2 15 min message!");
});
}

// loads the 3rd sub-mission
if (Time.tickCounter() % 108000 == 90000) // 60 min repeat, 50 min delay
{
GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/My_mis1/mission3.mis");
GamePlay.gpHUDLogCenter("Mission3.mis loaded!");

double initTime = 0.0;
Timeout(initTime += 600, () =>
{
GamePlay.gpHUDLogCenter("Mission3 10 min message!");
});
Timeout(initTime += 300, () =>
{
GamePlay.gpHUDLogCenter("Now it really works! You are a genius! Have fun!");
});
}
}


}

with

using System;
using maddox.game;
using maddox.game.world;
using System.Collections.Generic;

public class Mission : AMission
{

// destroys aircraft abandoned by a player
private bool isAiControlledPlane (AiAircraft aircraft)
{
if (aircraft == null)
{
return false;
}

Player [] players = GamePlay.gpRemotePlayers ();
foreach (Player p in players)
{
if (p != null && (p.Place () is AiAircraft) && (p.Place () as AiAircraft) == aircraft)
{
return false;
}
}

return true;
}

private void destroyPlane (AiAircraft aircraft) {
if (aircraft != null) {
aircraft.Destroy ();
}
}

private void explodeFuelTank (AiAircraft aircraft)
{
if (aircraft != null)
{
aircraft.hitNamed (part.NamedDamageTypes.FuelTank0Exploded);
}
}

private void destroyAiControlledPlane (AiAircraft aircraft) {
if (isAiControlledPlane (aircraft)) {
destroyPlane (aircraft);
}
}

private void damageAiControlledPlane (AiActor actor) {
if (actor == null || !(actor is AiAircraft)) {
return;
}

AiAircraft aircraft = (actor as AiAircraft);

if (!isAiControlledPlane (aircraft)) {
return;
}

if (aircraft == null) {
return;
}

aircraft.hitNamed (part.NamedDamageTypes.ControlsElevatorDisabled);
aircraft.hitNamed (part.NamedDamageTypes.ControlsAileronsDisabled);
aircraft.hitNamed (part.NamedDamageTypes.ControlsRudderDisabled);
aircraft.hitNamed (part.NamedDamageTypes.FuelPumpFailure);
aircraft.hitNamed (part.NamedDamageTypes.Eng0TotalFailure);
aircraft.hitNamed (part.NamedDamageTypes.Eng1TotalFailure);

/***Timeout (240, () =>
{explodeFuelTank (aircraft);}
);
* ***/

Timeout (300, () =>
{destroyPlane (aircraft);}
);
}

//////////////////////////////////////////////////////////////////////////////////////////////////

public override void OnPlaceLeave (Player player, AiActor actor, int placeIndex)
{
base.OnPlaceLeave (player, actor, placeIndex);
Timeout (1, () =>
{damageAiControlledPlane (actor);}
);
}

public override void OnAircraftCrashLanded (int missionNumber, string shortName, AiAircraft aircraft)
{
base.OnAircraftCrashLanded (missionNumber, shortName, aircraft);
Timeout (300, () =>
{ destroyPlane(aircraft); }
);
}
public override void OnAircraftLanded (int missionNumber, string shortName, AiAircraft aircraft)
{
base.OnAircraftLanded(missionNumber, shortName, aircraft);
Timeout(300, () =>
{ destroyPlane(aircraft); }
);
}


}

Can i just paste the second piece of code (minus the namespace declarations and the first public class declaration) onto the end of the first piece of code?

I think it should work but I'm still a little hazy on class declarations etc at the moment.

Cheers,

WB.

ATAG_Colander
09-14-2012, 04:16 PM
Think of each class as a separate source file.
You can add between the class {}'s any function you want, just as you would on a source code.

So, you can copy the OnTickGame function and paste it inside the other's file "
public class Mission : AMission" and it will work.

Your only worry should be:
a) not have 2 functions with the same name (i.e. 2 OnTickGame's)
b) call the functions that are not called by the game from your code (i.e. isAiControlledPlane )