OK thanks - I see the problem now.
Ultimately this comes down to the fact runtime.objects.Monster
has a static type of IObjectType<InstanceType.Monster>
, which refers to the base class rather than the instance subclass. That base class is ultimately propagated to all its event handlers.
It's difficult to static type this correctly as from the perspective of TypeScript, setInstanceClass
is a runtime method and so can't be used to affect static typing.
I think this is just one of those cases you have to override TypeScript's types as you know more than TypeScript does. There's a couple of ways around it. Probably the most straightforward is you can just use as
to cast it to the right type, e.g.:
runtime.objects.Monster.addEventListener("instancecreate", e => DoMonsterSpawnedFunction4(e.instance as MonsterInstance));
// ...
const DoMonsterSpawnedFunction4 = (inst: MonsterInstance) => {
inst.OnCreated();
};
I'd recommend the above approach, but another possible approach is to make your own reference to the object type with the correct type IObjectType<MonsterInstance>
- and then using addEventListener
will propagate the type MonsterInstance
down to the event handler, e.g.:
const MonsterInstanceType = runtime.objects.Monster as IObjectType<MonsterInstance>;
MonsterInstanceType.addEventListener("instancecreate", DoMonsterSpawnedFunction2);
So yeah, basically just override the type for this case.