Castle Game Engine: CastleItems: Class TItemResource

# Class TItemResource
Description | Hierarchy | Fields | Methods | Properties
## Unit CastleItems ## 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 TObject T3DResource TItemResource ## Overview ### Methods
| `procedure PrepareCore(const BaseLights: TAbstractLightInstancesList; const GravityUp: TVector3Single; const DoProgress: boolean); override;`
| `procedure ReleaseCore; override;`
| `function ItemClass: TInventoryItemClass; virtual;`
| `constructor Create(const AName: string); override;`
| `destructor Destroy; override;`
| `procedure LoadFromFile(ResourceConfig: TCastleConfig); override;`
| `procedure GLContextClose; override;`
| `function Image: TEncodedImage;`
| `function GLImage: TGLImage;`
| `function CreateItem(const AQuantity: Cardinal): TInventoryItem;`
| `procedure InstantiatePlaceholder(World: T3DWorld; const APosition, ADirection: TVector3Single; const NumberPresent: boolean; const Number: Int64); override;`
| `function AlwaysPrepared: boolean; override;`
### Properties
| `property Caption: string read FCaption;`
| `property BaseAnimation: T3DResourceAnimation read FBaseAnimation;`
| `property ImageURL: string read FImageURL;`
| `property BoundingBoxRotated: TBox3D read FBoundingBoxRotated;`
## Description ### Methods
| `procedure PrepareCore(const BaseLights: TAbstractLightInstancesList; const GravityUp: TVector3Single; const DoProgress: boolean); override;`
| `procedure ReleaseCore; override;`
| `function ItemClass: TInventoryItemClass; virtual;`
Which TInventoryItem descendant to create when constructing item of this resource by CreateItem.
| `constructor Create(const AName: string); override;`
| `destructor Destroy; override;`
| `procedure LoadFromFile(ResourceConfig: TCastleConfig); override;`
| `procedure GLContextClose; override;`
| `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 [http://castle-engine.sourceforge.net/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.
| `function GLImage: TGLImage;`
OpenGL resource to draw Image.
| `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 [http://castle-engine.sourceforge.net/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, Vector3Single(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.
| `procedure InstantiatePlaceholder(World: T3DWorld; const APosition, ADirection: TVector3Single; const NumberPresent: boolean; const Number: Int64); override;`
Instantiate placeholder by create new item with CreateItem and putting it on level with TInventoryItem.PutOnWorld.
| `function AlwaysPrepared: boolean; override;`
### Properties
| `property Caption: string read FCaption;`
Nice caption to display.
| `property BaseAnimation: T3DResourceAnimation read FBaseAnimation;`
| `property ImageURL: string read FImageURL;`
| `property BoundingBoxRotated: TBox3D read FBoundingBoxRotated;`
The largest possible bounding box of the 3D item, taking into account that actual item 3D model will be rotated when placed on world. You usually want to add current item position to this.
Generated by PasDoc 0.13.0 on 2015-06-15 04:43:10