- Home /
C# Delegates, a question
I began researching delegates and events recently to try and improve the versatility of my classes. Considering various classes in my game are going to be needing player input (keyboard or mouse), I've set up delegate events such as OnMouseDown, OnMouseUp, and OnKeyPressed. Each event passes an object containing data for the input (screen position of click, world position, targeted unit, etc), which gives me super easy access to a flexible player input from anywhere.
While it seems effective and clean, I'm curious if there's any downfalls to it or perhaps any better methods?
Also another question, if anybody is experienced in Garry's Mod Lua, you're able to hook into various events such as : http://wiki.garrysmod.com/page/ENTITY/OnTakeDamage
And modify the parameters of the event (CTakeDamageInfo) before its passed to the entity taking damage. Is this possibly the work of delegate events on a lower level?
You do realise you are describing what Unity already did in the EventSystem introduced in 4.6. Its a great idea, that's why Unity did it. But you could have saved yourself some work and used the existing solution.
Answer by KMKxJOEY1 · Jan 06, 2015 at 01:15 AM
Personally, instead of delegates I prefer to use abstraction or just plain virtual overriding. This is just stylistic personal preference though, as I haven't noticed any efficiency differences between the 2 methods.
Pertaining to your other question, could you be a bit more specific (maybe an example)? I have dabbled in Lua, but not anything to do with G-Mod.
Hmm, scripting in Garry's $$anonymous$$od you're able to hook into various events such as when an entity takes damage. ENTITY:OnTakeDamage( CDamageInfo dmginfo )
Before the Entity actually receives damage it's possible to take the CDamageInfo object and modify its values to your desire, such as CDamageInfo:SetDamage(1) so that the Entity takes only 1 damage, even though the original event may have called for 20 damage.
Back to delegates, I'm not sure if it could work, but it seems like I would be able to hook into my delegate events (somehow) and read or write parameters in a similar fashion to Garry's $$anonymous$$od.
public struct $$anonymous$$eyboardState
{
public char $$anonymous$$ey;
public DateTime TimeSent;
// etc
}
public delegate void $$anonymous$$eyboardInputHandler($$anonymous$$eyboardState state);
public static event $$anonymous$$eyboardInputHandler on$$anonymous$$eyPressed;
public static void On$$anonymous$$eyPressed($$anonymous$$eyboardState state)
{
if(on$$anonymous$$eyPressed != null)
{
// If the player pressed "k" state.$$anonymous$$ey's original value is "k"
// Hook in right here somehow and if I want, I can set the value of
// state.$$anonymous$$ey to anything, for example, "P"
on$$anonymous$$eyPressed(state);
}
}
public class SomeOtherClass
{
void Start()
{
InputEvents.on$$anonymous$$eyPressed += this.On$$anonymous$$eyPressed;
}
public void On$$anonymous$$eyPressed($$anonymous$$eyboardState state)
{
Debug.Log (state.$$anonymous$$ey);
// Because I hooked into and modified state earlier, output should be "P"
}
}
edit: This functionality wouldn't be entirely useful for me on the back-end, more out of curiosity and ideas of how I might be able to implement community modding