StatFunctions.AddCustomFunction
Declaration#
public static void AddCustomFunction(string functionId, StatFunction function)Parameters#
| Name | Description |
|---|---|
| functionId | Id of the function |
| function | Function callback |
Description#
Add a custom function to the registered list
Usage#
Example
using NullSave.GDTK;using NullSave.GDTK.Stats;using System.Collections.Generic;using UnityEngine;
public class Example : MonoBehaviour{
public void ExampleMethod() { // Add the ability to check if a stat value is even or not // Returns 1 if stat value is even, 0 if not
// Adds normal function StatFunctions.AddCustomFunction("hasStat", StatIsEven);
// Adds subscription for function StatFunctions.AddCustomFunctionSubscripiton("hasStat", StatIsEven); }
public static void StatIsEven(string request, Dictionary<string, StatSource> sources, out string result) { // Check if we have a quantifier (such as global:) bool hasCol = request.IndexOf(':') >= 0; foreach (var entry in sources) { // Find an entry that matches the request if ((entry.Key != "" && request.StartsWith(entry.Key)) || (entry.Key == string.Empty && !hasCol)) { // Look for the stat on that entry GDTKStat stat = entry.Value.GetStat(request.Substring(entry.Key.Length)); if (stat != null) { // Check if the stat is even result = stat.value % 2 == 0 ? "1" : "0"; return; } } }
result = "0"; }
private void StatIsEven(Dictionary<string, StatSource> sources, string request, SimpleEvent requester, List<SimpleEvent> subscriptionList) { bool hasCol = request.IndexOf(':') >= 0; foreach (var entry in sources) { if ((entry.Key != "" && request.StartsWith(entry.Key)) || (entry.Key == string.Empty && !hasCol)) { GDTKStat stat = entry.Value.GetStat(request.Substring(entry.Key.Length)); if(stat != null) { stat.onValueChanged += requester; subscriptionList.Add(stat.onValueChanged); } } } }
}