-
-
Notifications
You must be signed in to change notification settings - Fork 92
Description
It has happened to me multiple times that, when working with custom resources, sometimes I have to modify a part of another resource in order to integrate them. While some of these integrations are easily achievable using events or exports, I miss a feature similar to the Wordpress filters.
If you're not familiar with Wordpress, filters are similar to events, but allow you to return modified data. You can find some documentation here: https://developer.wordpress.org/plugins/hooks/
Ideally, they could be chained, so multiple resources could modify the data using the same filter.
Example
Let's say I want multiple resources to modify the paycheck of a player, for example to give them a bonus or to apply taxes. Then, every time the paycheck function is executed, it could call a filter. Then, my custom resources could modify the salary. The code using filters would look something like this:
Framework:
function processPaycheck(citizenId, baseSalary)
local totalSalary = lib.filters.trigger("myFramework:paycheck", citizenId, baseSalary)
-- Operate now with the total Salary
endBonus function in business script:
lib.filters.add("myFramework:paycheck", function(citizenId, baseSalary)
local bonus = GetBonusForCitizenId(citizenId)
return baseSalary + bonus
endTaxes function in another resource:
lib.filters.add("myFramework:paycheck", function(citizenId, baseSalary)
local taxes = CalculateTaxesForCitizenId(citizenId)
return baseSalary - taxes
end