Skip to content

Families and marriage from code

Weddings, divorces and births are three calls on Actor. Around them sit a set of terms that shape one call, a static class that holds the world’s rules and your hooks, and the records a marriage leaves behind. This page lists them. The ideas are on Families and marriage.

You want your game to marry two characters, end a marriage, bring a child into the world, or say no to a wedding that the world’s rules would allow. Writers refuse on a multiplayer client with one warning. All calls are main thread. See the Scripting overview.

All in Runtime/Actors/Actor.Family.cs.

Signature What it does
Marriage Marry(Actor spouse, MarriageTerms terms = default) Marries the actor to spouse. The world’s rules that are on are checked first. Returns null when refused. Several spouses are normal: earlier marriages of both stay. A wedding with a dead actor is a widowhood at once.
bool CanMarry(Actor spouse) True when the actor may marry spouse now.
bool CanMarry(Actor spouse, out MarriageCheck check) The same, and check says why not.
bool Divorce(Actor spouse, DivorceTerms terms = default) Ends a standing marriage, or a widowhood when the spouse is dead. Other marriages stay. false when the two are not married or a rule or hook refuses.
int DivorceAll(DivorceTerms terms = default) Divorces every living spouse. Widowhoods stay. Returns how many ended.
Actor HaveChild(Actor otherParent = null, ChildOptions options = null) Creates a child as a new runtime actor with both as parents. Returns null when refused.
bool AddChild(Actor child, bool joinFamily = true, bool setOrigin = true) Makes an existing actor a child of this actor. Refused when it would give the child a third parent, or when the child is this actor or one of its ancestors.
bool AddChild(Actor otherParent, Actor child, bool joinFamily = true, bool setOrigin = true) The same with two parents.

A Marry call made inside a listener of a family change is answered at once from the state the listener sees and carried out after the current change, checked again first. The Marriage you got back can still be refused then. To know whether it happened, ask MarriageWith once the change is over.

Example, copied from the demo project (Game/Rules/RulesPeople.cs, lines 143 and 149).

if (woman.CanMarry(man, out var check)) suitors.Add(man);
// ...
if (woman.Marry(husband) != null)
Type Fields Meaning
MarriageTerms (struct) Home, Family, Clan Home is a MarriageHome: ByCustom, Actor or Spouse. Family and Clan are a Move: ByCustom, Join or Stay, for the partner who is not home. The default value follows the customs.
DivorceTerms (struct) KeepFamily, KeepClan The partner who moved at the wedding stays in the family or clan.
ChildOptions (class) Name, Gender, Age, Culture, Portrait, Look, Family, FamilyOverride, JoinClan What a newborn is like. A field left at its default follows the parents and the world’s customs. Family is a ChildFamily: ByRule, Parent, OtherParent, None or Given.

FamilyLaw is a static class (Families/FamilyLaw.cs). It holds the world’s rules, checks with reasons, and the hooks.

Member What it does
static FamilyRules Rules The world’s rules of the running game. Change fields on it to change the rules. Marriages that exist are never undone.
static MarriageCheck Check(Actor actor, Actor spouse) Whether the two may marry now, and why not.
static event Func<Actor, Actor, bool> CanMarry Asked before a wedding, after the world’s rules allowed it. A listener that returns false refuses it with MarriageRefusal.Refused. Arguments: the actor the call is on, the spouse.
static event Func<Actor, Actor, bool> CanDivorce The same for a divorce.
static event Func<Actor, Actor, bool> CanHaveChild Asked before a birth, and before an existing actor is made a child. The second actor may be null.
static Func<Actor, Actor, Actor> DecideHome Decides who is home in a wedding held as the customs say. Return one of the two, or null to let the customs decide.
static Actor HomeOf(Actor actor, Actor spouse, MarriageTerms terms = default) Who would be home if the two married with these terms.
static Family FamilyOfChild(Actor parent, Actor otherParent) The family a child of the two would be born into.

A hook that throws is logged and counts as no objection. Hooks are cleared when a Play session starts, so subscribe from a component. A hook that starts another check inside itself is asked again at most four deep.

MarriageCheck has Allowed, Refusal, Who (the partner the refusal is about) and Text (the reason in plain English). MarriageRefusal is None, SamePerson, Dead, TooYoung, AlreadyMarried, SpouseLimit, CloseKin, SameGender or Refused. DivorceRefusal is None, NotMarried, Dead or Refused. BirthRefusal is None, SamePerson, ParentDead, ParentTooYoung, HasTwoParents, OwnAncestor or Refused.

Example, not compiled. It refuses any wedding that crosses an imaginary rule, here a check on a variable you would define.

void OnEnable() { FamilyLaw.CanMarry += Allow; }
void OnDisable() { FamilyLaw.CanMarry -= Allow; }
bool Allow(Actor actor, Actor spouse) => !MyGame.IsBetrothedElsewhere(actor, spouse);

A Marriage is shared by both partners (Families/Marriage.cs).

Member What it does
Actor First, Actor Second The partners. First is the actor the wedding was called on.
Actor Home The partner whose family the couple’s children are born into.
Actor Other(Actor partner) The other partner, or null.
bool Involves(Actor actor) True when the actor is one of the two.
MarriageState State, bool Standing Married, Widowed, Divorced or Past, and whether it is Married.
bool Moved The partner who is not home joined the home partner’s family through it.
int Order A world-wide sequence number. A smaller number is an earlier marriage. Saved with the game.

On the actor: Marriages (every marriage, earliest first, whatever its state), MarriageWith(Actor other) (the latest, or null), Spouse (the partner of the earliest standing marriage, else of the latest widowhood), SpouseCount, IsMarried, IsWidowed, MaritalStatus (Single, Married, Widowed or Divorced), GetSpouses(List<Actor> results, bool includeDead = false) and GetFormerSpouses(List<Actor> results).

Event Raised
IM.onMarried (MarriageInfo) Once after a wedding, when families, clans and both partners’ onActorIsMarried are done.
IM.onDivorced (MarriageInfo) Once after a divorce.
IM.onWidowed (MarriageInfo) For each marriage that became a widowhood because a partner died.
IM.onChildBorn (BirthInfo) Once after a birth, after each living parent’s onChildBorn.

MarriageInfo carries Marriage, Actor, Spouse, Home, Mover, FamilyLeft, FamilyJoined and Cause (Wedding, Divorce or Death). BirthInfo carries Child, Parent, OtherParent, Family, Clan and Marriage. A listener that throws is logged and does not stop the others.

What you see Why What to do
Marry returns null The world’s rules, FamilyLaw.CanMarry or a missing spouse refused it. The reason is logged at Log level. Ask CanMarry(spouse, out var check) first and read check.Text.
Divorce returns false The two are not married, or a rule or hook refused. Check MarriageWith(spouse).
HaveChild returns null A rule or CanHaveChild refused. Read the Log line.
Your hook stops working after Play starts Play start cleared it. Subscribe from OnEnable.