99
edits
m (Restore LiquidVolume variable, alter event header) |
(Add explanation for AllowStaticRegistration method) |
||
Line 12: | Line 12: | ||
To listen for an event you typically override the '''Register''' method of the base '''IPart''' or '''Effect''' class.<br/> | To listen for an event you typically override the '''Register''' method of the base '''IPart''' or '''Effect''' class.<br/> | ||
This is executed '''once''' when the IPart/Effect is added to the object and subsequently [[Modding:Serialization_(Saving/Loading)|serialized]].<br/> | This is executed '''once''' when the IPart/Effect is added to the object and subsequently [[Modding:Serialization_(Saving/Loading)|serialized]].<br/> | ||
Overriding the '''AllowStaticRegistration''' method to return true alters this behaviour: registrations are no longer serialized and '''Register''' is re-executed every '''save load'''.<br/> | |||
It is however possible to register for an event ''anywhere'' as long as you have an instance of both an IPart/Effect and a GameObject. | It is however possible to register for an event ''anywhere'' as long as you have an instance of both an IPart/Effect and a GameObject. | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public override void Register(GameObject obj) { | public override void Register(GameObject obj) { | ||
// Listen for when the GameObject in obj is awarded XP. | |||
obj.RegisterPartEvent(this, "AwardXP"); | |||
// The otherwise identical method call used for when [this] is an Effect. | |||
obj.RegisterEffectEvent(this, "AwardXP"); | |||
// Call the base Register method that we overrode. | |||
base.Register(obj); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 30: | Line 31: | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public void AwardPlayerXP() { | public void AwardPlayerXP() { | ||
// Get the player GameObject. | |||
GameObject player = XRLCore.Core.Game.Player.Body; | |||
// Create a new "AwardXP" Event, specifying an "Amount" parameter with the integer value 50. | |||
// You can keep declaring staggered parameters or leave them out entirely. | |||
// Event.New(ID, [PrmName1, PrmVal1, PrmName2, PrmVal2, PrmName3, ...]) | |||
Event awardXP = Event.New("AwardXP", "Amount", 50); | |||
// You can also set the parameters one by one instead of passing them to the constructor. | |||
awardXP.SetParameter("Amount", 50); | |||
// Fire the event on the player. | |||
player.FireEvent(awardXP); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 50: | Line 51: | ||
public override bool FireEvent(Event E) | public override bool FireEvent(Event E) | ||
{ | { | ||
// If the ID of our event is "AwardXP"... | |||
if (E.ID == "AwardXP") { | |||
// Get the parameter we specified either in the constructor or using SetParameter. | |||
int amount = E.GetIntParameter("Amount"); | |||
// Add the amount to this GameObject's experience. | |||
this.ParentObject.Statistics["XP"].BaseValue += amount; | |||
} | |||
// Return the result of the base FireEvent that we overrode, which returns a literal true. | |||
// Should you instead return false further event processing will stop, meaning no Parts and Effects after get the chance to handle the event. | |||
return base.FireEvent(E); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<!-- Minimal events? Minified? Minimum? Unsure. | <!-- Minimal events? Minified? Minimum? Unsure. | ||
Rename if someone can coax one out of the devs. -Arm --> | |||
=Minimal Events= | =Minimal Events= | ||
Minimal events are a new event system introduced with the {{favilink|Tomb of the Eaters}} update (V200).<br/> | Minimal events are a new event system introduced with the {{favilink|Tomb of the Eaters}} update (V200).<br/> | ||
Line 78: | Line 79: | ||
// Listens for one event always. | // Listens for one event always. | ||
public override bool WantEvent(int ID, int cascade) { | public override bool WantEvent(int ID, int cascade) { | ||
// Check if the ID parameter matches one of the events we want, in this case ZoneActivatedEvent. | |||
// The base WantEvent of IPart/Effect will always return false. | |||
return base.WantEvent(ID, cascade) || ID == ZoneActivatedEvent.ID; | |||
} | } | ||
Line 89: | Line 90: | ||
// Listens for an event dynamically. | // Listens for an event dynamically. | ||
public override bool WantEvent(int ID, int cascade) { | public override bool WantEvent(int ID, int cascade) { | ||
if(ID == ZoneActivatedEvent.ID) | |||
return true; | |||
if(wantEndTurn && ID == EndTurnEvent.ID) | |||
return true; | |||
return base.WantEvent(ID, cascade); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 103: | Line 104: | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public void GivePlayerDrams() { | public void GivePlayerDrams() { | ||
// Get the player GameObject. | |||
GameObject player = XRLCore.Core.Game.Player.Body; | |||
// Retrieve a GiveDramsEvent from the pool, this is the preferred way to get an event instance. | |||
GiveDramsEvent E = GiveDramsEvent.FromPool(); | |||
// Some events have overloaded FromPool to set properties of the event on retrieval. | |||
E = GiveDramsEvent.FromPool(Actor: player, Drams: 50); | |||
// You can still set properties directly. | |||
E.Liquid = "water"; | |||
// Yet other events do not have an accessible FromPool method, in which case you'll have to instantiate your own. | |||
E = new GiveDramsEvent() { | |||
Actor = player, | |||
Drams = 50 | |||
}; | |||
// Fire the event on the player. | |||
player.HandleEvent(E); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 132: | Line 133: | ||
// This method handles the GiveDramsEvent. | // This method handles the GiveDramsEvent. | ||
public bool HandleEvent(GiveDramsEvent E) { | public bool HandleEvent(GiveDramsEvent E) { | ||
liquid.GiveDrams(E.Liquid, ref E.Drams, E.Auto); | |||
// Just like old events, returning false prevents further event processing. | |||
// Here we have no more liquid left to give so there's no reason to continue. | |||
if (E.Drams <= 0) | |||
return false; | |||
return true; | |||
} | } | ||
// This method handles the FrozeEvent. | // This method handles the FrozeEvent. | ||
public bool HandleEvent(FrozeEvent E) { | public bool HandleEvent(FrozeEvent E) { | ||
E.Object.DisplayName = "&CFrozen Object"; | |||
return true; | |||
} | } | ||
Line 151: | Line 152: | ||
// This is more useful for when you're cascading events carte blanche to sub-objects, see the Cascading section below. | // This is more useful for when you're cascading events carte blanche to sub-objects, see the Cascading section below. | ||
public override bool HandleEvent(MinEvent E) { | public override bool HandleEvent(MinEvent E) { | ||
if (E.ID == GiveDramsEvent.ID) { | |||
return HandleEvent(E as GiveDramsEvent); | |||
} else if (E is FrozeEvent) { | |||
return HandleEvent(E as FrozeEvent); | |||
} | |||
return true; | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 179: | Line 180: | ||
{ | { | ||
// If the cascade variable has the inventory bit... | // If the cascade variable has the inventory bit... | ||
if (MinEvent.CascadeTo(cascade, MinEvent.CASCADE_INVENTORY)) { | |||
// Check if any item in our inventory wants this event. | |||
foreach(GameObject item in Inventory) { | foreach(GameObject item in Inventory) { | ||
if(item.WantEvent(ID, cascade)) | if(item.WantEvent(ID, cascade)) | ||
return true; | return true; | ||
} | } | ||
} | |||
return base.WantEvent(ID, cascade); | |||
} | } | ||