Making weapons
Make any weapon as a Plugin, from a pack of records with its own flash, tracer and casings and no code, to a C# behaviour for a gun that does something new.
A weapon in Evil Engine is three things: a weapon record, the content it names, and a behaviour that decides what it does. The record and the content are data a Plugin gives in Records/ and Content/. The behaviour is code, either one a Plugin registered by name that your record picks and tunes, or your own.
Everything a weapon looks and sounds like is data: its view model, sounds, muzzle flash, impacts, tracers and spent casings. Every player’s game draws them from your records, so a weapon looks the same to everyone. Code only decides when they happen.
Garry’s Mod’s Half-Life 2 weapons are made the same way, through the same API, so nothing they do is out of your reach.
A weapon with no code
The engine ships evil.weapons, a Plugin of generic behaviours: a firearm, a launcher, a melee weapon and a beam. Name one in your weapon record and tune it, and your weapon fires. This is a whole Plugin, a folder in user://plugins/ with a plugin.toml and one record file:
id = "pulse"
name = "Pulse rifle"
version = "1.0.0"
api = 1
[[mounts]]
game = "gmod"
[weapon."weapon/pulse"]
title = "Pulse Rifle"
slot = 2
viewmodel = "pulse:viewmodel/pulse"
behaviour = "evil.weapons:firearm"
clip_size = 30
default_clip = 90
ammo = "gmod:ammo/ar2"
damage = 15
sounds = { fire = "gmod:weapon_ar2.single", empty = "gmod:weapon_pistol.empty", reload = "gmod:weapon_ar2.reload" }
effects = { muzzle = "pulse:effect/flash", tracer = "pulse:tracer/pulse", impact = "pulse:effect/hit", casing = "pulse:casing/cell" }
[weapon."weapon/pulse".tuning]
fire = "burst"
burst = 3
refire = 0.4
spread = 0.6
recoil = { pitch = -2.5, yaw = 1.5 }
draw_rate = 4
animations = { draw = "@ACT_VM_DRAW", fire = "@ACT_VM_PRIMARYATTACK", reload = "@ACT_VM_RELOAD" }
[viewmodel."viewmodel/pulse"]
fov = 41.83
parts = [
{ id = "weapon", model = "gmod:models/weapons/c_irifle", angles = [0, 180, 0], idle = "@ACT_VM_IDLE" },
{ id = "hands", hands = true, parent = "weapon", merge = true },
]
[effect."effect/flash"]
light = { colour = "#40a0ff", energy = 5, range = 5, time = 0.07, fade = 0.04 }
glow = { colour = "#60c0ff", size = 0.45, grow = 2, life = 0.07, strength = 2.2 }
[effect."effect/hit"]
glow = { colour = "#80d0ff", size = 0.25, grow = 3, life = 0.15, strength = 2, fade = 0.12 }
[tracer."tracer/pulse"]
colour = "#40b0ff"
width = 0.09
length = 4
speed = 220
[casing."casing/cell"]
model = "gmod:models/weapons/rifleshell"
speed = [2, 3]
bounce = "gmod:bounce.rifleshell"
That’s a three-round burst rifle on Garry’s Mod’s AR2 model, with a blue flash, blue tracers and casings of its own. Give it to yourself from the console to try it:
give pulse:weapon/pulse
A player class’s kit gives it too, as { give = "pulse:weapon/pulse" }. A weapon record whose behaviour a loaded Plugin registered is a registered weapon, so kits hand it out like any other.
Every number in a hand-written record is in metres, seconds, degrees and metres a second, and every colour is #rrggbb or #rrggbbaa. How the record shows on the wheel, its title, slot, order and icons, is the weapon wheel’s.
Your own models
Export a model from Blender as a binary glTF, .glb, into your Plugin’s Content/models/, and it’s a model by its path: Content/models/blaster.glb is blaster:models/blaster in a Plugin whose id is blaster. Nothing else is needed:
- Its Principled materials draw with their base colour and base colour texture, their alpha mode (opaque, alpha clip with its threshold, or blended) and whether backface culling is off. Normal, metallic, roughness and emission maps aren’t drawn yet.
- Every empty is an attachment by its name, wherever it’s parented, a bone included. Name one
muzzleand oneejectand your flash, tracers and casings come from them. - Every action is an animation by its name in lower case, so a
fireaction plays when the behaviour playsfire, and an event can sit on it withsequence = "fire". Only an action namedidleor ending in_idleloops. Animations play on an armature, so a gun whose parts move needs one, with its meshes skinned to it. - glTF faces +Z and the view looks down -Z, so a view model part turns it with
angles = [0, 180, 0]and places it withoffset, in metres from the eye:
[viewmodel."viewmodel/blaster"]
parts = [{ id = "gun", model = "blaster:models/blaster", offset = [0.12, -0.12, -0.32], angles = [0, 180, 0], idle = "idle" }]
A view model with no hands draws only your gun. Add { id = "hands", hands = true, parent = "gun", merge = true } to put the player’s own arms on it, which takes bones named as the playermodel’s arms are.
In other people’s hands
Everyone else sees your weapon as its worldmodel in the holder’s hand, and so do you in third person. A model that shares bones with the holder’s body by name, as Garry’s Mod’s w_ models share ValveBiped.Bip01_R_Hand, is merged onto them and sits as its game holds it. Any other sits on a bone of the body, placed by a table:
worldmodel = { model = "blaster:models/blaster", bone = "RightHand", offset = [0.02, 0.08, 0.03], angles = [0, 0, 90] }
bone is a humanoid bone’s name (RightHand, LeftHand, Chest) or one of the body’s own, RightHand by default. offset is in metres and angles in degrees in that bone’s own space, which differs from rig to rig, so place a glb on the bodies your players wear. skin and body pick its skin and body groups. In third person the muzzle flash, tracers, casings and the model’s events come from the world model’s attachments by the same names as the view model’s, or from the hand when it has none.
hold is how the body stands, walks, runs, crouches, jumps and swims while it’s held, and which attack and reload gestures it plays. Every Garry’s Mod person has Garry’s Mod’s eighteen: pistol, smg, grenade, ar2, shotgun, rpg, physgun, crossbow, melee, slam, fist, melee2, passive, knife, duel, camera, magic and revolver. A person of your own gives a hold its clips in a holds table, by the same slots as its motion, with gesture.attack and gesture.reload:
[person.agent.holds.blaster]
idle = "agent:anim/blaster_idle"
run = "agent:anim/blaster_run"
"gesture.attack" = "agent:anim/blaster_fire"
A slot a hold leaves out plays the person’s own, so a hold can be only an idle and a recoil. The engine’s behaviours play the attack gesture with each shot or swing and the reload gesture with each reload: evil.weapons stretches it to the reload’s length, and evil.source plays it at its own speed, as Garry’s Mod does. A behaviour of your own plays them with holder.Gesture, and changes the hold with holder.Hold.
The weapon record
Beyond the wheel’s keys, a weapon record holds:
| Key | What it is |
|---|---|
viewmodel |
The viewmodel record drawn in the holder’s hands in first person. |
worldmodel |
The model in the holder’s hand everyone else sees: a model id, or a table with model, bone, offset, angles, skin and body (above). |
hold |
The hold type the holder’s body takes while it’s held (above). |
behaviour |
The behaviour that makes it do something, by id. |
tuning |
A table the behaviour reads. Its keys are the behaviour’s own. |
clip_size, default_clip |
Rounds in a clip, and what a new one starts with. Leave clip_size out to fire straight from the reserve. |
ammo, ammo2 |
The ammo types it and its second trigger use, by id. |
damage |
Damage per bullet or hit. |
sounds |
Named sounds the behaviour plays: fire, alt, empty, reload, draw, hit, miss, stop, and any of your own. |
effects |
Its looks: muzzle, impact and effect records, its tracer and its casing. |
attachments |
The attachments on its models the looks come from: muzzle and eject, which default to muzzle and eject. |
events |
Ties its model’s animation to its looks (below). |
modes |
Its alternate modes, each with a title, accent and its own viewmodel, worldmodel, hold, effects and attachments. |
base builds one weapon on another, and every key you leave out comes from the base, tuning merging key by key. So a Mk II is a record with a base and the three numbers it changes.
Looks
Effects
An effect record is a one-shot look played at a point or on an attachment: a muzzle flash, an impact, a charge-up. It’s made of layers, each a table or a list of them, each starting after its own delay in seconds.
| Layer | Keys |
|---|---|
light |
colour, energy, range in metres, time it lasts, fade over its last seconds. |
glow |
The engine’s soft glowing billboard: colour, size in metres, grow in metres a second, life, strength, fade. |
sprite |
A texture drawn facing the camera: texture, colour, size, grow, spin in degrees a second, life, additive (true by default, false to blend), fade. |
sound |
A sound id, or { id = "...", delay = 0.1 }. |
particles |
A particle system by its set and name, { system = "gmod:particles/water_impact#water_splash_01" }, a mounted game’s or your own. It starts after its delay where the effect plays, facing out of the surface it hit or along the attachment. |
base builds an effect on another, replacing only the layers it names. The engine’s own are evil:effect/muzzle and evil:effect/impact.
Impacts and splashes
Where a bullet lands, the first of these plays: the bullets’ own Impact, the weapon’s effects.impact, the struck surface’s bullet effect, then evil:effect/impact. So a weapon with no impact of its own throws up whatever each surface says, sparks off metal and dust off concrete, once a mount or a Plugin gives its surfaces effects:
[surface."surfaces/metal"]
effects = { bullet = "mygame:effect/sparks" }
[liquid.water.effects]
bullet = "mygame:effect/splash"
A shot that crosses a liquid whose effects name a bullet effect splashes where it crosses the surface, facing up whatever the shot’s angle. A behaviour’s own traces carry the struck surface’s effect as Impact, which evil.weapons’ melee and beam play when the weapon names none.
Bullet holes
Every bullet leaves a mark where it lands: the bullets’ own Decal, or the struck surface’s decals.bullet. A decal record is the pictures a mark is picked from, each a material and its rectangle on it, its size in metres, and the material it draws with on props and people:
[surface."surfaces/metal"]
decals = { bullet = "mygame:decal/dents" }
[decal."decal/dents"]
pick = [
{ material = "mygame:materials/dents", size = [0.15, 0.15], uv = [0.0, 0.0, 0.5, 1.0] },
{ material = "mygame:materials/dents", size = [0.15, 0.15], uv = [0.5, 0.0, 1.0, 1.0], model = "mygame:materials/dents_model" },
]
The engine lays it as Garry’s Mod lays its own: upright on walls, along +Z on floors, onto the faces round a corner, draped over terrain, and world-aligned on props and people, which carry it as they move. A melee behaviour marks what it struck with Impact, which plays the impact effect too and takes the surface’s decals.melee before its decals.bullet. A blast names its scorch as WeaponBlast.Decal. The world keeps 2,048 pieces and every model its last 50 marks; r_cleardecals clears them.
Tracers
| Key | What it is |
|---|---|
colour, strength |
Its colour and how bright. |
width, length |
Its size in metres. |
speed |
How fast it races to the hit, in metres a second. |
texture |
A texture drawn along it, in place of the engine’s hot streak. |
nearest |
No tracer for hits closer than this. |
Every key you leave out is evil:tracer/bullet’s. A firearm draws a tracer every tracer_every bullets.
Casings
| Key | What it is |
|---|---|
model, skin |
The casing’s model. |
speed |
How fast it leaves the port, one number or a low and a high. |
side, up |
How far it scatters sideways and upwards, in metres a second. |
spin |
How fast it tumbles, in degrees a second. |
size |
Its collision box, three sizes in metres. |
life, fade |
How long it lies, one number or a range, then how long it takes to shrink away. |
bounce, bounces |
The sound it makes landing, and how many landings sound. |
most |
How many of it lie at once before the oldest goes. |
Casings are small bodies that bounce off the world and props. Nothing collides with them.
Events
A weapon’s events list plays its looks at moments in its animation. Each entry matches either an event the model’s sequence carries, as Source’s models do, or a point in a sequence of your own:
events = [
{ sequence = "fire", cycle = 0.0, effect = "muzzle" },
{ sequence = "reload", cycle = 0.4, casing = "casing", at = "eject" },
{ sequence = "reload", cycle = 0.1, sound = "reload" },
{ on = "AE_MUZZLEFLASH", effect = "muzzle", at = "{2}" },
{ on = "6001", options = "2", casing = "gmod:casing/shotgun_shell", at = "1" },
]
Each plays one thing: an effect, a casing or a sound, by id or by one of the weapon’s own names (muzzle, impact and casing from its effects, or a sound from its sounds). at names the attachment, defaulting to the muzzle, or to the eject port for a casing. {1} to {9} stand for the model event’s own words, so Source’s events pass their attachment and sound names through.
A firearm flashes its muzzle on every shot unless its events already do, and ejects a casing on every shot unless its events already do.
The engine’s behaviours
evil.weapons gives four behaviours. Each reads only the keys below, and any other key in your tuning, or a value of the wrong kind, is noted once by your weapon and the key.
Every one of them reads these too:
| Key | Default | What it is |
|---|---|---|
speed |
1 | A scale on the holder’s movement while it’s held, 0.8 for a heavy gun. |
zoom |
A zoom on attack2: fov in degrees (20), time it takes to ease there in seconds (0.2), button, "toggle" on each press or "hold" while held, hide to hide the view model while zoomed, speed while zoomed (the weapon’s speed), and stack, a viewmodel record swapped in while zoomed. A firearm with an alt trigger keeps attack2 for that and doesn’t zoom. |
[weapon."weapon/marksman".tuning]
refire = 0.6
speed = 0.9
zoom = { fov = 20, hide = true, speed = 0.5 }
A zoom plays the weapon’s zoom sound each way, and a firearm’s reload ends it.
evil.weapons:firearm
| Key | Default | What it is |
|---|---|---|
fire |
"semi" |
"semi", one shot a press; "auto", firing while held; "burst", burst shots a press. |
refire |
0.1 | Seconds between shots, or between bursts. |
burst, burst_gap |
3, 0.06 | Shots in a burst and the seconds between them. |
rounds |
1 | Rounds a shot takes from the clip. 0 fires for free. |
pellets |
1 | Bullets a shot fires, or projectiles it launches, from 1 to 100. |
spread |
0 | The cone’s half angle in degrees. |
spread_step, spread_most, spread_rest |
0, spread, 0.5 |
Degrees the cone widens each quick shot, the widest it gets, and the seconds of rest that settle it. |
damage |
the record’s damage |
Damage per bullet. |
person_damage |
damage |
Damage per bullet to people, where it differs. |
force |
5 | The push each bullet gives what it hits, in kg·m/s. |
range |
1,441 | How far bullets reach, in metres. |
tracer_every |
2 | A tracer every so many bullets, none at 0. |
recoil |
0, 0 | { pitch, yaw } kick to the view, in degrees a second; yaw goes either way at random. |
animation, sound |
The animation and sound of a shot, over animations.fire and the fire sound. |
|
projectile |
Fire a projectile instead of bullets (the launcher’s keys). | |
alt |
A second trigger on attack2: a table of any of the keys above, each falling back to the first trigger’s. It draws on ammo2 when the record names one. |
|
reload |
"clip" |
"clip", the whole clip at once; "shells", one at a time until full or fired; "none". |
reload_time |
1.5 | A reload’s length when the model has no reload animation. |
eject |
"fire" |
When a casing comes out: "fire", "reload" or "none". |
draw_rate, draw_time |
1, 0.5 | How fast the draw animation plays, and how long the draw takes with none. |
animations |
The model’s animation names: draw, fire, alt, reload, reload_start, reload_finish. Each defaults to its key. |
|
clip_body, clip_pose |
An ammo counter on the model: a body group whose choice is the rounds in the clip, and a pose parameter set from 1 when full to 0 when empty. |
A press made while the gun is drawing or before its refire still fires as soon as it’s ready, as long as the button is held.
evil.weapons:launcher
A firearm whose shots are projectiles. It takes every firearm key, and needs a projectile table:
| Key | Default | What it is |
|---|---|---|
model |
required | The projectile’s model. |
speed |
30 | Metres a second along the aim. |
gravity |
1 | Its share of the world’s gravity. |
physical, mass |
false | A physics body that rolls, bounces and can be grabbed, and its mass in kg. |
size |
0 | The box it’s traced with, in metres out from its centre, or a line at 0. |
spin |
0 | Degrees a second it tumbles. |
fuse |
0 | Seconds until it goes off by itself, never at 0. |
touch |
"explode" |
What touching something does: "explode", "stick", "bounce", "strike" (hit and vanish) or "remove". |
stick, bounce |
5, 0.4 | How long a stuck one stays, at most 60 seconds, and how much speed a bounce keeps. |
damage, force |
0 | What hitting a prop or person directly does. |
blast |
{ damage, radius, force, sound, colour, size }: it explodes with the engine’s blast, falling off to the edge of its radius. |
|
glow, trail |
{ colour, size, blink } and { colour, width, life, smoke }. |
evil.weapons:melee
| Key | Default | What it is |
|---|---|---|
range |
1.9 | Metres the swing reaches. |
box |
0.4 | A swing that misses with a line tries again with a box this far out on every side. |
damage, force |
the record’s damage, ¾ of it |
What a hit does. |
refire, miss_refire |
0.4, refire |
Seconds before the next swing after a hit and after a miss. |
delay |
0 | Seconds between the swing starting and it landing. |
fire |
"auto" |
"auto" swings while held, "semi" once a press. |
draw_rate, draw_time, animations |
As the firearm’s, with fire for a hit and miss for a miss. |
A hit plays the weapon’s impact effect, or the struck surface’s, and its hit sound; a miss plays its miss sound.
evil.weapons:beam
| Key | Default | What it is |
|---|---|---|
range |
20 | Metres the beam reaches. |
size |
0 | Sweep a box this far out from the line, for a thick beam. |
damage, force |
the record’s damage, 0 |
Damage and push a second, dealt every pulse. |
pulse |
0.1 | Seconds between strikes and impact effects. |
drain |
0 | Rounds a second it uses while firing. |
heat |
{ rise, fall, resume }: heat from 0 to 1 that rises and falls by so much a second (0.5, 0.25) while firing and while not. At 1 it overheats, plays its overheat sound and won’t fire again until it cools to resume (0.3). The heat is the weapon’s heat reading. |
|
draw_rate, draw_time, animations |
As the firearm’s. |
The beam shows in the holder’s weapon colour while attack is held, plays fire as it starts and stop as it ends.
Readings on the HUD
A weapon’s readings show wherever a HUD field or a power bar segment names them. A hudfield record reads one with value = "weapon.<name>", and shows only while the held weapon gives it:
[hudfield."hudfield/heat"]
title = "Heat"
region = "lower_right"
row = 1
shows = "meter"
value = "weapon.heat"
low = 0.8
A barsegment reads it in braces, text = "Heat {weapon.heat:0%}".
A behaviour of your own
When no tuning says what you want, write the behaviour. A C# Plugin registers it by name while it loads, and from then on any weapon record, yours or anyone’s, can name it:
using EvilEngine.Api;
using EvilEngine.Api.Weapons;
using EvilEngine.Formats;
using EvilEngine.Formats.Records;
namespace ChainGun;
public sealed class ChainPlugin : IPlugin
{
public void Load(PluginContext context)
{
if (context.Realm == Realm.Server)
context.Weapons.RegisterBehaviour(ContentId.Parse("chain:lightning"), record => new Lightning(record));
}
public void Unload()
{
}
}
sealed class Lightning(WeaponRecord record) : IWeaponBehaviour
{
readonly double damage = record.Tuning.Number("damage", 12);
readonly int jumps = record.Tuning.Integer("jumps", 3);
readonly ContentId zap = record.Tuning.Id("zap") ?? ContentId.Parse("chain:effect/zap");
double next;
public void Deploy(IWeaponHolder holder)
{
holder.Animate("draw");
next = holder.Time + 0.4;
}
public void Think(IWeaponHolder holder)
{
if (holder.Time < next || !holder.Held.HasFlag(WeaponButtons.Attack))
return;
next = holder.Time + 0.15;
holder.Animate("fire");
foreach (var hit in holder.Fire(new WeaponBullets(jumps, 0.05, damage, 2) { TracerEvery = 1 }))
if (hit.Hit)
holder.Effect(zap, WeaponEffectAt.At(hit.End, hit.Normal));
}
public void Holster(IWeaponHolder holder)
{
}
}
The factory is called once for each weapon a body carries, with that weapon’s record resolved down its bases, so each instance keeps its own state and reads its own tuning. Read the tuning in the constructor, as above: whatever it hasn’t read by then is noted as doing nothing. WeaponTuning reads numbers, choices, colours, ids, lists and nested tables, each with a fallback and each wrong value named.
What the holder gives a behaviour each tick:
| Member | What it does |
|---|---|
Eye, Aim, Velocity, Yaw |
Where the holder looks from and along, and how they move. |
Held, Pressed, Released, Mode |
The buttons this tick and the weapon’s mode. |
Time, Interval |
The session’s clock and the tick’s length. |
Record, Clip, Ammo, SetAmmo |
The weapon’s record, its clip and the body’s ammo by type. |
Animate, Duration |
Play an animation on the view model, and how long one lasts, to time a refire or reload by it. |
Sound, Punch |
Play a sound at the holder, kick the view. |
Fire |
Fire bullets, which can name their own tracer, impact and flash, and get back what each pellet hit. |
Trace |
A line, or a box with a size, through the world, its props and its people. |
Strike |
Hurt or push what a trace hit. |
Effect, Eject |
Play an effect record at a point or on the weapon’s attachment, eject a casing. |
Launch |
Launch a projectile with a behaviour of your own that steers it and decides what it does when it touches something. |
Beam, Dot |
Show a beam to a point or a body, or a laser dot. |
Speed, Zoom |
Slow or speed up the holder, and ease their field of view in and out. |
Hold, Gesture |
Change the hold type the holder’s body takes, and play a gesture on it everyone sees. |
Reading |
A number the HUD and power bar show by name, a charge or a heat. |
ViewModel |
The view model’s skin, body groups, pose parameters, a stack swapped in for its own, and whether it shows. |
Speed, zoom, readings and the view model’s changes last until they’re changed again or the weapon is put away, when they all go back at once.
A weapon can also be registered on its own, context.Weapons.Register(weapon, () => new MyGun()), which wins over the behaviour its record names.
Telling what went wrong
Nothing about a weapon fails quietly. The boot transcript and the session’s notes name a weapon record naming a behaviour no Plugin registered, tuning a behaviour didn’t read or couldn’t use, an effect, tracer or casing record that isn’t there, an event naming a slot the weapon hasn’t got, and a behaviour that threw, each once. A weapon with a missing part still shows on the wheel and does the rest.