Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 02-14-2007, 02:25 PM   #1 (permalink)
Forum Expert
 
Join Date: Feb 2005
Age: 24
Posts: 985
Send a message via AIM to Nochte
Default Mixed-type collections?

It's been a SUPER long time since I've worked with C#, so I'm a bit fuzzy on this one.
Here's what I've got:

I have two classes; Player, and AIPlayer. AIPlayer is derived from Player. I want to use a container that will hold both types, and be able to call methods from each object as its own class. I've attempted ArrayList, using similar to the following code:
Code:
System.Collections.ArrayList players = new System.Collections.ArrayList();
Player one = new Player();
AIPlayer two = new AIPlayer();
players.Add(one);
players.Add(two);

players[0].Move;
players[1].Move;
...etc
players[0] returns an object, not a player or AIPlayer.
Ideas?
__________________
Quote:
Originally Posted by kkjjkk View Post
how sad you people call yourselves scripters and you can't even format an emulator game into something completely differnt....no wounder y'all are stuck here with no real scripting jobs unlike me. please go back to school.
QFDA
Nochte is offline   Reply With Quote
Old 02-14-2007, 02:28 PM   #2 (permalink)
Master of the Internet
 
Join Date: Oct 2005
Age: 44
Posts: 6,283
Default

If you use an arraylist, the arraylist has no types, it can only return an object. You will have to test for type, and then cast it accordingly. Or, you can use a Generic List which you can type (if you are using .Net 2.0), and make it of the base type:

Code:
List<Player> list = new List<Player> ();
__________________
Why is it that I'm never as smart as I thought I was yesterday?
My vast knowledge is only surpassed by my infinite ignorance.
<TheOutkastDev> i might have to hire an assassin to killl mal so that i can jump in front of the bullet and piss on him
Malaperth is offline   Reply With Quote
Old 02-14-2007, 02:32 PM   #3 (permalink)
Forum Expert
 
Join Date: Feb 2005
Age: 24
Posts: 985
Send a message via AIM to Nochte
Default

So I can add any object to the list, as long as it's derived from player?
__________________
Quote:
Originally Posted by kkjjkk View Post
how sad you people call yourselves scripters and you can't even format an emulator game into something completely differnt....no wounder y'all are stuck here with no real scripting jobs unlike me. please go back to school.
QFDA
Nochte is offline   Reply With Quote
Old 02-14-2007, 02:45 PM   #4 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,762
Default

Arraylists are object lists, whatever is in it, is assumed to be an object, thus when you get something out of it, you need to cast it as its type

Code:
System.Collections.ArrayList players = new System.Collections.ArrayList();
Player one = new Player();
AIPlayer two = new AIPlayer();
players.Add(one);
players.Add(two);

((Player)players[0]).Move;
((Player)players[1]).Move;
...etc
If you end up casting it as the wrong type, you will get a Invalid cast type exception and a crash.

Using a generic list, can give you a list that doesnt need casting. For example


Code:
System.Collections.Generics.List<Player> players = new System.Collections.Generics.List<Player>();
Player one = new Player();
AIPlayer two = new AIPlayer();
players.Add(one);
players.Add(two);

players[0].Move;//this will work
players[1].Move;//this will work

//If you needed to see if the specific player was a AIPlayer you could do this
if( players[0] is AIPlayer )
    ((AIPlayer)players[0]).DoThis();
Hope this helps
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote
Old 02-14-2007, 02:53 PM   #5 (permalink)
Forum Expert
 
Join Date: Feb 2005
Age: 24
Posts: 985
Send a message via AIM to Nochte
Default

It helps a lot. I've got it sorta working. When a property (move) is called, regardless of if it's Player or AIPlayer, the Player's move is used. How would I specify that the AIPlayer's move needs to be used when the type is AIPlayer?

Let me know if you need to see my declarations for everything, I'll attach on the next post. Thanks
__________________
Quote:
Originally Posted by kkjjkk View Post
how sad you people call yourselves scripters and you can't even format an emulator game into something completely differnt....no wounder y'all are stuck here with no real scripting jobs unlike me. please go back to school.
QFDA
Nochte is offline   Reply With Quote
Old 02-14-2007, 02:55 PM   #6 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,762
Default

Quote:
Originally Posted by Nochte View Post
It helps a lot. I've got it sorta working. When a property (move) is called, regardless of if it's Player or AIPlayer, the Player's move is used. How would I specify that the AIPlayer's move needs to be used when the type is AIPlayer?

Let me know if you need to see my declarations for everything, I'll attach on the next post. Thanks
Is Move a virtual Function? if so, when you override it in the AIPlayer class, reguardless of it being Player or AIPlayer, it should call its top overriden function.
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote
Old 02-14-2007, 03:33 PM   #7 (permalink)
Forum Expert
 
Join Date: Feb 2005
Age: 24
Posts: 985
Send a message via AIM to Nochte
Default

It is not. Should I create a completely virtual base class Player, then derive AIPlayer and HumanPlayer (currently, Player is a human player) from it?
__________________
Quote:
Originally Posted by kkjjkk View Post
how sad you people call yourselves scripters and you can't even format an emulator game into something completely differnt....no wounder y'all are stuck here with no real scripting jobs unlike me. please go back to school.
QFDA
Nochte is offline   Reply With Quote
Old 02-14-2007, 03:50 PM   #8 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,762
Default

Quote:
Originally Posted by Nochte View Post
It is not. Should I create a completely virtual base class Player, then derive AIPlayer and HumanPlayer (currently, Player is a human player) from it?
Doesnt matter how u do it, thats all preference but when you inherit methods you want ot be different the base class has to be virtual and the class inhereted to needs to override those methods to perform them differently.

Examples

Code:
public class BaseClass
{
    public BaseClass(){}

    public virtual void Foo() {}
}

public class InheretedClass : BaseClass
{
    public InheretedClass() : base() {}

    public override void Foo() {}
}
In this instance, if i cast InheredtedClass as a BaseClass, and call Foo it will call it at its InheretedClass level.
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote
Old 02-14-2007, 03:55 PM   #9 (permalink)
Forum Expert
 
Join Date: Feb 2005
Age: 24
Posts: 985
Send a message via AIM to Nochte
Default

Perfect. Exactly what I need. Thanks. I'll get back to ya later when I'm done.

Update:
Worked like a champ. Thanks for the help, Jeff and Mal
__________________
Quote:
Originally Posted by kkjjkk View Post
how sad you people call yourselves scripters and you can't even format an emulator game into something completely differnt....no wounder y'all are stuck here with no real scripting jobs unlike me. please go back to school.
QFDA

Last edited by Nochte; 02-14-2007 at 04:54 PM. Reason: Update
Nochte is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5