Skip to content

Instantly share code, notes, and snippets.

@hvacengi
Last active January 27, 2017 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hvacengi/554ecf0463ab4045af24da95f3dec091 to your computer and use it in GitHub Desktop.
Save hvacengi/554ecf0463ab4045af24da95f3dec091 to your computer and use it in GitHub Desktop.
Comments on code testing AGX events
public class AGXEventHandler
{
public static EventData<List<float>> onTestEvent;
public static List<float> numList;
public static AGXEventHandler myEventHandler;
public static AGXFlight myFlightAddon;
public static AGXEditor myEditorAddon;
public void init()
{
Debug.Log("AGX event init fired!");
onTestEvent = new EventData<List<float>>("onTestEvent");
Debug.Log("AGX event init 1fired!");
numList = new List<float>();
Debug.Log("AGX event init 1A fired!");
numList.Add(Time.realtimeSinceStartup);
Debug.Log("AGX event init2fired!");
onTestEvent.Add(testAGXmethod);
Debug.Log("AGX event init 3fired!");
}
public void testAGXmethod(List<float> lst)
{
Debug.Log("AGX sees event fire!");
lst = numList; // hvacengi: this only sets the local variable lst (the parameter) to our reference
// in order to assign the list to class EventTest, it must be fired passing the value from this
// instance and the delegate method (this method) needs to be in EventTest.
// You could make this can work either by using 2 events, one fired by EventTest telling AGX to
// fire the event, or by the AGX event firing occasionally over time.
}
}
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class AGXEventTestFlight : PartModule
{
bool timeCheck;
public void Start()
{
StartCoroutine("numCounter");
}
public IEnumerator numCounter()
{
timeCheck = false;
AGXEventHandler.numList.Add(Time.realtimeSinceStartup);
Debug.Log("AGX event add time" + AGXEventHandler.numList.Count);
yield return new WaitForSeconds(5f);
timeCheck = true;
}
public void Update()
{
if (timeCheck)
{
StartCoroutine("numCounter");
}
}
}
//------------------------------------------------------- code from other mod:
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class EventTest : PartModule
{
public static List<EventTestListClass> testListContainer;
public void Start()
{
testListContainer = new List<EventTestListClass>();
}
public static void CallEvent()
{
Debug.Log("MA Call event start");
EventData<List<float>> MA2testEvent = GameEvents.FindEvent<EventData<List<float>>>("onTestEvent");
List<float> tempList = new List<float>();
// as explained above, the following call doesn't work because we're passing this class's reference
// to the local variable of the parameter. To make it work, the reference from AGXEventHandler needs
// to be passed to a method on this class instead.
MA2testEvent.Fire(tempList); //tried (ref tempList) and would not compile
testListContainer.Add(new EventTestListClass(Time.realtimeSinceStartup, tempList));
Debug.Log("MA Call event end " + tempList.Count);
}
public static void PrintContainer()
{
Debug.Log("MA container print start " + Time.realtimeSinceStartup + testListContainer.Count);
for (int i = 0; i < testListContainer.Count; i++)
{
Debug.Log("MA entry " + i + "|" + testListContainer[i].numList.Count);
foreach (float fl in testListContainer[i].numList)
{
Debug.Log("MA entry sub " + testListContainer[i].initTime + "|" + fl);
}
}
Debug.Log("MA Container print end");
}
}
public class EventTestListClass //use for event testing
{
public List<float> numList;
public float initTime;
public EventTestListClass()
{
numList = new List<float>();
initTime = Time.realtimeSinceStartup;
}
public EventTestListClass(float tmr, List<float> tempList)
{
numList = tempList;
initTime = tmr;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment