Time for action – employing scripting for npc AI
Let's implement a script serving as artificial intelligence (AI) for a nonplayer character in a simple Dungeons & Dragons game. The engine will periodically execute the script, exposing two objects to it—the creature and the player. The script will be able to query the properties of the player and invoke functions on the creature.
Let's create a new project. We'll start by implementing the C++ class for creatures in our game world. Since both the NPC and player are living entities, we can have a common base class for them. In Chapter 4, Qt Core Essentials, we already had a data structure for players, so let's use that as a base by equipping our entities with similar attributes. Implement LivingEntity
as a subclass of QObject
with the following properties:
Q_PROPERTY(QString name READ name NOTIFY nameChanged) Q_PROPERTY(char direction READ direction NOTIFY directionChanged) Q_PROPERTY(QPoint...