Flowscript Framework is a Reloaded-II mod that allows modders to easily add new flowscript functions without having to replace existing ones.
Download the p5rpc.flowscriptframework.interfaces package from nuget and add p5rpc.flowscriptframework as a dependency in your mods ModConfig.json, Then add this code to your mods mod.cs file:
var flowFrameworkController = _modLoader.GetController<IFlowFramework>();
if (flowFrameworkController == null || !flowFrameworkController.TryGetTarget(out var flowFramework))
{
throw new Exception("Failed to get IFlowFramework Controller");
}The IFlowFramework interface provides two methods Register and GetFlowApi, an enum FlowStatus, and another interface IFlowApi
The Register method is what lets you add new (or replace) flowscript functions. Here is an example of how it should be used:
flowFramework.Register("CUSTOM_FUNCTION", 2, () =>
{
// Code goes here
return FlowStatus.SUCCESS;
});The first two args are the function name and arg counts respectively, and the third is an anonymous function containing your functions code. The function will return an Id thats generated by the framework to be used in your custom Functions.json. The Register method allows for an optional fourth argument to provide your own id, but this should only be used to replace existing script functions, not for creating new ones.
After registering your custom function, launch the game and check the log for a line that looks something like:
Registered Function <CUSTOM_FUNCTION> with 2 args at index 0x9F14
In this instance, 0x9F14 is your Id.
Check the BF Emulator Docs for more details on how to create a custom Functions.json.
NOTE: the Id you get depends on the name of the function. If you decide to change the name of your function, be sure to get the new Id.
The IFlowApi interface gives you access to all of the necessary functions to retrieve arguments and set return values. You can gain access to the api with the following line:
var flowApi = flowFramework.GetFlowApi();The API provides functions to retrieve int, float and string args, and set the return value as either an int or float. Here is an example of how it may be used.
flowFramework.Register("SQUARE_NUMBER", 1, () =>
{
var flowApi = flowFramework.GetFlowApi();
var num = flowApi.GetIntArg(0);
flowApi.SetReturnValue(num * num);
return FlowStatus.SUCCESS;
});The FlowStatus enum tells the game whether or not the function has completed execution. FlowStatus.SUCCESS indicates that the function has finished execution, and allows the game to continue executing the current script, while FlowStatus.FAILURE will cause the game to run the function again instead of moving on.
This repo includes a test mod you may use as a reference when building your own projects.