-
Notifications
You must be signed in to change notification settings - Fork 0
Delegates in C#
Although this guide assumes prior programming experience, delegates are something that you may still be unfamiliar with. Delegates are types that represent references to methods, and are often used for event handling. When modding, we often use delegates to run code in response to buttons being pressed, as seen in this code snippet:
button.OnInteract += delegate () { DoSomething(); return false; };
Here, button.OnInteract
is the event handler which is called when the button
, which is a KMSelectable
, is interacted with, and we are subscribing to the event using the assignment operator +=
. This means that, when button.OnInteract
is called, the code in the DoSomething
method will run, and then the delegate will return false
. Any number of delegates can be assigned to an event handler, and they will all be called every time the event happens.
In this guide, I will use the following alternative syntax:
button.OnInteract += () => { DoSomething(); return false; };
For more help, join the Keep Talking and Nobody Explodes Discord server, and visit the #mod-resources
and #mod-creation
channels.
Contact me on Discord at ku.ro
to ask questions or give feedback!
This sidebar, as with the rest of the wiki, is a work in progress. If a link is missing, that means the relevant page has not been made yet, but is planned.
- What's in the main script?
- Extras
- Grayscale background
- Alternative component puzzle-background UV-mapping
- Animated button component
- Example modules
- Setting up the module
- Making text
- Making selectable buttons
- Adding sound effects
- How to do lighting
- Animations
- Testing
- Extras
- Colourblind support
- Controller support
- Twitch Plays support
- Souvenir support
- Rule seed support
- How to make a boss module
- How to make a needy module
- Importing the game's assemblies