MatchEngine: Difference between revisions
Tayfunwiki (talk | contribs) (Replaced content with ",") Tag: Replaced |
Tayfunwiki (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
, | = 🧠 Intrigues MatchEngine API Reference = | ||
== Overview == | |||
The <code>MatchEngine</code> is part of the Intrigues core system and provides a lightweight, callback-based matchmaking mechanism between actors. | |||
It helps characters find suitable partners using rule-based compatibility checks and user-defined filters. | |||
---- | |||
== 🔧 CandidateFilter (Optional) == | |||
<syntaxhighlight lang="c#"> | |||
IM.Instance.MatchEngine.CandidateFilter = (self, other) => ...; | |||
</syntaxhighlight> | |||
=== Description === | |||
<code>CandidateFilter</code> is a user-defined function that determines who can be evaluated as a match. | |||
It runs '''before''' expensive rule-based logic (<code>IsCompatibleAsync</code>) to filter out invalid candidates. | |||
---- | |||
=== Signature === | |||
<syntaxhighlight lang="c#"> | |||
public Func<Actor, Actor, bool> CandidateFilter; | |||
</syntaxhighlight> | |||
---- | |||
=== Purpose === | |||
To filter out obviously incompatible actors early — improving performance and allowing logic customization | |||
(e.g., same-gender marriage, family bans, rank restrictions). | |||
---- | |||
=== Example === | |||
<syntaxhighlight lang="c#"> | |||
// Only opposite-gender, unmarried actors | |||
IM.Instance.MatchEngine.CandidateFilter = (self, other) => | |||
!other.HasSpouse && other.Gender != self.Gender; | |||
// Same-gender allowed | |||
IM.Instance.MatchEngine.CandidateFilter = (self, other) => | |||
!other.HasSpouse; // No gender check | |||
</syntaxhighlight><blockquote>📝 '''Note:''' CandidateFilter is typically set in <code>DemoManager.cs > Start()</code> and affects all matchmaking.</blockquote> | |||
---- | |||
== 🔍 FindMatch (Callback-Based) == | |||
<syntaxhighlight lang="c#"> | |||
IM.Instance.MatchEngine.FindMatch(actor, "Love Rule", match => { | |||
if (match != null) | |||
actor.StartScheme("Love", match); | |||
}); | |||
</syntaxhighlight> | |||
=== Description === | |||
<code>FindMatch</code> scans all eligible candidates for the given actor, using a specified rule (like <code>"Love Rule"</code>). | |||
It executes asynchronously in the background and returns the first valid match through a callback. | |||
---- | |||
=== Signature === | |||
<syntaxhighlight lang="c#"> | |||
public void FindMatch(Actor actor, string rule, Action<Actor> onMatchFound) | |||
</syntaxhighlight> | |||
---- | |||
=== Parameters === | |||
{| class="wikitable" | |||
!Name | |||
!Type | |||
!Description | |||
|- | |||
|<code>actor</code> | |||
|<code>Actor</code> | |||
|The actor seeking a match | |||
|- | |||
|<code>rule</code> | |||
|<code>string</code> | |||
|The rule used to determine compatibility | |||
|- | |||
|<code>onMatchFound</code> | |||
|<code>Action<Actor></code> | |||
|Called with the matched actor or <code>null</code> if none found | |||
|} | |||
---- | |||
=== Example === | |||
<syntaxhighlight lang="c#"> | |||
IM.Instance.MatchEngine.FindMatch(actor, "Love Rule", match => { | |||
if (match != null) | |||
actor.StartScheme("Love", match); | |||
else | |||
Debug.Log("No match found."); | |||
}); | |||
</syntaxhighlight> | |||
---- | |||
=== Notes === | |||
* The callback '''always fires''', even if no match is found (with <code>null</code>). | |||
* Ideal for use in <code>Update()</code>, timers, <code>MarriageTimer()</code>, or any non-async context. | |||
* Evaluation is spread over frames to avoid freezing the main thread. | |||
---- | |||
== ⚠️ Performance Tip == | |||
Always define a <code>CandidateFilter</code> — especially in simulations with 100+ actors — to avoid evaluating thousands of unnecessary compatibility rules. | |||
---- | |||
== 🧪 Debugging Tip == | |||
<syntaxhighlight lang="c#"> | |||
var count = IM.Actors.Count(p => MatchEngine.CandidateFilter(actor, p)); | |||
Debug.Log($"Evaluating {count} candidates for {actor.Name}"); | |||
</syntaxhighlight>You can log how many candidates passed the filter: | |||
---- | |||
== ✅ Summary == | |||
{| class="wikitable" | |||
!Feature | |||
!Purpose | |||
|- | |||
|CandidateFilter | |||
|Pre-filters actors before heavy compatibility check | |||
|- | |||
|FindMatch | |||
|Finds the first compatible partner via callback | |||
|- | |||
|Fully async | |||
|Internally asynchronous without requiring async/await | |||
|- | |||
|Lightweight | |||
|Designed to scale to hundreds of actors | |||
|- | |||
|Customizable | |||
|Users define their own filtering logic | |||
|} | |||
---- |
Latest revision as of 22:23, 16 May 2025
🧠 Intrigues MatchEngine API Reference
Overview
The MatchEngine
is part of the Intrigues core system and provides a lightweight, callback-based matchmaking mechanism between actors.
It helps characters find suitable partners using rule-based compatibility checks and user-defined filters.
🔧 CandidateFilter (Optional)
IM.Instance.MatchEngine.CandidateFilter = (self, other) => ...;
Description
CandidateFilter
is a user-defined function that determines who can be evaluated as a match.
It runs before expensive rule-based logic (IsCompatibleAsync
) to filter out invalid candidates.
Signature
public Func<Actor, Actor, bool> CandidateFilter;
Purpose
To filter out obviously incompatible actors early — improving performance and allowing logic customization
(e.g., same-gender marriage, family bans, rank restrictions).
Example
// Only opposite-gender, unmarried actors
IM.Instance.MatchEngine.CandidateFilter = (self, other) =>
!other.HasSpouse && other.Gender != self.Gender;
// Same-gender allowed
IM.Instance.MatchEngine.CandidateFilter = (self, other) =>
!other.HasSpouse; // No gender check
📝 Note: CandidateFilter is typically set in
DemoManager.cs > Start()
and affects all matchmaking.
🔍 FindMatch (Callback-Based)
IM.Instance.MatchEngine.FindMatch(actor, "Love Rule", match => {
if (match != null)
actor.StartScheme("Love", match);
});
Description
FindMatch
scans all eligible candidates for the given actor, using a specified rule (like "Love Rule"
).
It executes asynchronously in the background and returns the first valid match through a callback.
Signature
public void FindMatch(Actor actor, string rule, Action<Actor> onMatchFound)
Parameters
Name | Type | Description |
---|---|---|
actor
|
Actor
|
The actor seeking a match |
rule
|
string
|
The rule used to determine compatibility |
onMatchFound
|
Action<Actor>
|
Called with the matched actor or null if none found
|
Example
IM.Instance.MatchEngine.FindMatch(actor, "Love Rule", match => {
if (match != null)
actor.StartScheme("Love", match);
else
Debug.Log("No match found.");
});
Notes
- The callback always fires, even if no match is found (with
null
). - Ideal for use in
Update()
, timers,MarriageTimer()
, or any non-async context. - Evaluation is spread over frames to avoid freezing the main thread.
⚠️ Performance Tip
Always define a CandidateFilter
— especially in simulations with 100+ actors — to avoid evaluating thousands of unnecessary compatibility rules.
🧪 Debugging Tip
var count = IM.Actors.Count(p => MatchEngine.CandidateFilter(actor, p));
Debug.Log($"Evaluating {count} candidates for {actor.Name}");
You can log how many candidates passed the filter:
✅ Summary
Feature | Purpose |
---|---|
CandidateFilter | Pre-filters actors before heavy compatibility check |
FindMatch | Finds the first compatible partner via callback |
Fully async | Internally asynchronous without requiring async/await |
Lightweight | Designed to scale to hundreds of actors |
Customizable | Users define their own filtering logic |