[.net] Need a hand with Events and Delegates

Started by
3 comments, last by Talonius 15 years, 9 months ago
Im in the middle of the design of the rendering code for my map object. Since my map is divided into layers, I think that the proper way to render the map and the actors respecting the layering occlusion, is to launch an event in the MAP Draw() method , just when it reaches the actor layer rendering, This event should then trigger the Draw() method of the Actor class. Each Draw() method receives a SpriteBatch object. In summary:

// MAP                                    ACTOR
// ----------------                       ----------------
// Draw(spriteBatch)  -> raises event to  Draw(SpriteBatch)





I've been reading tutorials and books about how to do this but it seems that I just can't get it. Any guidance will be greatly appreciated. Thank you [Edited by - EvilNando on July 21, 2008 4:53:03 AM]
Advertisement
Why not just have a MapLayer class that contains all the objects in that layer and a list of MapLayer objects in the map that the Map's Draw method iterates though? I don't know that you need to fire off an event to do this.

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

I plant to have many things on the map (actors, events, collisions..)

so I thought it would be nice to fill in a delegate with all the methods that need to be called when the map is rendering.

right now what I have is a Draw function inside the map class that receives a reference of an Actor class, so when the actor layer is finished rendering it calls for the Actor[].Draw(SpriteBatch) method
Quote:Original post by EvilNando
I plant to have many things on the map (actors, events, collisions..)

so I thought it would be nice to fill in a delegate with all the methods that need to be called when the map is rendering.


Your rendering code should just render, not do other stuff like worry about collisions or events being triggered on the map.

Quote:Original post by EvilNando
right now what I have is a Draw function inside the map class that receives a reference of an Actor class, so when the actor layer is finished rendering it calls for the Actor[].Draw(SpriteBatch) method

So what happens when there's more than one Actor object on the map?

Why wouldn't the actor layer take care of getting actor objects to render?

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

To answer your question, you need to publish an event on the rendering engine which each actor would subscribe to. The event definition would be defined using a delegate.

First, know that a delegate is nothing more than a function pointer. It's a way to generically reference a function or method, without knowing which specific function you're referencing.

We declare a delegate using one of two forms:
     public delegate void SampleDelegate(SampleDelegateEventArgs e)
This doesn't do anything other than inform the compiler about a delegate type named SampleDelegate. Behind the scenes a great deal of infrastructure is actually created for you, and I highly recommend that curious folks look into it. This delegate can point to any function which takes a single argument of the type SampleDelegateEventArgs.

The second way to declare a delegate is to not declare one, and to instead use the generic form of the event instead, such as:
     public event EventHandler<SampleDelegateEventArgs> SampleDelegateEvent;
You can use this delegate if you want.
     SampleDelegate delegateVariable = null;     delegateVariable += new SampleDelegate(targetFunction);     delegateVariable(new SampleDelegateEventArgs());
This usage doesn't qualify as an event, per se.

Events are based on delegates. A delegate must be defined prior to an event. An event is declared like:
     public event SampleDelegate SampleDelegateEvent;
Note that the event definition doesn't include arguments that are passed; those are included with the delegate definition.

So your renderer needs to define a delegate, publish an event, and then your actors will need to subscribe to the event. Each actor will need to access an instance of the containing class and add an event handler for the event you created. You'll need to create a MapRenderEventArgs object and include inside of it the SpriteBatch object you want each Actor to use and/or reference.
     Actor actor = new Actor();     Renderer.GetInstance().RenderActors += actor.RenderSelf;
Inside the Renderer you would invoke the actor's rendering like so:
     protected void OnActorRender()     {          if(RenderActors != null)          {               RenderActors(new MapRenderEventArgs(SpriteBatch));          }     }
Really though... the questions raised in the first replies to your post are valid and should be considered. Would it be simpler to iterate through a collector of Actors and ask them to draw themselves? Would it be simpler to simply include the drawing logic within the renderer?

[Edited by - Talonius on July 22, 2008 4:07:06 PM]
..what we do will echo throughout eternity..

This topic is closed to new replies.

Advertisement