Entity class
Part of Currencies
Entity extensions of the Currencies plugin: the money an entity holds.
The balances of an entity are networked, so they can be read on the server and the client;
on the server they can be set, given, taken, dropped and moved to another entity. The
balance of a player belongs to their active character and is stored in its Currency
records, while other entities, such as containers, keep theirs on the entity.
Money that is added with Entity:give_money passes through the AdjustReceivedMoney hook,
which can change the amount or refuse it, whatever the money comes from: another player,
a container, money picked up from the ground or a plugin. Entity:set_money sets a
balance directly and does not run that hook.
Summary
Methods
Methods
Checks whether the entity is able to hold money by running the CanContainMoney hook.
Returns
- Boolean true when a hook allows it, otherwise nil
Drops money from a player as an fl_money entity where they are looking, at most 120 units away; if they look at another player the money is given to that player. Server only.
local success, err = actor:drop_money('tokens', 50)
if success == false then
actor:notify(err)
end
Parameters
- currency String currency ID
- value Number amount to drop; by default it has to be positive and may not have more decimals than the currency
Returns
- Boolean false when the drop is refused
- String error phrase; nothing otherwise
Returns how much of a currency the entity holds.
Parameters
- currency String currency ID
Returns
- Number amount, 0 if the currency is not registered
Entity:give_money(currency, value, source)
server plugins/currencies/plugin/lib/meta/sh_entity.lua:217Adds money to the entity. The amount is rounded to the currency's decimals before it is added, as in take_money. An amount above 0 first goes through the AdjustReceivedMoney hook, which can change or refuse it, and is reported with the EntityMoneyReceived hook once it has been added. Server only.
local received = target:give_money('tokens', 50, 'reward')
if received == false then
actor:notify('error.money_refused')
end
Parameters
- currency String currency ID
- value Number amount to add
- source Any optional, defaults to
nilwhat the money comes from, for the hooks: the entity that gives it (a player, a container or the fl_money entity that is picked up), or a string that names the reason
Returns
- Number the amount that was added, which a hook may have changed, 0 if the currency is not registered; false if a hook has refused the money
Entity:give_money_to(target, currency, value)
server plugins/currencies/plugin/lib/meta/sh_entity.lua:310Moves money from this entity to another one if the CanGiveMoney hook allows it, and notifies the players involved. The AdjustReceivedMoney hook is run for the receiver before anything is moved, so it can lower what arrives or refuse the transfer; the giver is then charged the full amount and the receiver gets what the hook has left, which is reported with the EntityMoneyReceived hook. Server only.
local success, err = actor:give_money_to(target, 'tokens', 50)
if success == false then
actor:notify(err)
end
Parameters
- target Entity optional, defaults to
nilreceiver; a player gives to the entity they look at when nil - currency String currency ID
- value Number amount to move; by default it has to be positive and may not have more decimals than the currency
Returns
- Boolean false when the transfer is refused
- String error phrase; nothing otherwise
Checks whether the entity holds at least the given amount of a currency.
Parameters
- currency String currency ID
- value Number
Returns
- Boolean
Sets how much of a currency the entity holds, rounded to the currency's decimals and
never below 0, networks it and runs the EntityMoneyChanged hook. A character that was
created before the currency was registered gets its Currency record here, as long
as the ID is spelled the way the currency is registered. Server only.
Parameters
- currency String currency ID; nothing happens if it is not registered
- value Number new amount
Removes money from the entity; the balance stops at 0. The amount is rounded to the currency's decimals before it is subtracted, so that taking an amount from one entity and giving the same amount to another always moves the same sum. Server only.
if actor:has_money('tokens', 50) then
actor:take_money('tokens', 50)
end
Parameters
- currency String currency ID
- value Number amount to remove