Duck Game

Duck Game

Duck Game Script Extender (DEPRECATED)
Is actually ted  [developer] 22 Nov, 2016 @ 7:16pm
How It Works
MainLogic
The class that drives this mod is MainLogic, which creates a second thread to insert ScriptThing into every level that is loaded. Initially, this was directly calling Level.Add(scriptThing) on a set interval, but this was very unstable because the thread would be adding scriptThing to a level while the main game thread was trying to enumerate through the Level's objects. This semi-frequently crashed the game (but not consistently!).
I then tried to test for any boolean I could find that would be true while the game was doing an update. This was a slight improvement, but there were still times in the update cycle when I couldn't tell the game was updating (namely, the graphics drawing).
After still being met with errors, I once again scoured the DuckGame source code, and finally, on top of mount olympus, surrounded by god rays, there it was: Tasker.Task(). I was so relieved!
Tasker is a class in duckgame that stores a bunch of actions that are executed on the start of each Level update.
Tasker.RunTasks(4294967295u);
You can add to this list of tasks using Tasker.Task(), and that's exactly what I did. In the current and probably final version of this mod, the secondary thread consistently checks if scriptThing is present in the level, and if it is not, the secondary thread tells the Tasker object to add scriptThing to the level on the next frame update.

ScriptThing
Alright, enough teasing. I've mentioned this object a bunch of times now, but what does it actually do? ScriptThing extends Thing, the basic DuckGame Object. As a Thing, it has the normal functions a Thing does. It uses many of these functions to trigger the events in DGSE_Events. This part was simple.

DGSE_Events
Super simple. Has events and invokes them through public functions. Nothing to it.

HatsWithSounds
Listens to the onDucksSpawned event from DGSE_Events.instance and looks through a dictionary of teams and soundNames. If any duck's team is found in the dictionary, that duck's hat is replaced with a custom hat which has a custom sound.