• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Having trouble accessing an objects properties.

4 posts in this topic

            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

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

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

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0