First, to benefit from this system, you need two mods (if there is a single mod, the information can be passed directly). And:
- you want to be able to load these mods separately;
- if loaded together, you want them to communicate.
Say, mod A provides maximum monster health modification, and mod B wants to know the new maximum monster health.
In mod A you define a Service that looks like this:
- Code: Select all • Expand view
class MaxEnemyHealthService : Service
{
override string get(string enemyClassName)
{
int newMaxHealth = calculateNewEnemyHealth(enemyClassName);
string result = string.format("%d", newMaxHealth);
return result;
}
}
where calculateNewEnemyHealth is whatever math you have.
in mod B, where you want to know if the maximum monster health is modified, and what the new value is, you look if the service exists:
- Code: Select all • Expand view
...
ServiceIterator i = ServiceIterator.find("MaxEnemyHealthService");
if (!i.serviceExists())
{
// use default maximum monster health
return;
}
Service s;
while (s = i.Next())
{
string maxHealthData = s.get(monster.getClassName());
int maxHealth = maxHealthData.toInt();
// do something with maxHealth.
}
...
Note that somebody else may create another version of mod A, say, mod A2, and you have to deal with several information providers. Or mod A could subclass MaxEnemyHealthService several times.
Regarding scope, yes, there are two variants of get: in play and UI scope. Use one that is appropriate for your data usage. Purely cosmetical - use UI. Modifies gameplay - use play.