Having trouble accessing an objects properties.

Started by
3 comments, last by SonicD007 10 years, 3 months ago

            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.

Advertisement
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).

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();

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

This topic is closed to new replies.

Advertisement