Jump to content

[Help] Ability Trigger does not work


Bret1

Recommended Posts

...I just find it a lot harder to track down what's not working correctly in situations like this when the even logic is all packaged away.

 

If I understand correctly, the ability should be triggering damage against the unit with the ability every time they end a move? From what I can tell, it does look like that should be working.

I want the abiltiy to trigger in death, but changed that to move, as that is easier/faster to test.

I also think it looks correct as it is basicly just coped from the andromedon code...

but like u said its realy hard to debug, so i hope u dont mind me picking ur code apart ;)

((love the Infantry mod btw^^))

Link to comment
Share on other sites

I wrote an Effect which i added to the DominateTemplate which adds an ability to the target (if the target doesnt already has the ability).

 

 

I have a theory why this isn't working. After poking around in X2TacticalGameRuleSet.uc I found a function which is responsible for registering EventListeners in abilties.

//  THIS SHOULD ALMOST NEVER BE CALLED OUTSIDE OF NORMAL TACTICAL INIT SEQUENCE. USE WITH EXTREME CAUTION.
simulated function StateObjectReference InitAbilityForUnit(X2AbilityTemplate AbilityTemplate, XComGameState_Unit Unit, XComGameState StartState, optional StateObjectReference ItemRef, optional StateObjectReference AmmoRef)
{
   ///......
}

I won't post entire function (as you can look it up), but it would appear these triggers might be registered during mission start up .. only once. However, in your case, you are adding the ability via an effect (dynamically). My guess is that the trigger simply doesn't get registered in this case.

 

I don't know how you add the ability in your effect, but you might be able to add RegisterListener call manually.

Link to comment
Share on other sites

Thanks CaveRat,

that was the trick. I coped the parts of that funktion that do matter and added them to my effect that adds the Ability.

now the ability does work with the trigger.

:smile:

// Adds a Ability to a unit (if the unit doenst already have the ability)
// Code is partialy copied from the "EvacAll" Mod (thanks for documenting it so nicely :) )
class X2Effect_EnsureAbilityOnUnit extends X2Effect config(LegitPsi);

var X2AbilityTemplate AbilityTemplate;
var name AbilityTemplateName;

simulated protected function OnEffectAdded(const out EffectAppliedData ApplyEffectParameters, XComGameState_BaseObject kNewTargetState, XComGameState NewGameState, XComGameState_Effect NewEffectState)
{
	local X2AbilityTemplateManager AbilityTemplateManager;
	if(AbilityTemplateName == '') {
		`RedScreenOnce("LegitPsi - EnsureAbilityOnUnit - AbilityTemplateName is not set");
	}
	
	AbilityTemplateManager = class'X2AbilityTemplateManager'.static.GetAbilityTemplateManager();
	AbilityTemplate = AbilityTemplateManager.FindAbilityTemplate(AbilityTemplateName);
	if(AbilityTemplate == none) {
		`RedScreenOnce("LegitPsi - EnsureAbilityOnUnit - AbilityTemplate " @ AbilityTemplateName @ " not found");
	}
	EnsureAbilityOnUnit(ApplyEffectParameters.TargetStateObjectRef,AbilityTemplate,AbilityTemplateName,NewGameState);
}

// Ensure the unit represented by the given reference has the ability
function EnsureAbilityOnUnit(StateObjectReference UnitStateRef, X2AbilityTemplate Template, name TemplateName, XComGameState NewGameState)
{
	local XComGameState_Unit UnitState, NewUnitState;
	local XComGameState_Ability AbilityState;
	local StateObjectReference StateObjectRef;
	
	local X2TacticalGameRuleset TacticalRules;
	local XComGameState_Player PlayerState;
	local Object FilterObj, AbilityObj;
	local AbilityEventListener kListener;
	local X2AbilityTrigger Trigger;
	local X2AbilityTrigger_EventListener AbilityTriggerEventListener;

	// Find the current unit state for this unit
	UnitState = XComGameState_Unit(`XCOMHISTORY.GetGameStateForObjectId(UnitStateRef.ObjectID));
	`Log("EnsureAbilityOnUnit" @ TemplateName,, 'LegitPsi');//`
	// Loop over all the abilities they have
	foreach UnitState.Abilities(StateObjectRef) 
	{
		AbilityState = XComGameState_Ability(`XCOMHISTORY.GetGameStateForObjectID(StateObjectRef.ObjectID));//`
		// If the unit already has this ability, don't add a new one.
		if (AbilityState.GetMyTemplateName() == TemplateName)
		{
			return;
		}
	}

	// Construct a new unit game state for this unit, adding an instance of the ability
	NewUnitState = XComGameState_Unit(NewGameState.CreateStateObject(class'XComGameState_Unit', UnitState.ObjectID));
	AbilityState = Template.CreateInstanceFromTemplate(NewGameState);
	AbilityState.InitAbilityForUnit(NewUnitState, NewGameState);
	NewGameState.AddStateObject(AbilityState);
	NewUnitState.Abilities.AddItem(AbilityState.GetReference());
	NewGameState.AddStateObject(NewUnitState);

	//Now register the EventListerners and Triggers of the Ability
	TacticalRules = `TACTICALRULES;//`

	PlayerState = XComGameState_Player(NewGameState.GetGameStateForObjectID(NewUnitState.ControllingPlayer.ObjectID));
	if (PlayerState == none) {
		PlayerState = XComGameState_Player(`XCOMHISTORY.GetGameStateForObjectID(NewUnitState.ControllingPlayer.ObjectID));//`
	}

	foreach Template.AbilityEventListeners(kListener)
	{
			AbilityObj = AbilityState;
			
			FilterObj = GetEventFilterObject(kListener.Filter, NewUnitState, PlayerState);
			`XEVENTMGR.RegisterForEvent(AbilityObj, kListener.EventID, kListener.EventFn, kListener.Deferral, /*priority*/, FilterObj);//`
	}
	foreach Template.AbilityTriggers(Trigger)
	{
		if (Trigger.IsA('X2AbilityTrigger_EventListener'))
		{
			AbilityTriggerEventListener = X2AbilityTrigger_EventListener(Trigger);
			FilterObj = GetEventFilterObject(AbilityTriggerEventListener.ListenerData.Filter, NewUnitState, PlayerState);
			AbilityTriggerEventListener.RegisterListener(AbilityState, FilterObj);
		}
	}
}

private function Object GetEventFilterObject(AbilityEventFilter eventFilter, XComGameState_Unit FilterUnit, XComGameState_Player FilterPlayerState)
{
	local Object FilterObj;

	switch(eventFilter)
	{
	case eFilter_None:
		FilterObj = none;
		break;
	case eFilter_Unit:
		FilterObj = FilterUnit;
		break;
	case eFilter_Player:
		FilterObj = FilterPlayerState;
		break;
	}

	return FilterObj;
}

That is the Effect i wrote for anybody who might try something similar :smile:

Edited by Bret1
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...