Entities
Entities are the core building blocks of TPNRP Core. They encapsulate game objects with state and behavior.
Entity Pattern
Entities follow a class-like pattern using Lua metatables:
MyEntity = {}
MyEntity.__index = MyEntity
function MyEntity.new(...)
local self = setmetatable({}, MyEntity)
-- Initialize properties
-- Define methods
return self
end
Client Entities
CPlayer
Represents a player on the client-side.
Properties:
playerSource: The player sourcelicense: Player license identifierplayerData: Player data received from serverinventories: Inventory reference
Methods:
getCoords(): Get player coordinatesgetHeading(): Get player heading
Usage:
local player = CPlayer.new(source, license)
local coords = player:getCoords()
Server Entities
SPlayer
Manages player state and data on the server.
Properties:
playerSource: The player sourceplayerData: Player datainventory: Inventory entityequipment: Equipment entity
Methods:
save(): Save player data to databasegetCoords(): Get player coordinatesgetHeading(): Get player headingupdatePlayerData(): Sync player data to clientlogin(): Handle player loginlogout(): Handle player logout
Usage:
local player = SPlayer.new(source)
player:login()
player:save()
SInventory
Manages player inventory on the server.
Properties:
owner: Owner entity (usually SPlayer)- Inventory state and items
Methods:
load(type): Load inventory by typesave(): Save inventory to database- Inventory manipulation methods (add, remove, etc.)
SEquipment
Manages player equipment and clothing.
Properties:
owner: Owner entity (usually SPlayer)- Equipment state
Methods:
save(): Save equipment to database- Equipment manipulation methods
Entity Lifecycle
- Creation:
Entity.new(...)creates a new instance - Initialization: Constructor sets up initial state
- Usage: Methods interact with entity
- Persistence:
save()methods persist state - Cleanup: Entity is garbage collected when no longer referenced
Best Practices
- Single Responsibility: Each entity should have one clear purpose
- Encapsulation: Keep entity state internal, expose through methods
- Type Annotations: Use type annotations for better IDE support
- Error Handling: Validate inputs and handle errors gracefully