10.1. Using existing creatures / items classes

You can use the existing creature and item classes in CastleCreatures and CastleItems units. To do this, define your resources (creatures/items) in the resource.xml files, and call


(from unit CastleResources) at initialization. That's i! See castle_game_engine/examples/fps_game/data/ for sample creatures/items. You can copy their resource.xml files (and accompanying 3D models) to your project, as a starting point.

See creating resources guide for the detailed documentation what can be used in resource.xml file. To add initial creatures/items on the level, place on the level 3D model special "placeholder" items, see creating levels guide.

Creating creatures / items during the game

Let's create (spawn) a new creature at an arbitrary position using Object Pascal code. This assumes we have a creature named Knight, that is there must be a resource.xml file with name="Knight".

uses ..., CastleVectors, CastleCreatures;
procedure SpawnMyCreature;
  Position, Direction: TVector3Single;
  CreatureResource: TCreatureResource;
  Position := Vector3Single(1, 2, 3);
  Direction := Vector3Single(1, 0, 0);
  CreatureResource := Resources.FindName('Knight') as TCreatureResource;
  { CreateCreature creates TCreature instance and adds it to SceneManager.Items.
    We could store CreateCreature result, but in this case we just ignore it. }
  CreatureResource.CreateCreature(SceneManager.Items, Position, Direction);
// Remember to actually call SpawnMyCreature
// from wherever you want in your game.
// For example, from the OnPress event.

Creating an item is similar, except that we have an intermediate step where we get TInventoryItem instance. This can be either wrapped inside TItemOnWorld instance to put it on level, or it can be added to someone's inventory.

uses ..., CastleVectors, CastleItems;
procedure SpawnMyItemOnLevel;
  Position: TVector3Single;
  ItemResource: TItemResource;
  Item: TInventoryItem;
  Position := Vector3Single(1, 2, 3);
  ItemResource := Resources.FindName('MedKit') as TItemResource;
  { ItemResource.CreateItem(<quantity>) creates new TInventoryItem instance. }
  Item := ItemResource.CreateItem(1);
  { PutOnWorld method creates TItemOnWorld (that "wraps" the TInventoryItem
    instance) and adds it to SceneManager.Items.
    We could store PutOnWorld result, but in this case we just ignore it. }
  Item.PutOnWorld(SceneManager.Items, Position);
  { You may of course shorten it like this: }
  // ItemResource.CreateItem(1).PutOnWorld(SceneManager.Items, Position);
  { You could instead add the item directly to someone's inventory, like this: }
  // Player.PickItem(Item);
  // Player.PickItem(ItemResource.CreateItem(1));
// Remember to actually call SpawnMyItemOnLevel
// from wherever you want in your game.
// For example, from the OnPress event.

Overview of existing resource classes


Creature with walk-attack state intelligence. Such creature tracks the enemy (remembers last seen enemy 3D position, walks/flies to it, possibly through sectors/waypoints — so it can pass through narrow doors in a labyrinth or walk over a narrow bridge), attacks the enemy from the right distance (this can be either a melee attack, or shooting a missile — which adds a missile to the 3D world), eventually runs from the enemy (when enemy is too close and/or our creature health is low).

There are a lot of settings to achieve particular behavior, e.g. cowardly/brave, offensive/defensive, melee/ranged, etc.


A "missile" intelligence that blindly goes into the given direction (possibly with gravity and/or homing (close-on-target) features). On impact, missile may explode, hurting player and/or other creatures. Any kind of missile, like arrow or lighting bolt, is represented as such "missile creature", that flies independently of the shooter.


Creature just standing still. It can still show some looping animation, but there is no fancy logic behind it. It can die. This is one way to make destructible level parts.


Basic item that can be kept in the inventory. You usually want to extend this class, otherwise the item doesn't do anything except occupying space in the inventory... Although, if you all you want is something like keys/keycards in the game, then this is enough.


Weapon that can be equipped. Very configurable in resource.xml file, can make a melee attack, or immediately shoot, or fire a missile (the last case means that we create new creature of TMissileCreatureResource type), may need ammunition or not.