For C++ class methods, 'const' means you won't modify any class member variables. It's completely valid to change other memory that's separate from the class. For C++ class methods, it's sometimes easier to consider them as having an implicit 'this' pointer parameter. And qualifiers such as const (or volatile), are applied to what 'this' points to. So for example, with
Code: Select all
class Npc {
...
void onHit(const MWWorld::Ptr &ptr, float damage, bool ishealth, const MWWorld::Ptr &object, const MWWorld::Ptr &attacker, const osg::Vec3f &hitPosition, bool successful) const;
...
};
you can think of it like
Code: Select all
void onHit(const Npc *this, const MWWorld::Ptr &ptr, float damage, bool ishealth, const MWWorld::Ptr &object, const MWWorld::Ptr &attacker, const osg::Vec3f &hitPosition, bool successful);
};
whereas if the const qualifier was omitted, it would be like
Code: Select all
void onHit(Npc *this, const MWWorld::Ptr &ptr, float damage, bool ishealth, const MWWorld::Ptr &object, const MWWorld::Ptr &attacker, const osg::Vec3f &hitPosition, bool successful);
};
More conceptually, as I understand it, OpenMW doesn't store the NPC state in the Npc class. Instead, the NPC state is divided into sub-groups. So for instance, NPCs have 'actor-specific state' (position, name, etc), 'creature-specific state' (health, AI, etc), and 'npc-specific state' (skills, attributes, etc). This is because different actor types are comprised of different sub-groups of state, and it's helpful to keep each sharable sub-group type together for common access, rather than duplicating it for each type (e.g. no need to define NPCs and Monsters as completely separately object types, when they have common state like names, positions, health, AI, etc).
In this case, the Npc class simply provides access to get, use, or modify state an NPC would have, which is stored elsewhere. Because an NPC being hit needs to check npc-specific state (skills and attributes), an NPC being hit would need to call Npc::
onHit. The MWWorld::
Ptr object acts as a lookup for the Npc class to know which instance's state to use. And because an NPC also has actor- and creature-specific state, the same Ptr can be used to get, use, or modify actor- and creature-specific state too, since they all belong to the same object instance.