Skip to content

Microgames Development

Robert A Fraser edited this page Mar 13, 2020 · 1 revision

Introduction

Microgames modifiers are a nice way to learn the very basics about how game mode design works. Let's take a look at a simple example:

-- Crowbar Wars
-- Simple deathmatch with crowbars
MOD = {
    name = 'Crowbar Wars',
    subtext = 'The red colour hides the blood',
    func_player = function(ply)
        ply:Give('weapon_crowbar')
    end,
}

Each modifier is defined as a table named MOD. In this table, we specify the name of the gamemode, the subtext, and some other additional properties.

Each game mode is made up of various functions - for example, func_player is a function which is called for each player in the game and can be used to do stuff such as assign weapons.

Properties List

Property Description
name Title of the modifier.
subtext Subtitle of the modifier.
time Duration of this modifier, in seconds. Gamemode default is 20.
maps A whitelist of maps that this modifier can run on.
hooks A table of additional gamemode hooks to register with this modifier. Allows any Garry's Mod or Minigames hook to be used by a modifier - this is where some nice complexity can be added.
func_init Function called at the start of the modifier. Has no parameters. Can be used to spawn entities, etc.
func_player Called at the start of the modifier. Has each player passed through as a parameter. Can be used to equip players, etc.
func_check Function called for each player right before a winner is identified. This is often used to assign points to players.
func_cleanup Similar to func_init, but called at the end of the modifier. The map is automatically cleaned up, but anything else that may need removal can be done here.
func_finish Similar to func_player, but called at the end of the modifier. Can be used to cleanup any weird player properties.

Complex Example

-- Crate Time
-- Players must break a crate or lose
MOD = {
    name = 'Crate Time',
    subtext = 'Break a crate OR DIE',
    time = 10,

    -- Spawn a random number of crates
    func_init = function()
        local spawns = table.Shuffle(ents.FindByClass('marker_sky'))
        local number = math.Clamp(player.GetCount() + math.random(-1, 3), 3, #spawns)
        for i=1,number do
            local crate = ents.Create('prop_physics')
            crate:SetPos(spawns[i]:GetPos() + Vector(0, 0, 32))
            crate:SetModel('models/props_junk/wood_crate001a.mdl')
            crate:Spawn()
        end
    end,
    
    -- Players get a crowbar
    func_player = function(ply)
        ply:Give('weapon_crowbar')
        ply.BrokeCrate = false
    end,
    
    -- Players win if and only if they have broken a crate
    func_check = function(ply)
        if not ply.BrokeCrate then
            if not ply.Spectating and ply:Alive() then ply:Kill() end
        elseif ply:Alive() and not ply.Spectating then
            ply:AddFrags(2)
        end
        ply.BrokeCrate = nil
    end,
    
    -- Check for crate breaking
    hooks = {
        EntityTakeDamage = function(ent, dmg)
            if ent:IsPlayer() then return true end
        end,
        
        PropBreak = function(ply)
            ply.BrokeCrate = true
        end,
    }
}