Jump to content

  • Log In with Google      Sign In   
  • Create Account


Having trouble accessing an objects properties.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 demongunsman   Members   -  Reputation: 136

Like
0Likes
Like

Posted 25 December 2013 - 07:48 AM

            Character[] PlayerOne = new Character[5];

            PlayerOne[1] = new BladeWarrior();
            PlayerOne[2] = new FistWarrior();
            PlayerOne[3] = new Archer();
            PlayerOne[4] = new RedMage();
            PlayerOne[5] = new BlueMage();          

I know through polymorphism, a BladeWarrior can be a Character but it cant be the other way around. My problem is that when I try to access an element of an array. Player[1] for example, I cant access functions and variables from the BladeWarrior class. It's only letting me access variables and functions from the Character class which the BladeWarrior class inherits from.

 

I'm going to need to be able to access Bladewarrior functions/variables if Im going to want 2 characters to fight.

I was thinking I could use the "as" function to set PlayerOne[1] as the specific object . Not exactly like this:

string s = objArray[i] as string;
 The line of code above is just to get an idea of which "as" Im talking about.

What is a solution to this problem? Your time and effort is much appreciated. Thank you in advanced.


Edited by demongunsman, 25 December 2013 - 07:49 AM.


Sponsor:

#2 Telastyn   Crossbones+   -  Reputation: 3726

Like
3Likes
Like

Posted 25 December 2013 - 09:06 AM

The solution is to not need BladeWarrior's methods to fight. Generally that means making common methods available on character, making BladeWarrior an instance of Character (rather than a derivative of it), or by letting the thing that controls the fighting get a BladeWarrior rather than a Character array (though that one depends a lot more on how your game works).

#3 Aurioch   Crossbones+   -  Reputation: 1298

Like
0Likes
Like

Posted 27 December 2013 - 08:22 AM

While Telastyn's solution is proper one, band aid is to cast each element from Character class into appropriate class. That will give you access to that class' methods and properties.

((RedMage)PlayerOne[4]).CastFireball();

Edited by Aurioch, 27 December 2013 - 08:25 AM.


#4 SonicD007   Members   -  Reputation: 457

Like
0Likes
Like

Posted 29 December 2013 - 02:47 PM

I'm not 100% sure what the problem your running into is aside from accessing the derived classes data.

 

You could have character have a virtual method called DoAttack() where each derived class can override it to do whatever they have todo.  Another approach to the problem is to use composition and instead of creating bladewarrior, fistwarrior, drunkenwarrior, you could have a single class called GameMonster (or w.e) which would contain a pointer to another class that has more information on the type of monster it is.

 

This article should help you. http://gameprogrammingpatterns.com/type-object.html

 

The cool part about doing it that was is you can have all your different monsters in an xml file and modify attributes without having to recompile.  You can also make sub-monster types without having to specify every attribute and instead have it "inherit" from it's parent monster type.

 

EDIT: Since it's C# that you're using, you don't have to worry about pointers.  Classes are passed by reference so you can just set your monsters to have w.e monster type you want.

 

e.g. Monster m = new Monster(); //You could do it the way the article shows as well

m.SetType(new FistWarrior());

 

The only thing is that if you have custom attack patterns or something you would need to do a litle more work.  Maybe something like m.SetBehavior(new WarriorBehavior(WarriorStyle.Fist));   Just an idea, haven't gotten up to that yet in my project


Edited by SonicD007, 29 December 2013 - 02:51 PM.


#5 d4n1   Members   -  Reputation: 423

Like
0Likes
Like

Posted 30 December 2013 - 05:12 PM

Sounds like you should be doing a design by contract, where your objects extend an abstract class which implements and IInterface.

 

Would it not be easier for you to just say PlayerOne[n-1].primary(), and not worry about WHAT primary is, let the code decide that.  Your object should contain all the information that IT needs to know what IT should do, I would personally group your attacks and such into something more abstract.  Let your interface define what it is you would like to implement, and then each sub class of your character will implement it.  

 

In doing so, you would end up with something like

 

ICharacter c1 = Factory.Create("Mage");

ICharacter c2 = Factory.Create("Rob Ford");

 

c1.primary(); // code will execute maybe a fireball attack 

c2.primary(); //will cause the character to smoke a joint


Edited by d4n1, 30 December 2013 - 05:20 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS