|
| | #137 (permalink) | |
| euro scum Join Date: Aug 2002 Location: Sweden
Posts: 808
| Quote:
In either case, yes in some situations you can use the one the framework supply for you, but in most cases you can't. Is it thread safe? If so, what do you do when you don't want a thread safe observer pattern, or if it isn't thread safe, what do you do when you want it to be thread safe? You implement it of course. I really don't think the difference between the "home made" observer/subject pattern above is much different from the one in the framework (yes Java have it as well, but it doesn't get the exact behavior you're after most of the time), so while I respect your opinion on the matter, I can't more than to disagree. A Framework is good to have no doubt, but you shouldn't use it blindly when it can't support the exact behavior you want. Besides, it consists mostly of interfaces, and I bet it does in the framework as well, which well... results in you implementing it anyway (interfaces are almost always better than inheritance) But of course, you're entitled to your opinion as I said already, lucky for us we all think differently 8) EDIT: Aha now I see what you mean (after reading up on it), you meant it's part of the .NET framework. Though, one should notice that delegate / event is part of the Observable role, but yes that role should indeed be replaced by delegate / events in .NET and not implement it explicitly when using .NET. I still don't think it's misleading however, since you kind of need to know the pattern to understand what delegate / events means in .NET But the point still stands, it's a good situation where a Observer/Subject pattern should be used, whether it's with using delegate/events as in .NET or Subject/Observer in Java. Last edited by slitz : 05-05-2008 at 05:11 AM. | |
| | |
| | #138 (permalink) | ||
| Registered User Join Date: Feb 2006
Posts: 1,634
+7 Internets | Quote:
Quote:
In C#, the subject class is analogous to a delegate, which is a built-in type. A delegate operates in a similar way to a function pointer (or rather, an ordered list of function pointers.) When you invoke it, it calls all of the functions in its list. For example, I could write: Code:
In this example, the delegate calls both WriteToFile() and WriteToScreen() with the argument "Here's some error text." Delegates are used directly as part of the C# event model. When you declare an event, you associate it with a delegate, which keeps a list of who is subscribed to the event. Instead of trying to explain exactly how events interact with delegates, I'll just write some code, and it should mostly speak for itself. This should be analogous to the Java subject/observer code above. Every time the spaceship hits an asteroid, it will fire off a damage-taken event, and it will let the UI know about it. Code:
Hope this satisfies folks' curiosity. C# is a pretty handy language. Last edited by Fog : 05-05-2008 at 09:34 AM. | ||
| | |
| | #140 (permalink) |
| upper management material Join Date: Nov 2002 Location: Orlando, FL
Posts: 1,997
+8 Internets | In Java do Action Listeners give similar functionality to Delegates? I don't do much Java either besides what was required in some undergrad classes. I've recently migrated from C++ to C# for all my personal and work projects when I can use it. I will NEVER go back to C++ ... C# is that much better. BTW, I'm using Delegates to speed up some C# Neural Network code that must be executed 100's of time per frame in game. Each node in the network is assigned a random mathematical function, from out of approximately 15 functions. Each network has possibly dozens of nodes, and there are a lot of networks. Instead of a switch of if-else at each node like this ... Code:
Code:
|
| | |
| | #141 (permalink) | |
| euro scum Join Date: Aug 2002 Location: Sweden
Posts: 808
| Quote:
But I guess, the functionality is similar, what happens under the hood is not similar at all however. Code:
Code:
And what if you want to send a object reference, and not really a method/function pointer? Last edited by slitz : 05-06-2008 at 01:09 AM. | |
| | |
| | #142 (permalink) | |
| Registered User Join Date: Feb 2006
Posts: 1,634
+7 Internets | Quote:
Code:
As for sending an object reference, the standard practice for events is to send relevant arguments into the event handling function. For example, here's my mouse event handler above: Code:
Code:
If you look at my previous post, you'll see that anyone handling the DamageTaken event gets a reference to the SpaceShip that fired the event. Different events obviously have different parameters that get passed. Here's what you get from a Windows KeyDown or KeyUp event: ![]() If you are defining your own events for classes you've written, you can pass any information at all, or none. Last edited by Fog : 05-06-2008 at 07:38 AM. | |
| | |
| | #143 (permalink) |
| euro scum Join Date: Aug 2002 Location: Sweden
Posts: 808
| Ah ok... What I meant with the other part was: Assuming a actionListener inherit a "close enough" kind of behavior, and you want to program that behavior into a interface. In java you would do: public interface MyCoolInterface extends KeyListener, MouseListener{ } At first glance I assumed you could do public interface MyCoolInterface { public delegate void KeyEventHandler(Object sender, EventArgs e); public delegate void MouseEventHandler(Object sender, MouseEventArgs e); } But then again that probably won't work since the delegate is actually a object in itself (or rather a pointer to a function, but then we are going into semantics)?. My assumption with this is that you can't include the delegate keyword in a interface, so you can't actually program to the interface in this case? How would you force the programmer to use a delegate when implementing a certain interface if you can't use the keyword delegate in a interface? |
| | |
| | #144 (permalink) | |
| Registered User Join Date: Feb 2006
Posts: 1,634
+7 Internets | Quote:
(Remember, however, that those two delegate lines aren't creating any objects at all. They're declaring two types of the sort MyCoolInterface.KeyEventHandler and MyCoolInterface.MouseEventHandler. Those types are the prototypes for functions that another class can use as an event handler to receive those events.) However, when you say something like Code:
Well, in C#, the first interface step is not necessary. You define your own event handling methods, without having them handed down from a base interface or base class, and then you hook them up to the events individually, like the first code snippet in my post above. | |
| | |
| | #146 (permalink) |
| Registered User Join Date: Apr 2008
Posts: 1
| First off, thanks Cloud9 for making this thread, had no idea XNA existed until I stumbled upon this. I've been having a lot of fun with this. Spent the last two weeks or so going through many C# and XNA tutorials alike, because I don't know either. Currently working on a 2D RPG game, yet another Final Fantasy type clone. It's slow going since I'm far from a professional programmer. As far as OOP goes I've had basic Java and Visual Basic.NET classes, but that was quite a few years back and I don't really remember either, so it's like I'm starting anew. After a few days I have a walkable world map, and random battles you step into, although for now they're all the same level 1 slime monster until I implement my planned encounter system. I'm not sure if it's been mentioned in the thread already or not, if it has consider this just another plug: Nick Gravelyn's Tutorials are very well done and easy to follow. I followed along with his Tile Engine tutorial and used the multi-layer concept as the basis for my own world, with my own spin on things. |
| | |
| | #147 (permalink) | |
| Registered User Join Date: Jul 2002 Location: Los Angeles California
Posts: 228
+9 Internets | Quote:
and he also wrote a full 'space invaders' clone tutorial that can be found on ziggyware.But yeah, i also like his videos because he explains what he does, and works out problems as he's recording can get dull at times but still fun to watch. | |
| | |
| | #148 (permalink) |
| Registered User Join Date: Jul 2002 Location: Los Angeles California
Posts: 228
+9 Internets | Didn't feel like it warrented a new thread, but I've been starting to update the code.google.com site a little more for the RPG project I'm working on. Right now I'm working on a few things (like animation system and what not) but I have a lot of things completed or "working" (like equipment, swords armors, etc) firstxnarpg - Google Code Revision 13 is the most up to date. It also includes all the .png's if you wanted to try and checkout + compile |
| | |
| | #150 (permalink) |
| upper management material Join Date: Nov 2002 Location: Orlando, FL
Posts: 1,997
+8 Internets | Not sure if you guys working on RPG games have noticed, but they have an RPG Starter Kit up on XNA.com: XNA Creators Club Online - role-playing game Seems pretty nice. If nothing else you could at least get some art assets out of it and see how they do some stuff. |
| | |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
| |