Actors, Components & Reactions
Things in a Moonwell map are actors (Actor). An actor holds components (Component) and reacts to game events through virtual reaction methods on its class and on its attached components.
Actors
Moonwell provides five actor kinds across two levels:
Game— the map-wide singleton (Game.Instance), which is also the rootOwner.Player— one of the 28 player slots (0toPlayer.MaxSlots - 1), retrieved withPlayer.Get(slot)orPlayer.Of(handle).- Widgets (
Widget : Actor):Unit(andHero : Unit) — a unit or hero on the map.Item— an item on the ground or in an inventory.Destructable— a tree, gate, barrel, or other destructable on the map.
Every widget has IsGone, which turns true once the widget is removed from the game (Unit.Remove(), Item.Remove(), Destructable.Remove(), or when the game removes it after decay).
Declared classes are runtime instances
When you declare a unit, hero, item, or destructable class in C#, every instance of that object type in the game is an instance of your class — whether created in code, trained at a building, dropped as loot, or placed in World Editor. Instance fields on the class belong to that individual object:
using Moonwell;
using Moonwell.Objects.Units.Human;
namespace MyMap;
public class Champion : Paladin
{
public int Kills { get; private set; }
public Champion()
{
Name = "Champion";
HitPoints = 950;
}
public override void OnKill(Unit victim)
{
Kills++;
Natives.DisplayTextToPlayer(Player.Handle, 0, 0, $"Champion kills: {Kills}");
}
} Unregistered standard objects (such as a standard Footman you haven't replaced or subclassed) are wrapped on sight as plain Unit, Hero, Item, or Destructable instances.
Creating and wrapping actors
Create units in game code with Unit.Create<T> or Unit.Create:
Champion hero = Unit.Create<Champion>(Player.Get(0), 0, 0, 270);
Unit guard = Unit.Create(ObjectTypes.IdOf<Footman>(), Player.Get(0), 200, 0, 270); Create items with Item.Create and ObjectTypes.IdOf<T>():
Item potion = Item.Create(ObjectTypes.IdOf<PotionOfGreaterHealing>(), 128, 0); When a native returns a raw unit, item, or destructable handle, wrap (or look up) its Moonwell actor with Of:
Unit? unit = Unit.Of(Natives.GetTriggerUnit());
Item? item = Item.Of(Natives.GetManipulatedItem());
Destructable? tree = Destructable.Of(Natives.GetEnumDestructable());
Player? owner = Player.Of(Natives.GetOwningPlayer(Natives.GetTriggerUnit())); Calling Unit.Of, Item.Of, Destructable.Of, or Player.Of with the same live handle always returns the same Moonwell instance.
Components
A component is a reusable piece of state and behaviour attached to an actor. Each actor kind has its own component base class:
| Component base class | Attached to | Actor property |
|---|---|---|
GameComponent : MapComponent | Game.Instance | Game |
PlayerComponent : MapComponent | Player | Player |
UnitComponent : Component | Unit / Hero | Unit |
ItemComponent : Component | Item | Item |
DestructableComponent : Component | Destructable | Destructable |
Attaching components
Add starting components in a Map, Unit, Hero, Item, or Destructable constructor via Components.Add(...). For units, items, and destructables, the generated factory re-runs those Components.Add(...) calls whenever an instance enters the game:
public class Champion : Paladin
{
public Champion()
{
Components.Add(new Fury());
}
} You can also attach components at run time with Add:
Game.Instance.Add(new WaveSpawner());
Player.Get(0).Add(new BountyTracker());
hero.Add(new Thorns());
potion.Add(new ExpireOnDrop()); Multiple instances of the same component class can be attached to the same actor.
Querying and removing components
Every Actor supports querying and removing its attached components by type (where T : Component, including base classes):
bool hasFury = hero.Has<Fury>();
Fury? first = hero.Get<Fury>();
List<Fury> all = hero.GetAll<Fury>();
int total = hero.ComponentCount;
hero.Remove(first!); // removes a specific component instance
int removed = hero.RemoveAll<Fury>(); // removes all matching components A component is also an Owner (and Owned), so calling component.End() removes it from its actor and ends any timers or other objects it owns.
Component lifecycle hooks
Override OnAdded() and OnRemoved() on any Component to run setup and cleanup when the component is attached to or detached from its actor:
public class RegenerationAura : UnitComponent
{
protected override void OnAdded()
{
Timer.Every(1f, () =>
{
Natives.SetWidgetLife(Unit.Handle, Natives.GetWidgetLife(Unit.Handle) + 10f);
});
}
protected override void OnRemoved()
{
// Timers started in OnAdded already belong to this component and end automatically.
}
} Reactions
Rather than creating and wiring trigger handles manually, override virtual reaction methods on your object classes or components.
Reaction methods by class
| Reaction method | Actor / Ability | Component |
|---|---|---|
OnGameStart() | — | MapComponent (GameComponent, PlayerComponent) |
OnEnter() | Unit, Hero, Item, Destructable (protected) | — |
OnLeave() | Unit, Hero, Item, Destructable (protected) | — |
OnUnitEnter(Unit unit) | — | MapComponent (GameComponent, PlayerComponent) |
OnDamaged(Damage damage) | Unit, Hero | UnitComponent, Buff |
OnDealtDamage(Damage damage) | Unit, Hero | UnitComponent, Buff |
OnUnitDamaged(Damage damage) | — | MapComponent (GameComponent, PlayerComponent) |
OnDeath(Unit? killer) | Unit, Hero | UnitComponent, Buff |
OnKill(Unit victim) | Unit, Hero | UnitComponent, Buff |
OnUnitDied(Unit dying, Unit? killer) | — | MapComponent (GameComponent, PlayerComponent) |
OnDeath() | Destructable | DestructableComponent |
OnCast(Cast cast) | Ability, Unit, Hero | UnitComponent, Buff |
OnUnitCast(Cast cast) | — | MapComponent (GameComponent, PlayerComponent) |
OnItemPickedUp(Item item) | Unit, Hero | UnitComponent, Buff |
OnItemDropped(Item item) | Unit, Hero | UnitComponent, Buff |
OnPickedUp(Unit by) | Item | ItemComponent |
OnDropped(Unit by) | Item | ItemComponent |
Modifying damage
OnDealtDamage, OnDamaged, and OnUnitDamaged receive a Damage object:
damage.Target(Unit) — the unit taking damage.damage.Source(Unit?) — the unit dealing damage, if any.damage.Amount(float, read/write) — the damage amount. Settingdamage.Amountupdates the game's event damage viaBlzSetEventDamage.damage.IsAttack(bool) —truewhen the damage came from a basic attack.
public class ManaShield : UnitComponent
{
public override void OnDamaged(Damage damage)
{
damage.Amount *= 0.5f;
}
} Dispatch order
Every event dispatches in a fixed, deterministic order. Within an actor, its own class method runs first, followed by its components in the order they were added (operating on a snapshot so adding or removing components mid-reaction is safe):
- Game start (
OnGameStart):Game.Instancecomponents (OnGameStart())Playercomponents across slots0..27(OnGameStart())
- Unit enters map:
- The unit's starting components are added (
OnAdded()), and thenunit.OnEnter() Game.Instancecomponents (OnUnitEnter(unit))unit.Playercomponents (OnUnitEnter(unit))
- The unit's starting components are added (
- Unit damaged (
Damage):- Source unit:
source.OnDealtDamage(damage)→sourcecomponents (OnDealtDamage(damage)) - Target unit:
target.OnDamaged(damage)→targetcomponents (OnDamaged(damage)) Game.Instancecomponents (OnUnitDamaged(damage))target.Playercomponents (OnUnitDamaged(damage))source.Playercomponents (OnUnitDamaged(damage)), ifsourceexists and belongs to a different player thantarget- If
damage.Amountwas changed by any reaction,BlzSetEventDamage(damage.Amount)is applied after all reactions complete.
- Source unit:
- Unit dies:
- Dying unit:
dying.OnDeath(killer)→dyingcomponents (OnDeath(killer)) - Killer unit (if any):
killer.OnKill(dying)→killercomponents (OnKill(dying)) Game.Instancecomponents (OnUnitDied(dying, killer))dying.Playercomponents (OnUnitDied(dying, killer))killer.Playercomponents (OnUnitDied(dying, killer)), ifkillerexists and belongs to a different player thandying- Buffs on
dyingwithEndsOnDeath == trueend after all death reactions have run.
- Dying unit:
- Spell cast (
Cast):ability.OnCast(cast)(withOwner.Currentset tocast)- Caster unit:
caster.OnCast(cast)→castercomponents (OnCast(cast)) Game.Instancecomponents (OnUnitCast(cast))caster.Playercomponents (OnUnitCast(cast))
- Item picked up or dropped:
- Item:
item.OnPickedUp(unit)/item.OnDropped(unit)→itemcomponents - Unit:
unit.OnItemPickedUp(item)/unit.OnItemDropped(item)→unitcomponents
- Item:
- Destructable dies:
destructable.OnDeath()→destructablecomponents (OnDeath())
Lazy triggers and error isolation
- Lazy trigger registration: Moonwell only registers Warcraft III event triggers for events that at least one active class or attached component actually overrides.
- Error isolation: Each reaction invocation is wrapped in an error handler. If one reaction throws an exception, Moonwell reports the C# file and line on screen and in
Documents\Warcraft III\CustomMapData\Moonwell\log.txt, and the remaining reactions for that event still run.