Skip to content

[Suggestion] Wordpress-like filters #53

@albertorm1

Description

@albertorm1

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
end

Bonus function in business script:

lib.filters.add("myFramework:paycheck", function(citizenId, baseSalary)
    local bonus = GetBonusForCitizenId(citizenId)
    return baseSalary + bonus
end

Taxes function in another resource:

lib.filters.add("myFramework:paycheck", function(citizenId, baseSalary)
    local taxes = CalculateTaxesForCitizenId(citizenId)
    return baseSalary - taxes
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions