🧢XTASK.trigger

The actual code that will be ran to track the achievement.

Will be deprecated in the future. Replaced with:

  • XTASK.onShared

  • XTASK.onClient

  • XTASK.onServer

▸ Data Type

function

▸ Description

The trigger is a vital part to an achievement. It is the code that will be ran and determines what is tracked for a specific achievement.

To make your own, you must know Lua.

Triggers look like the following:

XTASK.trigger = function( )
    // your code here
end

Typically the trigger function will be used in combination with Garry's Mod hooks built into the game to track certain things.

As an example, to track when a player spawns on the map; you can use the following:

XTASK.trigger = function( )
    hook.Add( 'PlayerSpawn', pf .. XTASK.title, function( ply )
        local i = calc.players( )
        if i >= XTASK.param_extra then
            ply:ach_execute( mod:ach_id( XTASK.title ) )
        end
    end )
end

The above code triggers when a player spawns onto the map. It counts how many players are currently on the server and determines if you should earn the achievement if enough players are connected.

This should be ran on player spawn because the number of players on a server changes constantly, and you do not want the server always running the check due to it being taxing on performance. So we use a player's spawning as a time to check to see if they meet the requirements.

The trigger function provided above goes to the achievement Who Keeps Giving Birth?

XTASK                   = { }
XTASK.enabled           = true
XTASK.maxreq            = 1
XTASK.param_extra       = 60
XTASK.po_enabled        = true
XTASK.po                = { items = { }, currency = { points = 0, points_prem = 0, darkrpfunds = 75 } }
XTASK.gamemode          = { 'darkrp', 'hogwartsrp', 'starwarsrp', 'halorp', 'militaryrp' }
XTASK.title             = 'Who Keeps Giving Birth?'
XTASK.desc              = sf( 'Play on the server when there\'s %s or more people. (Bots don\'t count)', tostring( XTASK.param_extra ) )
XTASK.cat               = 'Population'
XTASK.author            = 'Richard'
XTASK.icon              = 'rlib/modules/xtask/ach/whos_giving_birth.png'
XTASK.stat              = nil

XTASK.trigger = function( )
    hook.Add( 'PlayerSpawn', pf .. XTASK.title, function( ply )
        local i = calc.players( )
        if i >= XTASK.param_extra then
            ply:ach_execute( mod:ach_id( XTASK.title ) )
        end
    end )
end

On a previous page, we explained what XTASK.maxreq does; which is determine how many times an action must take place before the player earns the achievement. In the above code, we only need it to occur once.

However, there's an addtitional requirement specified by XTASK.param_extra which tells the code above that there must be 60 players on the server in order to obtain the achievement.

Last updated