Areas library
Part of Areas API
Areas are named regions of the map, made of one or more polygons with a floor and a
height, that the server tracks players entering and leaving.
Every area has a type registered with Areas.register_type. The type's callback runs
when a player enters or leaves an area of that type, on the server and on that player's
client. An area is built with Areas.create and its add_vertex and register
methods, or drawn in game with the Area Tool. Registered areas are sent to all clients,
and the Areas API plugin saves them.
Summary
Functions
Functions
Returns all registered areas.
Returns
- Map area tables keyed by area id
Creates an area builder, or fetches the already registered area with that id. The returned table gains add_vertex(vect), finish_poly() and register() methods. The first vertex of a polygon sets the floor height, the following ones are flattened to it. finish_poly() closes the current polygon and starts a new one. register() closes the current polygon and passes the area to Areas.register.
local area = Areas.create('town_square', 512, { type = 'textarea' })
area.text = 'Town Square'
area:add_vertex(Vector(0, 0, 0))
area:add_vertex(Vector(512, 0, 0))
area:add_vertex(Vector(512, 512, 0))
-- Optional: start another polygon within the same area.
area:finish_poly()
area:add_vertex(Vector(1024, 0, 0))
area:add_vertex(Vector(1536, 0, 0))
area:add_vertex(Vector(1536, 512, 0))
area:register()
Parameters
- id String unique area id
- height Number optional, defaults to
0height of the area above its first vertex - data Map optional, defaults to
nilextra fields to merge into a new area, e.g. { type = 'textarea' }. The type defaults to 'area'
Returns
- Map the area table with the builder methods
See also
Returns all registered areas of the specified type.
Parameters
- type_id String area type id
Returns
- List list of area tables
Returns the enter / leave callback of the specified area type.
Parameters
- area_type String area type id
Returns
- Function the callback set with Areas.set_callback, else the default callback of the type, else a stub that prints a developer warning
Returns the callbacks that were set with Areas.set_callback.
Returns
- Map callback functions keyed by area type id
Returns the color that is used to draw areas of the specified type in the area tool.
Parameters
- type_id String area type id
Returns
- Color the color of the type, or nil if the type is not registered
Returns how many times an area has been registered through Areas.register. The counter is not decreased when an area is removed.
Returns
- Number
Returns all registered area types.
Returns
- Map type tables (name, description, callback, color) keyed by type id
Stores the area under the given id, stripping any functions from it. When called on the server the area is also sent to all clients.
Parameters
- id String unique area id
- data Map area table as built by Areas.create; needs at least one polygon
Returns
- Map the stored area, or nil if an argument is missing or the area has no polygons
See also
Areas.register_type(id, name, description, color, default_callback)
shared plugins/areas/plugin/lib/sh_areas.lua:223Registers an area type along with the callback that runs when a player enters or leaves an area of that type. The callback runs on the server for every player and on the client for the local player.
Areas.register_type(
'safezone',
'Safe Zone',
'Notifies other plugins when a player enters or leaves the zone.',
Color(0, 255, 0),
function(actor, area, has_entered, pos, cur_time)
if has_entered then
hook.Run('PlayerEnteredSafeZone', actor, area, cur_time)
else
hook.Run('PlayerLeftSafeZone', actor, area, cur_time)
end
end
)
Parameters
- id String unique type id
- name String human-readable name of the type
- description String
- color Color optional, defaults to
Color(255, 0, 255)the color of the areas in the area tool - default_callback Function optional, defaults to
nilcalled as callback(actor, area, has_entered, pos, cur_time) unless overridden with Areas.set_callback
See also
Removes the area with the given id. When called on the server the removal is also sent to all clients.
Parameters
- id String area id
Overrides the enter / leave callback for the specified area type. The callback takes priority over the default callback of the type.
Parameters
- area_type String area type id
- callback Function called as callback(actor, area, has_entered, pos, cur_time)
See also
Replaces all registered areas at once. Does not network anything.
Parameters
- stored_table Map area tables keyed by area id; any non-table value clears the areas