Jump to content
  • Advertisement
Sign in to follow this  

Having trouble accessing an objects properties.

This topic is 2117 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

            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

Share this post

Link to post
Share on other sites
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).

Share this post

Link to post
Share on other sites

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.

Edited by Aurioch

Share this post

Link to post
Share on other sites

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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!