Created
January 27, 2017 17:23
-
-
Save hvacengi/9a7ef88ac0b4010cabf4a2653adc1352 to your computer and use it in GitHub Desktop.
Testing of KSP Events for AGX - working code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AGXEventHandler | |
{ | |
public static EventData<Action<List<float>>> onTestEvent; | |
public static readonly List<float> numList = new List<float>(); // readonly, so it cannot be overwritten | |
public static AGXEventHandler myEventHandler; // singleton class instance | |
public static void Init() | |
{ | |
if (myEventHandler == null) // only create a new object if it doesn't exist yet | |
{ | |
myEventHandler = new AGXEventHandler(); | |
myEventHandler.init(); | |
} | |
} | |
public void init() | |
{ | |
onTestEvent = new EventData<Action<List<float>>>("onTestEvent"); | |
Debug.Log("AGXEventHandler event created"); | |
numList.Add(Time.realtimeSinceStartup); | |
Debug.Log("AGXEventHandler adding initial time to numList"); | |
onTestEvent.Add(testAGXmethod); | |
Debug.Log("AGXEventHandler subscribed to event"); | |
} | |
public void testAGXmethod(Action<List<float>> lstAction) | |
{ | |
Debug.LogError("AGXEventHandler testAGXmethod: event fired!"); | |
lstAction(numList); // call the action, passing it numList (which C# passes by reference) | |
} | |
} | |
[KSPAddon(KSPAddon.Startup.Instantly, true)] // instantly ensures the event is created as early as possible | |
public class AGXEventTestFlight : MonoBehaviour | |
{ | |
public void Start() | |
{ | |
AGXEventHandler.Init(); // initialize the event handler | |
DontDestroyOnLoad(this); // coroutines stop when the object is destroyed, this keeps it running constantly | |
StartCoroutine("numCounter"); | |
} | |
public IEnumerator numCounter() | |
{ | |
while (true) // rather than starting a new coroutine every 5s, just let this one loop | |
{ | |
AGXEventHandler.numList.Add(Time.realtimeSinceStartup); | |
Debug.Log("AGX event add time, i = " + AGXEventHandler.numList.Count); | |
yield return new WaitForSeconds(5f); | |
} | |
} | |
} | |
//------------------------------------------------------- code from other mod: | |
[KSPAddon(KSPAddon.Startup.MainMenu, false)] // do main menu for testing so we don't have to load a save | |
public class EventTest : MonoBehaviour | |
{ | |
public static List<float> testList = new List<float>(); // init to empty list just so it isn't "null", reference will be overwritten | |
public void Start() | |
{ | |
Debug.Log("EventTest get event"); | |
var testEvent = GameEvents.FindEvent<EventData<Action<List<float>>>>("onTestEvent"); | |
if (testEvent != null) // null check in case somethign isn't set up correctly | |
{ | |
Debug.Log("EventTest event found, firing!"); | |
Debug.Log("EventTest before fire list count: " + testList.Count); | |
testEvent.Fire(l => testList = l); // pass an anonymous method which accepts a "list" parameter, and sets testList to that reference | |
// This could have also passed a method accepting a single List<float> parameter | |
Debug.Log("EventTest event fired, current list count: " + testList.Count); // this count now reflects the other list, proving that | |
// events don't wait until the next tick to execute. | |
} | |
else | |
{ | |
Debug.LogError("EventTest failed to find event."); | |
} | |
StartCoroutine("PrintCoroutine"); | |
} | |
public IEnumerator PrintCoroutine() | |
{ | |
Debug.Log("EventTest PrintCoroutine started"); | |
yield return new WaitForSeconds(5f); // Wait until 5s after the coroutine is started | |
for (int i = 0; i < 100; ++i) // iterate through 100 times every time you go to the main menu | |
{ | |
PrintList(); | |
yield return new WaitForSeconds(5f); | |
} | |
} | |
public static void PrintList() | |
{ | |
Debug.Log("EventTest PrintList start " + Time.realtimeSinceStartup + testList.Count); | |
for (int i = 0; i < testList.Count; i++) | |
{ | |
Debug.LogError("EventTest testList[" + i + "] = " + testList[i]); | |
} | |
Debug.Log("EventTest PrintList end"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment