Advertisement
TankorSmash

Untitled

Jan 10th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. I was working on my game and I was getting to a point where I needed things to happen in response to user actions. For instance I wanted a mob to freeze in response to a player's spell, or something similar. At first it was as simple as putting in a method that was called when the player picked up an item, but as the complexity of the events increased this no longer was working very well. My problem in accomplishing this was that I began having to pass the entire level object to an item so it was able to make changes to the mobs, maps and items stored in that level object. This seemed like a very clunky way to do things as with bigger projects I am sure this will cause some memory overhead problems.
  2.  
  3. With some of my brief experimentation with other languages I had used a concept called <em>Events and Listeners.</em> This is a concept that is most often associated with GUI controls. For instance a user clicks a button and that button fires off an event, some form is listening some where and responds to that event being fired.
  4.  
  5. The question now was how do I learn to use events properly in C Sharp to do what I want to do. Doing some searching turned up a lot of stuff on the MSDN site, and while that site has a lot of great information for some one more experienced in programming, the layout and sheer volume of information is often daunting to some one with little to no experience like myself. Other searches brought sites with smaller examples and less information, however this also lead to confusion as it was not fully explained what was going on.
  6.  
  7. Any way on to the good part!
  8.  
  9. This is where C Sharp <em>delegates</em> come into play, for now you can just think of the delegate as the event publisher. MSDN describes a delegate as:
  10. <blockquote>A <a href="http://msdn.microsoft.com/en-us/library/900fyy8e%28v=vs.80%29.aspx">delegate</a> is a type that references a method. Once a delegate is assigned a method, it behaves exactly like that method. The delegate method can be used like any other method, with parameters and a return value.</blockquote>
  11. I look at that description even now and it still is a little confusing to me. Basically what this does is let me link a method to a delegate as long as their return type and parameters are the same; in essence I am linking functionality from two classes together.
  12.  
  13. So what I do to get things working in sync is to create the delegate on an object that I want to publish my events, In the case of my game this delegate is on my Base Item class.
  14.  
  15. &nbsp;
  16. [csharp]
  17. public delegate void ItemEvent(String type);
  18. public event ItemEvent OnCollisionEvent;
  19.  
  20. protected virtual void EventOnCollision()
  21. {
  22. if(OnCollisionEvent != null)
  23. this.OnCollisionEvent(m_Type);
  24. }
  25. [/csharp]
  26. Since I am working toward a game, I will be using this in the context as such.
  27.  
  28. Line 1 I have created my delegate type, for this particular instance I don't want the function to return anything and I want it to work with a string, I will use the string to identify what object has published the event.
  29.  
  30. Line 2 I have created and Event Object, this is what I will use to tie a method from another object to this delegate.
  31.  
  32. Finally lines 4-8 is the method I will call to see if any outside methods have been assigned to my delegate.
  33.  
  34. &nbsp;
  35.  
  36. Next we need to create the method on another object that we will tie to the delegate type. We have to remember that the method we tie to a delegate has to have the same signature as the delegate. Since the term signature caused me a bit of confusion I will explain it  here: A signature  is both the return type and parameters fed to the method.
  37.  
  38. &nbsp;
  39. [csharp]
  40. public void EventHandler(String type)
  41. {
  42. if (type == &quot;freeze mobs&quot;)
  43. {
  44. BaseMob.m_frozen = true;
  45. }
  46. if (type == &quot;slow mobs&quot;)
  47. {
  48. BaseMob.m_slowed = true;
  49. }
  50. }
  51. [/csharp]
  52.  
  53. What I have done here is created an Event handler method that has no return type and accepts a string as the parameter. You can have this function do whatever you want or need it to do, I have just had it handle some situation where mobiles in the game are either frozen or slowed.
  54.  
  55. Finally we need to tie the event Publisher (delegate) and Subscriber (Event handler) together, which is done with one simple line of code:
  56.  
  57. &nbsp;
  58. [csharp]Item.OnCollisionEvent += new BaseItem.ItemEvent(mob.EventHandler);[/csharp]
  59. <strong>Item </strong>represents the creation of an item in game <strong>OnCollisionEvent</strong> is the object we created on our Item class Line 2;
  60.  
  61. Then we create a new <strong>ItemEvent</strong> from the item class and in the parenthesis we feed the mobs <strong>Eventhandler</strong> function.  Now whenever we call the Items EventOnCollision method it will check to see if any methods have been assigned to it's delegate and execute them.
  62. I hope this helps some one else figure out how to use events a little bit.<strong>
  63. </strong>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement