Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Option Attributes

4aiur edited this page Mar 23, 2019 · 2 revisions

What is it?

Option attributes are low-effort ways to implement various options into a plugin.
Note that these are designed not for a modular plugin approach, and as such is not intended for large scale use.

How do they work?

Simply place the corresponding attribute on a field which has a valid type to turn it into an option. The value after the plugin type is initialized is taken as the default. The name is automatically generated (unless specified) by converting anyCamelCaseName into any_camel_case_name.

It is recommended to keep these fields readonly to prevent accidentally writing to them during execution, whether from the declaring plugin or a third party.

Fields marked by these attributes will be refreshed every round before WaitingForPlayersEvent, but after the log message. This means do not try to read them before WaitingForPlayersEvent, which is often the first event anyway.

Features

ConfigOption

Prerequisites:

configPrefix in the declaring plugin's PluginDetails must be set.

Valid types:

  • bool
  • float
  • int
  • int[]
  • string
  • string[]
  • Dictionary<string, string>
  • Dictionary<int, int>
  • LiveConfig<T> where T is one of the stated types.

Constructors

  • bool primaryUser = true, bool randomized = false - Default config behavior.
  • string name, bool primaryUser = true, bool randomized = false - Allows for specific config names.
  • string name, string description, bool primaryUser = true, bool randomized = false - Allows for specific config names and specifying a(n often unused) description.

Example

[PluginDetails(
    ...
    configPrefix = "ot",
    ...
)]
public class OptionTester : Plugin
{
    // All of the following config field defaults can be set in the constructor (not Register()). 
    // However, setting the default next to the config option declaration is much easier to read.

    // Registers config "ot_magic_number" with a default of 5.
    [ConfigOption]
    public readonly int magicNumber = 5;

    // Registers config "ot_debug_adjective" with a default of "config options are awesome and I am not biased!"
    // Note: This is an example of a bad use of ConfigOption strings. A raw or to-format string is better suited for LangOptions. Try to only use ConfigOption strings for custom formatting or verbose options, e.g. if "rigged" is one of the several options to determine a gameplay element.
    [ConfigOption("debug_adjective")]
    public readonly string debugConfigAdjective = "rigged";

    // Registers random config "ot_coin_lottery" with a default of 1.
    [ConfigOption(true, true)]
    public readonly LiveConfig<int> coinLottery = new LiveConfig<int>(1);

    ...
}

LangOption

Prerequisites:

langFile (no extension) in the declaring plugin's PluginDetails must be set.

Valid types:

  • string

Constructors

  • no parameters - auto-generated name
  • string key - uses key instead of the usual auto-generated name.

Example

[PluginDetails(
    ...
    langFile = nameof(OptionTester),
    ...
)]
public class OptionTester : Plugin
{
    ...

    // Just like above, all of these can be set in the constructor, but it is less readable.
    /*
     * Note that unlike the above, all of these will be placed in
     * "sm_translations/OptionTester.txt" (because the file is nameof(OptionTester)) 
     * instead of the usual config_gameplay.txt.
     */

    // Registers lang "command_credits_output" with a default of "{0} made this!".
    [LangOption]
    public readonly string commandCreditsOutput = "{0} made this!";

    /*
     * Registers lang "config_lang_debug_format" with a default of 
     * "The truth is, the config string was \"{0}\" from the start."
     */
    [LangOption("config_lang_debug_format")]
    public readonly string debugConfigFormatter = "The truth is, the config string was \"{0}\" from the start.";

    ...
}

Server Guides

API Documents

Clone this wiki locally