C# Code Actions
Introduction
When you add a new Execute C# Code sub-action, you will be greeted with the following code snippet:
using System;
public class CPHInline
{
public bool Execute()
{
// your main code goes here
return true;
}
}
For every execute code sub-action, this is the minimum that must be present for it to function.
You can return false
to stop execution of the current action, including any subsequent sub-actions.
Within your Execute()
function, you have access to the CPH
class, allowing you to make calls to internal Streamer.bot methods.
CPH
refers to the original development name of Streamer.bot, ChannelPointsHandler
Arguments
C# actions have access to a Dictionary<string, object>
propertry named args
This property contains all variables from the current argument stack.
Accessing
CPH.TryGetArg
, added in Streamer.bot v0.2.3
.If you attempt to access an argument by calling it directly via args["argName"]
and it does not exist, a KeyNotFound
exception will be thrown.
You can also check if a key exists with the general C# methods ContainsKey
or TryGetValue
.
// NOTE: CPH.TryGetArg requires Streamer.bot 0.2.3 or later
// If %rawInput% exists, store it in rawInput
CPH.TryGetArg("rawInput", out string rawInput);
// Log the result
CPH.LogInfo($"Variable rawInput: {rawInput}");
Special
eventSource
and __source
must be accessed with their corresponding methods GetSource and GetEventType:EventSource source = CPH.GetSource(); // eventSource in args
Lifecycle Methods
In addition to the Execute()
method, you can also implement the lifecycle methods outlined below.
Init()
Executed when your code is first compiled, useful if you need to perform any initialization steps.
public void Init()
{
// place your init code here
}
Dispose()
Executed on destroy, useful when you need to perform some cleanup.
public void Dispose()
{
// place your dispose code here
}