items

Items are one of the most simple parts to CityRP 2, yet one of the widest ranging, due to their use within every sector of the gamemode, and constant development.

Types of Items

Items currently fall into 3 distinct categories.

| Item Type | Status | | --- | --- | | Table | Deprecated, Majority of Items | | Parented Table | Deprecated, Mostly Vehicles | | Item Class | Active, Development |

Table Items

Table items are the simplest, being mearly a table and a call to item.register.

local ITEM = {}
ITEM.name = "My Special Item"
ITEM.cost = 999
ITEM.description = [[Stuff and things are done by my super cool item.]]
ITEM.uniqueID = "specialitem"

cityrp.item.register(ITEM)

Whilst the simplest, these are also often the largest, if code is repeated, due to the fact that elements must be duplicated in their entirety. For example, for the suit items, even though there may be 10 or more suits with the same price, they must be repeated each time.

Table Items with Inherence

Similar to the table items, these items simple pass a table to cityrp.item.register. However, passing a second argument to register will cause it to take that already-registered item, and use it as a parent.

if not cityrp.plugin.get("Vehicles") then return end

local ITEM = {}
ITEM.name = "1981 Ford F150"
ITEM.cost = 160000
ITEM.model = "models/talonvehicles/tal_f150_1981.mdl"
ITEM.uniqueID = "81fordf150"

cityrp.item.register(ITEM, "base_vehicle")

This means that utility functions and shared information can stay on the base, reducing the duplication between files.

However, this has the downside of requiring that each base item is registered, and thus may accidentally become available to end users.

Class-Based Items.

To combat this, the alternative is class-based items. These use a completely different style to tabular items, can contain multiple items in the same file with ease, and can base themselves without registration.

local BASE = ITEM:New("base_accessory")
    :SetCost(10000)
    :SetModel("models/modified/backpack_2.mdl", 1, true)
    :SetCategory("Accessories")
    :SetDescription([[A modern camping backpack to carry all your gear.]])
    :SetOffEmote("takes off their backpack.")

BASE:Clone()
    :SetName("Navy and Tangerine Camping Backpack")
    :SetOnEmote("puts on their navy and tangerine camping backpack.")
    :Register("backpacknavy_2")

BASE:Clone()
    :SetName("Olive and Blue Camping Backpack")
    :SetSkin(2, true)
    :SetOnEmote("puts on their olive and blue camping backpack.")
    :Register("backpackolive_2")

BASE:Clone()
    :SetName("Red, White and Green Camping Backpack")
    :SetSkin(3, true)
    :SetOnEmote("puts on their red, white and green camping backpack.")
    :Register("backpackred_2")

In the above example, the base item is the accessory base, but the camping backpacks have data which is shared between each element, which is also put into the base. The base is then cloned, data updated, and then only then is it registered.