Currencies gives characters and containers money in any number of currencies.
A currency is registered with Currencies:register_currency. A character gets a Currency
record with a balance for every registered currency when it is created, and containers hold
money as well. Balances are read and changed through the Entity extensions, such as
Entity:get_money, Entity:give_money and Entity:give_money_to. Players give money to
what they are looking at, drop it as an fl_money entity, and move it in and out of
containers with the money panel next to their inventory.
One currency is the default one: the money commands use it when no currency is named, and
so can any other plugin, by calling Currencies:get_default_currency. It is named by the
default_currency config; while that config is not set, the only registered currency is
the default one.
A new character starts with the starting_amount of every currency, which is 0 unless the
currency is registered with another one. The starting_money config replaces it for the
default currency, and the GetStartingMoney hook has the last word.
Money that lies in the world is kept over restarts while the save_dropped_money config is
on: it is saved for every map whenever the framework saves its data, and put back when the
map is loaded again.
Hooks decide what is allowed: CanContainMoney names the entities that can hold money,
CanPlayerTransferMoney, CanGiveMoney, CanPlayerDropMoney and CanPlayerPickupMoney
can refuse to move it, PlayerPickupMoney can refuse a pickup, AdjustReceivedMoney
changes or refuses the money that an entity is given, EntityMoneyReceived reports the
money it was given and EntityMoneyChanged reports every change of a balance.
Summary
Hooks
Methods
default_currency config if it is registered, otherwise the only registered currency.model_table of the currency that the amount reaches, or else its model.starting_amount the currency is registered with, or the starting_money config for the default currency if that config is above 0.Currencies:save_money has saved for the current map, each entity exactly where it was, and freezes what was frozen.Hooks
Implements hook CanContainMoney
Allows players that are not bots and have a character loaded to hold money.
Parameters
- object Entity
Returns
- Boolean true for valid non-bot players with a loaded character, otherwise nil
Currencies:CanGiveMoney(actor, target, amount, currency)
server plugins/currencies/plugin/sv_hooks.lua:202Implements hook CanGiveMoney
Checks that the giver may transfer the money and that the target is valid, is not the giver, is able to contain money and is within 120 units of the giver's eyes.
Parameters
- actor Entity the entity giving the money, normally a player
- target Entity the receiver
- amount Number
- currency String currency ID
Returns
- Boolean false when not allowed
- String error phrase; nothing when allowed
Currencies:CanPlayerDropMoney(actor, amount, currency, pos, _trace)
server plugins/currencies/plugin/sv_hooks.lua:163Implements hook CanPlayerDropMoney
Checks that the player may transfer the money, that the drop position is within 120 units of their eyes and that they are neither on pickup cooldown nor have dropped money less than half a second ago.
Parameters
- actor Player
- amount Number
- currency String currency ID
- pos Vector position the money would be dropped at
- _trace Map eye trace result of the player
Returns
- Boolean false when not allowed
- String error phrase if there is one; nothing when allowed
Implements hook CanPlayerPickupMoney
Blocks money pickup while the player or the money entity is on its pickup cooldown.
Parameters
- actor Player
- entity Entity the fl_money entity
Returns
- Boolean false while on cooldown, otherwise nil
Currencies:CanPlayerTransferMoney(actor, amount, currency)
server plugins/currencies/plugin/sv_hooks.lua:133Implements hook CanPlayerTransferMoney
Checks that the amount is a positive, finite number with no more decimals than the currency has (a whole number for a currency without decimals), that the currency exists and that the entity can afford it. An amount with too many decimals is refused rather than rounded, so that both sides of a transfer always change by exactly the same sum.
Parameters
- actor Entity the entity that parts with the money, a player or a container
- amount Number
- currency String currency ID
Returns
- Boolean false when not allowed
- String error phrase; nothing when allowed
Implements hook CreatePlayerInteractions
Adds a money submenu to the player interaction menu with a 'give' option for every currency that is not hidden or that the local player owns.
Parameters
- menu Panel the interaction menu
- target Player the player being interacted with
Implements hook LoadData
Puts the saved money back into the world when the framework loads its data, if the save_dropped_money config is on.
Currencies:OnActiveCharacterCleared(owner, _character)
server plugins/currencies/plugin/sv_hooks.lua:105Implements hook OnActiveCharacterCleared
Clears the networked currency balances of a player who drops their character.
Parameters
- owner Player
- _character Character the character that is being dropped
Implements hook OnActiveCharacterSet
Networks the currency balances of the newly active character to the player entity.
Parameters
- owner Player
- character Character
Implements hook OnContainerOpened
Creates or refreshes the money panel below an opened container's inventory. Inventories that have an instance_id are skipped.
Parameters
- panel Panel the container panel
- inventory_id Number
Implements hook OnInventoryRebuild
Creates or refreshes the money panel that sits next to the 'pockets' inventory panel.
Parameters
- panel Panel the inventory panel that was rebuilt
Currencies:PostCreateCharacter(owner, char, char_data)
server plugins/currencies/plugin/sv_hooks.lua:38Implements hook PostCreateCharacter
Adds a Currency record for every registered currency to a new character, with the starting amount of that currency as its balance. Runs the GetStartingMoney hook for every currency.
Parameters
- owner Player
- char Character the character being created
- char_data Map character creation data
Implements hook PreContainerOpen
Gives a container zero balances for every currency if it has none yet and networks its balances.
Parameters
- entity Entity the container
Implements hook SaveData
Saves the money that lies in the world when the framework saves its data, if the save_dropped_money config is on. Deletes what has been saved for the map otherwise, so that money which is long gone does not come back once the config is turned on again.
Methods
Returns every registered currency.
Returns
- Map currency definitions keyed by currency ID
Creates a panel that lists the money of an entity and keeps track of it so that it is rebuilt when the server reports a change. Client only.
Parameters
- entity Entity entity whose money is shown
- parent Panel
Returns
- Panel the created fl_currencies panel
Returns the definition of a registered currency.
Parameters
- id String currency ID, letter case is ignored
Returns
- Map currency definition, or nil if it is not registered
Returns the ID of the default currency: the one named by the default_currency config
if it is registered, otherwise the only registered currency.
local currency = Currencies:get_default_currency()
if currency and actor:has_money(currency, 50) then
actor:take_money(currency, 50)
end
Returns
- String currency ID in lower case; nil if the config does not name a registered currency while none or more than one are registered
Returns the model that an amount of a currency has while it lies in the world: the
model of the largest threshold in the model_table of the currency that the amount
reaches, or else its model.
Parameters
- id String currency ID, letter case is ignored
- amount Number
Returns
- String model path; a small box if the currency has no model for the amount, nil if the currency is not registered
Returns how much of a currency a new character starts with: the starting_amount the
currency is registered with, or the starting_money config for the default currency if
that config is above 0. The GetStartingMoney hook may still change the amount for a
particular character.
Parameters
- id String currency ID, letter case is ignored
Returns
- Number amount, rounded to the decimals of the currency; 0 if the currency is not registered
Spawns the money that Currencies:save_money has saved for the current map, each
entity exactly where it was, and freezes what was frozen. Entries of currencies that are
not registered are skipped. Server only.
Gives the contents of a money entity to a player, notifies them, starts their pickup cooldown and marks the saved money as out of date. The entity is not removed here. Server only.
Parameters
- actor Player
- entity Entity the fl_money entity
Returns
- Boolean true if the player has received the money; false if the entity holds no registered currency or the AdjustReceivedMoney hook has refused the money, in which case the money stays where it is
Saves the money that lies in the world a second from now, if the save_dropped_money config is on. Calls that come within that second are saved together. Server only.
Registers a currency. Characters created afterward get a balance record for it.
Currencies:register_currency('tokens', {
name = 'currency.tokens.name',
symbol = 'T',
decimals = 0,
hidden = false,
-- How much of it a new character starts with.
starting_amount = 100,
-- Model of dropped money, and optionally models for larger amounts.
model = 'models/props_lab/box01a.mdl',
model_table = { [500] = 'models/props_c17/briefcase001a.mdl' }
})
Parameters
- id String currency ID; use lower case, lookups lower-case the ID they are given
- data Map currency definition: name, symbol, decimals, hidden, starting_amount, model, model_table
Returns the ID of the currency that a player has named, for instance as the argument of a command, or the ID of the default currency if they have named none.
Parameters
- id String optional, defaults to
nilcurrency ID as it was typed, letter case is ignored
Returns
- String currency ID in lower case, or nil if the named currency is not registered, or if none is named and there is no default currency
See also
Saves the currency, amount, position and angles of every fl_money entity in the world,
and whether it is frozen, to the plugin data of the current schema and map. Entities that
are being removed are left out, so it can be called right after Entity:Remove. The
plugin calls it whenever the framework saves its data, while the save_dropped_money
config is on. Server only.
Currencies:spawn_money(currency, amount, position, angles)
server plugins/currencies/plugin/sv_plugin.lua:57Puts an amount of a currency into the world as an fl_money entity, with the model that
the currency has for that amount. Nobody is charged for it: Entity:drop_money is what
takes the money out of the pocket of a player. Server only.
local trace = actor:GetEyeTraceNoCursor()
local money_ent = Currencies:spawn_money('tokens', 50, trace.HitPos)
Parameters
- currency String currency ID, letter case is ignored
- amount Number amount the entity holds, has to be above 0
- position Vector where to put the money; it is raised by the height of its bounds
- angles Angle optional, defaults to
nil
Returns
- Entity the fl_money entity; nil if the currency is not registered, the amount or the position is invalid, or the entity could not be created