Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Custom Converters

Emre Kara edited this page Jun 24, 2021 · 2 revisions

What does a converter do?

A converter converts the users input to the type the command method needs.

Example:

[CustomCommand("mycommand")]
public void MyCommandMethod(IPlayer player, Position position, IPlayer target, int amount)

Here each of the types have their own converter.
So if a user uses this command:

/mycommand 213.3 3.42 5.65 Bonus 5

The first 3 parameters will be used for Position, "Bonus" for IPlayer and "5" for the amount.

Default converters

This command system implements predefined converters for all basic C# types.

Also there are converters for these alt:V types:

  • Position (X, Y, Z)
  • Rgba (R, G, B, A)
  • DegreeRotation (Roll, Pitch, Yaw)
  • Rotation (Roll, Pitch, Yaw)
  • IPlayer (Name)

Set Converter (sync)

To register a custom converter, use this command.

using CustomCommandSystem.Integration;
using AltV.Net;

Alt.Core.SetCustomCommandsConverter<Type>(int amountArgumentsNeeded, ConverterDelegate converter);

Example:

Alt.Core.SetCustomCommandsConverter<int>(1, (player, args, cancel) => int.Parse(args[0]));

Use AltAsync.Do for code in converters!
It's not guaranteed that the converter code will be run in the main thread.

Set Converter (async)

To register a custom async converter, use this command.

using CustomCommandSystem.Integration;
using AltV.Net;

Alt.Core.SetCustomCommandsAsyncConverter<Type>(int amountArgumentsNeeded, ConverterDelegate converter);

Example:

Alt.Core.SetCustomCommandsAsyncConverter<IPlayer>(1, PlayerConverter)

private async Task<IPlayer> PlayerConverter(IPlayer player, ArraySegment<string> args, CancelEventArgs cancelErrorMsgOnFail) 
{
    IPlayer? target = await FindPlayer(args[0]);
    if (target is null)
    {
        cancelErrorMsgOnFail.Cancel = true;
        AltAsync.Do(() => player.SendChatMessage("Player with that name could not be found!"));
    }
    return target;
}

Use AltAsync.Do for code in converters!
It's not guaranteed that the converter code will be run in the main thread.

Clone this wiki locally