PDA

View Full Version : Triggers not working with scripts


moggel
10-31-2012, 10:48 AM
Hi everyone, long time and so on ...

I've been away for some time doing other things but I'm currently in testing mode for a new mod I call "Fighter Command". But as I created missions that rely on (timed) triggers they simply doesn't work. At first I suspected my code but it seems triggers simply doesn't wotk at all for missions that supports a script (.cs) file.

I created this litte file:


using System;
using maddox.GP;
using maddox.game;
using maddox.game.world;
using part;

public class Mission : AMission
{
public override void OnTrigger(int missionNumber, string shortName, bool active)
{
base.OnTrigger(missionNumber, shortName, active);
GamePlay.gpLogServer(null, "Trigger '" + shortName + "' = " + active, null);
}
}

The lines above does confirm that the triggers are honored by the sim but nothing gets spawned! I then tried to remove the .cs file and then it works fine.

Has anyone else seen this?

bolox
10-31-2012, 11:07 AM
triggers do work with scripts, if there is an action- ie a spawn associated you must also make it happen in script.

I use something like this
if ("trigger01".Equals(shortName) && active) // triggers 115 spawn
{
AiAction action = GamePlay.gpGetAction("trigger01");
if (action != null)
{
action.Do();
}
//GamePlay.gpHUDLogCenter("115");
GamePlay.gpGetTrigger(shortName).Enable = false;
}

remove 2 // on hudlog line and you'll get an on screen confirmation spawn has happened

moggel
10-31-2012, 11:43 AM
I assumed triggers where managed automatically by the sim and that I could override the default behavior by overriding. But you're saying I need to actively trigger the action from within my script?

FG28_Kodiak
10-31-2012, 12:01 PM
the orginal OnTrigger is defined in the Battle-Script:

public virtual void OnTrigger(int missionNumber, string shortName, bool active)
{
if (this.missions.Count > 0)
{
foreach (AMission mission in this.missions)
{
if (mission.IsMissionListener(missionNumber))
{
mission.OnTrigger(missionNumber, shortName, active);
}
}
}
else if (active)
{
AiAction action = this.GamePlay.gpGetAction(ActorName.Full(missionNu mber, shortName));
if (action != null)
{
action.Do();
}
}
}


If you override the OnTrigger - Method, you create a empty new method so the ability to spawn Airgroups etc is lost, this is nessesary to avoid side-effects.
So to get the old behavior you must insert code to spawn the Actors again for example with:

public override void OnTrigger(int missionNumber, string shortName, bool active)
{
base.OnTrigger(missionNumber, shortName, active);


AiAction action = GamePlay.gpGetAction(ActorName.Full(missionNumber, shortName));
if (action != null)
{
action.Do();
}
}

bolox
10-31-2012, 12:04 PM
correct - it would appear that using a script 'overrides' any trigger actions, therefore you must make the action happen in script.
Took me a week to figure this one out when starting writing campaign scripts:confused:

Assumption is the mother of all 'foul' ups;)

moggel
10-31-2012, 01:07 PM
the orginal OnTrigger is defined in the Battle-Script:

public virtual void OnTrigger(int missionNumber, string shortName, bool active)
{
if (this.missions.Count > 0)
{
foreach (AMission mission in this.missions)
{
if (mission.IsMissionListener(missionNumber))
{
mission.OnTrigger(missionNumber, shortName, active);
}
}
}
else if (active)
{
AiAction action = this.GamePlay.gpGetAction(ActorName.Full(missionNu mber, shortName));
if (action != null)
{
action.Do();
}
}
}


If you override the OnTrigger - Method, you create a empty new method so the ability to spawn Airgroups etc is lost, this is nessesary to avoid side-effects.
So to get the old behavior you must insert code to spawn the Actors again for example with:

public override void OnTrigger(int missionNumber, string shortName, bool active)
{
base.OnTrigger(missionNumber, shortName, active);


AiAction action = GamePlay.gpGetAction(ActorName.Full(missionNumber, shortName));
if (action != null)
{
action.Do();
}
}


Let's see if I understand you correctly Kodiak...

First of all, you mention "the battle script" and even present the OnTrigger virtual method. Is the AMission class source code available somewhere?! If so, where can I find it?

You then say...

If you override the OnTrigger - Method, you create a empty new method so the ability to spawn Airgroups etc is lost, this is nessesary to avoid side-effects.


If I override the OnTrigger i can add base.OnTrigger(...), which of course would be quite useless, but my experiments indicated that overriding OnTrigger is mandatory, like bolox said, to allow actions to actually be triggered, right?
In your override you check for 'missions' (if (this.missions.Count > 0)). Are those "sub missions" loaded by "this" mission?

You then iterate those missions and calls their OnTrigger(), but only if they are "mission listeners". What is that?

Then, if there's no (sub?) missions you move ahead and actively checks for any action to be triggered and calls its .Do() method if the trigger is active which makes sense of course but why is this done only if there's no (sub?) missions? Is this to allow those (sub?) missions to make the ultimate decision to act on the trigger or not?

Finally, you present an override that starts with calling the default behavior. But then it goes ahead and forces the affected action to trigger regardless. Why is that?

Thanks for taking the time mate!

FG28_Kodiak
10-31-2012, 01:59 PM
The first code is the orginal virtual method from ABattle (not from me, it contains also control structures for different missions), you don't have access to it via normal script, it was only a example how the orginal code looks like.

There is nothing interesting in AMission:

public abstract class AMission
{
// Methods
protected AMission()
{
}

public virtual void Init(ABattle battle, int missionNumber)
{
this.Battle = battle;
this.MissionNumber = missionNumber;
this.MissionNumberListener = missionNumber;
this.Inited();
}

public virtual void Inited()
{
}

public virtual bool IsMissionListener(int missionNumber)
{
return ((this.MissionNumberListener < 0) || (missionNumber == this.MissionNumberListener));
}

public virtual void OnActorCreated(int missionNumber, string shortName, AiActor actor)
{
}

public virtual void OnActorDamaged(int missionNumber, string shortName, AiActor actor, AiDamageInitiator initiator, NamedDamageTypes damageType)
{
}

public virtual void OnActorDead(int missionNumber, string shortName, AiActor actor, List<DamagerScore> damages)
{
}

public virtual void OnActorDestroyed(int missionNumber, string shortName, AiActor actor)
{
}

public virtual void OnActorTaskCompleted(int missionNumber, string shortName, AiActor actor)
{
}

public virtual void OnAiAirNewEnemy(AiAirEnemyElement element, int army)
{
}

public virtual void OnAircraftCrashLanded(int missionNumber, string shortName, AiAircraft aircraft)
{
}

public virtual void OnAircraftCutLimb(int missionNumber, string shortName, AiAircraft aircraft, AiDamageInitiator initiator, LimbNames limbName)
{
}

public virtual void OnAircraftDamaged(int missionNumber, string shortName, AiAircraft aircraft, AiDamageInitiator initiator, NamedDamageTypes damageType)
{
}

public virtual void OnAircraftKilled(int missionNumber, string shortName, AiAircraft aircraft)
{
}

public virtual void OnAircraftLanded(int missionNumber, string shortName, AiAircraft aircraft)
{
}

public virtual void OnAircraftLimbDamaged(int missionNumber, string shortName, AiAircraft aircraft, AiLimbDamage limbDamage)
{
}

public virtual void OnAircraftTookOff(int missionNumber, string shortName, AiAircraft aircraft)
{
}

public virtual void OnAutopilotOff(AiActor actor, int placeIndex)
{
}

public virtual void OnAutopilotOn(AiActor actor, int placeIndex)
{
}

public virtual void OnBattleInit()
{
}

public virtual void OnBattleStarted()
{
}

public virtual void OnBattleStoped()
{
}

public virtual void OnBombExplosion(string title, double mass, Point3d pos, AiDamageInitiator initiator, int eventArgInt)
{
}

public virtual void OnBuildingKilled(string title, Point3d pos, AiDamageInitiator initiator, int eventArgInt)
{
}

public virtual void OnCarter(AiActor actor, int placeIndex)
{
}

public virtual void OnMissionLoaded(int missionNumber)
{
}

public virtual void OnOrderMissionMenuSelected(Player player, int ID, int menuItemIndex)
{
}

public virtual void OnPersonHealth(AiPerson person, AiDamageInitiator initiator, float deltaHealth)
{
}

public virtual void OnPersonMoved(AiPerson person, AiActor fromCart, int fromPlaceIndex)
{
}

public virtual void OnPersonParachuteFailed(AiPerson person)
{
}

public virtual void OnPersonParachuteLanded(AiPerson person)
{
}

public virtual void OnPlaceEnter(Player player, AiActor actor, int placeIndex)
{
}

public virtual void OnPlaceLeave(Player player, AiActor actor, int placeIndex)
{
}

public virtual void OnPlayerArmy(Player player, int army)
{
}

public virtual void OnPlayerConnected(Player player)
{
}

public virtual void OnPlayerDisconnected(Player player, string diagnostic)
{
}

public virtual void OnSingleBattleSuccess(bool success)
{
}

public virtual void OnStationaryKilled(int missionNumber, GroundStationary _stationary, AiDamageInitiator initiator, int eventArgInt)
{
}

public virtual void OnTickGame()
{
}

public virtual void OnTickReal()
{
}

public virtual void OnTrigger(int missionNumber, string shortName, bool active)
{
}

public virtual void OnUserCreateUserLabel(GPUserLabel ul)
{
}

public virtual void OnUserDeleteUserLabel(GPUserLabel ul)
{
}

public virtual void Timeout(double sec, DoTimeout doTimeout)
{
this.Battle.Timeout(sec, doTimeout);
}

// Properties
public ABattle Battle { get; private set; }

public IGamePlay GamePlay
{
get
{
return this.Battle.GamePlay;
}
}

public int MissionNumber { get; private set; }

public int MissionNumberListener { get; protected set; }

public ITime Time
{
get
{
return this.Battle.Time;
}
}
}

moggel
11-09-2012, 07:22 AM
Thanks chaps. Everything is clear now. :O)

Punch_145
01-31-2013, 09:21 PM
Cant seem to get it to work...........:(
I have a trigger set for some Dorniers to spawn. The trigger is a air group entering an area and I see the Hud message but no dorniers spawn?

public override void OnTrigger(int missionNumber, string shortName, bool active)
{
base.OnTrigger(missionNumber, shortName, active);

if ("DoSpawn".Equals(shortName) && active)
{
AiAction action = GamePlay.gpGetAction("DoSpawn");

if (action != null)
{
action.Do();
}
SendScreenMessageTo(Blue(), "Dorniers request Escort: Boulogne,(H,5) Alt:3500M, 10 mins ! ");
GamePlay.gpGetTrigger(shortName).Enable = false;

}
}