# IWeapons interface

> The weapons a realm's Plugins give, each named by its weapon record. A Plugin gets its own view of it from PluginContext.Weapons, so every weapon it registers is listed under its id and goes when it unloads.

Namespace `EvilEngine.Api.Weapons`, assembly `EvilEngine.Api`. Engine commit 4ff877b1.

```csharp
public interface IWeapons
```

The weapons a realm's Plugins give, each named by its `weapon` record. A Plugin gets its own view of it from [PluginContext.Weapons](/docs/api/plugincontext#weapons), so every weapon it registers is listed under its id and goes when it unloads.

A player class's kit gives only registered weapons, so a weapon record from a mount or a Plugin's `Records/` puts nothing in anyone's hands until some Plugin registers it. A weapon registered with a behaviour does what its [IWeaponBehaviour](/docs/api/iweaponbehaviour) says while it's held; one registered without is carried, shown on the wheel and held, and nothing more.

```csharp
public void Load(PluginContext context)
{
    if (context.Realm == Realm.Server)
        context.Weapons.Register(ContentId.Parse("gmod:weapon/weapon_physgun"), () => new PhysicsGun());
}
```

## Members

### Registered (property)

```csharp
IReadOnlyList<ContentId> Registered { get; }
```

Every weapon registered in this realm so far, by any Plugin, once each.

### Behaviours (property)

```csharp
IReadOnlyList<ContentId> Behaviours { get; }
```

Every behaviour registered by name in this realm so far, by any Plugin, once each.

### Register (method)

```csharp
void Register(ContentId weapon)
```

```csharp
void Register(ContentId weapon, Func<IWeaponBehaviour> behaviour)
```

Registers a weapon by its `weapon` record's id, in any namespace, so the Source systems Plugin registers Garry's Mod's `gmod:weapon/weapon_physgun`. Call it while your Plugin loads, inside [Load](/docs/api/iplugin#load): a call after `Load` has returned throws `InvalidOperationException`.

The overload with a behaviour takes a factory the server calls once for each carried item of that weapon, so every player's copy keeps its own state.

Two Plugins can register the same weapon. The one that loaded later gives it, and when that one unloads the earlier one gives it again. Only the server realm reads registrations today.

### RegisterBehaviour (method)

```csharp
void RegisterBehaviour(ContentId behaviour, Func<WeaponRecord, IWeaponBehaviour> factory)
```

Registers a behaviour by an id in your Plugin's own namespace, for any `weapon` record to name with `behaviour = "<id>"`. Every weapon record naming one that's registered is a registered weapon, so a content pack with no code makes weapons on your behaviour. The factory gets the weapon's resolved record, `base` and all, once for each carried item, so one class serves every weapon that names it and reads each one's own `tuning`. Read the tuning in the constructor: whatever it hasn't read by then is noted once as doing nothing. A weapon's own registration through [Register](#register) wins over the behaviour its record names.

```csharp
context.Weapons.RegisterBehaviour(ContentId.Parse("mygun:chain"), record => new ChainLightning(record));
```

Call it while your Plugin loads. An id outside your namespace throws `ArgumentException`, and a call after `Load` returned throws `InvalidOperationException`. The engine's own behaviours come from the `evil.weapons` Plugin ([Making weapons](/docs/plugins/weapons)).
