Class TItemResource

Unit

Declaration

type TItemResource = class(T3DResource)

Description

Basic resource of an item that can be picked up, used and such.

A "resource" is an information shared by all items of given type, for example you can have two instances of class TItemResource: Sword and LifePotion. (Actually, TItemWeaponResource, which is a descendant of TItemResource, sounds like a better candidate for the Sword.) Using them, you can create milions of actual swords and life potions, and place them of your level (as well as in inventories of creatures able to carry items). Every life potion (TInventoryItem instance) may keep some individual information (for example, how much of the potion is already used/drunk), but all life potions will share the same TItemResource instance, so e.g. they all will be displayed using the same model on 3D level (TItemResource.BaseAnimation) and the same image in 2D inventory (TItemResource.Image).

Hierarchy

Overview

Methods

Protected procedure PrepareCore(const Params: TPrepareParams; const DoProgress: boolean); override;
Protected function ItemClass: TInventoryItemClass; virtual;
Public constructor Create(const AName: string); override;
Public destructor Destroy; override;
Public procedure LoadFromFile(ResourceConfig: TCastleConfig); override;
Public function Image: TEncodedImage;
Public function GLImage: TGLImage;
Public function CreateItem(const AQuantity: Cardinal): TInventoryItem;
Public procedure InstantiatePlaceholder(World: TSceneManagerWorld; const APosition, ADirection: TVector3; const NumberPresent: boolean; const Number: Int64); override;
Public function AlwaysPrepared: boolean; override;

Properties

Public property Caption: string read FCaption;
Public property BaseAnimation: T3DResourceAnimation read FBaseAnimation;
Public property ImageURL: string read FImageURL;

Description

Methods

Protected procedure PrepareCore(const Params: TPrepareParams; const DoProgress: boolean); override;
 
Protected function ItemClass: TInventoryItemClass; virtual;

Which TInventoryItem descendant to create when constructing item of this resource by CreateItem.

Public constructor Create(const AName: string); override;
 
Public destructor Destroy; override;
 
Public procedure LoadFromFile(ResourceConfig: TCastleConfig); override;
 
Public function Image: TEncodedImage;

2D image representing an item, to be used when showing inventory and such. The core engine itself doesn't use it, and doesn't force anything about such image (whether it should have some specific size, whether it should have alpha channel and such). It is up to the final game to make use of these images.

If you're wondering how to generate such image: one option is to open the item 3D model in [https://castle-engine.io/view3dscene.php] and use "Display -> Screenshot ..." menu option (maybe the one that makes transparent background). It is usually a good idea to also remember the camera used for such screenshot with "Console -> Print Current Camera (Viewpoint)..." menu option.

Public function GLImage: TGLImage;

Resource to draw Image.

Public function CreateItem(const AQuantity: Cardinal): TInventoryItem;

Create item. This is how you should create new TInventoryItem instances. It is analogous to TCreatureResource.CreateCreature, but now for items.

Note that the item itself doesn't exist on a 3D world — you have to put it there if you want by TInventoryItem.PutOnWorld. That is because items can also exist only in player's backpack and such, and then they are independent from 3D world.

Examples:

You usually define your own item resources by adding a subdirectory with resource.xml file to your game data. See [https://castle-engine.io/creating_data_resources.php] and engine tutorial for examples how to do this. Then you load the item resources with

var
  Sword: TItemResource;
...
  Resources.LoadFromFiles;
  Sword := Resources.FindName('Sword') as TItemResource;

where 'Sword' is just our example item resource, assuming that one of your resource.xml files has resource with name="Sword".

Now if you want to add the sword to your 3D world by code:

Because TInventoryItem instance is automatically owned (freed) by the 3D world or inventory that contains it, a simplest example how to add an item to your 3D world is this:

Sword.CreateItem(1).PutOnWorld(SceneManager.World, Vector3(2, 3, 4));

This adds 1 item of the MyItemResource to the 3D world, on position (2, 3, 4). In simple cases you can get SceneManager instance from TCastleWindow.SceneManager or TCastleControl.SceneManager.

If you want to instead add sword to the inventory of Player, you can call

SceneManager.Player.PickItem(Sword.CreateItem(1));

This assumes that you use SceneManager.Player property. It's not really obligatory, but it's the simplest way to have player with an inventory. See engine tutorial for examples how to create player. Anyway, if you have any TInventory instance, you can use TInventory.Pick to add TInventoryItem this way.

Public procedure InstantiatePlaceholder(World: TSceneManagerWorld; const APosition, ADirection: TVector3; const NumberPresent: boolean; const Number: Int64); override;

Instantiate placeholder by create new item with CreateItem and putting it on level with TInventoryItem.PutOnWorld.

Public function AlwaysPrepared: boolean; override;
 

Properties

Public property Caption: string read FCaption;

Nice caption to display.

Public property BaseAnimation: T3DResourceAnimation read FBaseAnimation;
 
Public property ImageURL: string read FImageURL;
 

Generated by PasDoc 0.15.0.