Fires of Heaven Guild Message Board  

Go Back   Fires of Heaven Guild Message Board > Fires of Heaven Related Forums > Uberworlds Development Forum
User Name
Password
ForumSpy Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 05-04-2008, 08:07 PM   #136 (permalink)
Kargon
Registered User
 
Join Date: Aug 2002
Location: Durham, NC
Posts: 137
Send a message via ICQ to Kargon
Quote:
Originally Posted by Fog View Post
This is kind of misleading in terms of advice for the above fellow, because I don't know about Java, but this functionality is native in C# by way of events and delegates. You don't need these homemade observer lists. Please don't implement code like this in C#.
Able to show us some code example that implements the same sort of functionality?
__________________
Kargon is offline   Reply With Quote
Old 05-05-2008, 04:43 AM   #137 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 807
-10 Internets
Quote:
Originally Posted by Fog View Post
This is kind of misleading in terms of advice for the above fellow, because I don't know about Java, but this functionality is native in C# by way of events and delegates. You don't need these homemade observer lists. Please don't implement code like this in C#.
I don't really think it's misleading at all since it's a good pattern to know whether it's implemented in the framework already or not. I don't get the part where you say it's implemented natively? Think you mean it's in the framework or?

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.
slitz is offline   Reply With Quote
Old 05-05-2008, 05:29 AM   #138 (permalink)
Fog
Registered User
 
Join Date: Feb 2006
Posts: 1,630
+6 Internets
Quote:
Originally Posted by slitz View Post
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.
Yes, that's what I meant. I agree it's the right idea -- it would just be very silly to implement your own objects like that in C# when it's built into the language.

Quote:
Originally Posted by Kargon View Post
Able to show us some code example that implements the same sort of functionality?
Sure, with the caveat that I don't know Java, so I might have conceivably missed some subtlety in the posted snippet. Here's what I get out of it: you have a Subject class and associated object. It keeps a list of (abstracted) objects that want to be notified of some things that happen, and provides a mechanism to notify them all of something at the same time.

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:
... /* delegates are strongly typed, this line is just a type declaration, not an * object! they can have non-void return types, too, but then they can only * refer to one function at a time (for obvious reasons) */ public delegate void ErrorHandler(string errorMessage); public static void WriteToFile(string str) { ... } public static void WriteToScreen(string str) { ... } public static void ReportError() { // this is the object: ErrorHandler simpleDelegate = null; // the += and -= notation is overloaded in C#, // it means "add/remove from a delegate's invocation list" simpleDelegate += new ErrorHandler(WriteToFile); simpleDelegate += new ErrorHandler(WriteToScreen); // invoking the delegate, as if it were a function simpleDelegate("Here's some error text."); } ...
(Trying to clarify things through comments for people who don't know C#.)

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:
... public class SpaceShip { // delegate type declaration for event handlers of this event: public delegate void DamageTakenHandler(SpaceShip ship, float amount); // the event itself: public event DamageTakenHandler DamageTaken; /* this method is what the class uses to fire the event. it just sees * if the event delegate is null (i.e. if there are subscribers) and if * there are, it fires the event; that is, invokes the delegate. */ protected void OnDamageTaken(SpaceShip ship, float amount) { if(DamageTaken != null) DamageTaken(ship, amount); } ... ... protected void GetHitByAsteroid() { OnDamageTaken(this, 50.0); } } public class UIHealthDisplay { public void DisplayDamageTaken(SpaceShip target, float amount) { ... // do UI stuff etc } public void RegisterSpaceshipOnUI(SpaceShip ship) { /* subscribe to the event by adding your event handling * function to the event's invocation list, like so: */ ship.DamageTaken += new SpaceShip.DamageTakenHandler(DisplayDamageTaken); /* if you wrote a "DeregisterSpaceshipOnUI" function, it would * just use the "-=" operator in the same way to remove it from * the invocation list of the spaceship's damage event. */ } } ...
Obviously, you could sign lots of methods up in the same way to subscribe to that event.

Hope this satisfies folks' curiosity. C# is a pretty handy language.

Last edited by Fog : 05-05-2008 at 09:34 AM.
Fog is offline   Reply With Quote
Old 05-05-2008, 06:02 AM   #139 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 807
-10 Internets
Quote:
Originally Posted by Fog View Post
Yes, that's what I meant. I agree it's the right idea -- it would just be very silly to implement your own objects like that in C# when it's built into the language.
Jupp agreed! Just had no idea that there was a delegate keyword in C# 8)
slitz is offline   Reply With Quote
Old 05-05-2008, 12:43 PM   #140 (permalink)
Froofy-D
upper management material
 
Froofy-D's Avatar
 
Join Date: Nov 2002
Location: Orlando, FL
Posts: 1,991
+7 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:
switch(functionNumber) { case 0: Sin(); case 1: Cos(); case 2: HyperbolicTangent(); case 3: Inverse(); case 4: Ramp(); case 5: Step(); case 6: Gaussian(); case 7: Spike(); case 8: Tangent(); etc... }
... each node gets assigned a Delegate called "functionPointer" that points to it's correct function. So the above switch statement within a node is replaced by simply:

Code:
node.functionPointer();
So, because nodes get activated 100's of times per frame, possibly thousands of 'switch' cases per frame are avoided, resulting in significant speedup.
Froofy-D is offline   Reply With Quote
Old 05-05-2008, 04:59 PM   #141 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 807
-10 Internets
Quote:
Originally Posted by Froofy-D View Post
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.
Java does not have function pointers / method references, whatever you want to call them. Think the decision was to make inner and anonymous classes instead.
But I guess, the functionality is similar, what happens under the hood is not similar at all however.

Code:
aButtonReference.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ } });
How do you solve delegates in Interfaces btw? If comparing that to listeners in java I could do:
Code:
public interface MyClass extends MouseListener,KeyListener,PropertyChangeListener, FocusListener { }
How would that be solved in C#? or perhaps it wouldn't?
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.
slitz is offline   Reply With Quote
Old 05-06-2008, 07:28 AM   #142 (permalink)
Fog
Registered User
 
Join Date: Feb 2006
Posts: 1,630
+6 Internets
Quote:
Originally Posted by slitz View Post
How do you solve delegates in Interfaces btw? If comparing that to listeners in java I could do:

How would that be solved in C#? or perhaps it wouldn't?
And what if you want to send a object reference, and not really a method/function pointer?
I don't think I understand exactly what you're trying to accomplish. If you're just trying to get an object to subscribe to more than one event, you write an event handler for each event, and you hook it up to the event's delegate. For example, here's a few lines of designer code in one of my applications:

Code:
this.Layout += new System.Windows.Forms.LayoutEventHandler(this.AdjustMinimap); this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMove); this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMouseDown);
I have three functions, one of which (AdjustMap) gets called on the control's "Layout" event (when the control gets resized and needs to reposition internal elements), one that gets called on the MouseMove event, and one that gets called on the MouseDown event. If I wanted to subscribe to more events I could handle as many as I liked.

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:
/* OnMouseMove gets a MouseEventArgs parameter which is an object filled * with useful information. By convention, the "sender" parameter is the * object that fired the event. */ protected void OnMouseMove(object sender, MouseEventArgs e) { if (miniMapOutlineDragging || mapDragging || miniMapOutlineRect.Contains(e.Location)) // referencing the mouse this.Cursor = Cursors.Hand; // location that gets passed else if (miniMapHandlesVisible) Cursor = GetProperCursor(GetMouseMinimapPosition(e.Location)); ... }
So you get the object reference of the control that sent the event in the event handler. Why do I get those parameters? In this case, it's because in the .NET Windows Forms library, a MouseEventHandler type is defined thusly:

Code:
public delegate void MouseEventHandler(object sender, MouseEventArgs e);
And somewhere deep within the framework, when a control fires off a mouse-moved event, it passes it an object reference and creates a MouseEventArgs packet of information about the current state, which ends up in our event handler's care.

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.
Fog is offline   Reply With Quote
Old 05-06-2008, 09:51 AM   #143 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 807
-10 Internets
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?
slitz is offline   Reply With Quote
Old 05-06-2008, 12:23 PM   #144 (permalink)
Fog
Registered User
 
Join Date: Feb 2006
Posts: 1,630
+6 Internets
Quote:
Originally Posted by slitz View Post
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);

}
This would be the right idea if you wanted classes implementing MyCoolInterface to publish key and mouse events for other classes to listen for.

(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:
public interface MyCoolInterface extends KeyListener, MouseListener {}
in Java, unless I am misunderstanding, the intent is that classes implementing MyCoolInterface have the capability to subscribe to key and mouse events coming from some external source. Presumably, you will go on elsewhere in your code to say something like AddKeyListener(someMyCoolInterfaceObject) so that it actually gets the events, right?

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.
Fog is offline   Reply With Quote
Old 05-06-2008, 11:33 PM   #145 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 807
-10 Internets
Jupp, would have to assign the listeners to a event pool.
I think I got the deal with delegates now, thanks 8)
slitz is offline   Reply With Quote
Old 05-08-2008, 04:16 PM   #146 (permalink)
Nirrudn
Registered User
 
Join Date: Apr 2008
Posts: 1
+0 Internets
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.
Nirrudn is offline   Reply With Quote
Old 05-08-2008, 10:03 PM   #147 (permalink)
Cloud9_
Registered User
 
Join Date: Jul 2002
Location: Los Angeles California
Posts: 228
+9 Internets
Send a message via ICQ to Cloud9_
Quote:
Originally Posted by Nirrudn View Post
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.
I followed his tutorials for a bit, and actually thats what got me the idea to do my in-game map editor. I took my current progress and adjusted it based on what he was doing, and it turned out pretty cool. He's working on a particle system right now that should be fun to keep tabs on 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.
Cloud9_ is offline   Reply With Quote
Old 06-06-2008, 11:57 AM   #148 (permalink)
Cloud9_
Registered User
 
Join Date: Jul 2002
Location: Los Angeles California
Posts: 228
+9 Internets
Send a message via ICQ to Cloud9_
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
Cloud9_ is offline   Reply With Quote
Old 06-06-2008, 12:16 PM   #149 (permalink)
Agonizing
Registered User
 
Join Date: Apr 2005
Posts: 134
-3 Internets
Just read through this post, had some helpful information. Downloading those C# and reading up on the language, Hopefully I can join you guys in the fun soon
Agonizing is offline   Reply With Quote
Old 06-20-2008, 01:23 PM   #150 (permalink)
Froofy-D
upper management material
 
Froofy-D's Avatar
 
Join Date: Nov 2002
Location: Orlando, FL
Posts: 1,991
+7 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.
Attached Images
 
Froofy-D is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
uberguilds network



All times are GMT -7. The time now is 09:03 AM.


Powered by vBulletin® Version 3.6.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0 RC6