• 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
biubidboy

Data encapsulation

6 posts in this topic

Hi all.
I am a beginning programmer writing a basic game engine. I am confused about data encapsulation within my programme, so I thought I would ask for advice on this forum. I have created a Property class that represents a basic "name, value" pair. Additionally, the "value" can be a list of Property objects in it's own right, allowing a single list of Property objects to represent a tree of "name, value" pairs. The Property class also provides a method that iterates recursively through the tree structure and saves the tree data in a to a file (currently XML). I am planning on using this system to store properties of objects within the game, but also game settings and other such disparate applications. Essentially, the "parent" object that contains a tree of Property objects should be able to modify the values of the Property objects at will (e.g. to update character health after a hit). I have avoided using getter and setter methods in my code thus far, but I am at a loss as to how I should provide access to the value of Property objects in an encapsulated way without them. How would you go about providing access to the information in the Property object without compromising code re-usability?
Thanks,
Mike.
0

Share this post


Link to post
Share on other sites
[quote name='biubidboy' timestamp='1309092873' post='4827877']
Hi all.
I am a beginning programmer writing a basic game engine. I am confused about data encapsulation within my programme, so I thought I would ask for advice on this forum. I have created a Property class that represents a basic "name, value" pair. Additionally, the "value" can be a list of Property objects in it's own right, allowing a single list of Property objects to represent a tree of "name, value" pairs. The Property class also provides a method that iterates recursively through the tree structure and saves the tree data in a to a file (currently XML). I am planning on using this system to store properties of objects within the game, but also game settings and other such disparate applications. Essentially, the "parent" object that contains a tree of Property objects should be able to modify the values of the Property objects at will (e.g. to update character health after a hit). I have avoided using getter and setter methods in my code thus far, but I am at a loss as to how I should provide access to the value of Property objects in an encapsulated way without them. How would you go about providing access to the information in the Property object without compromising code re-usability?
Thanks,
Mike.
[/quote]

I'm going to be very negative and say don't do this in the object system. This sounds like a fine system for configuration and settings but the overhead of such a generic system used in simulated objects (i.e. those that have health) will quickly become unreasonable if you go over a couple hundred objects being simulated per frame. I'd look at a dynamic yet compile time known solution instead of this for any of the object data because the cost is nearly exponential as you grow object types and counts.
0

Share this post


Link to post
Share on other sites
Thank you both for the replies.
Apoch: I am using C++, sorry about that omission :P
AllEightUp: I had considered that performance would be an issue, so I had planned on implementing a fixed size list of Properties for in-game objects (say 200 per object). All variably sized Property s (like current inventory contents) would then be sublists of the main list. I would then use a simple numerical index system to access the properties (e.g. the 8th entry in the main list is always current health, 100th entry is always a Property list containing the contents of the inventory etc.). Surely the overhead for such a system would just be the cost of accessing an element of an array?
0

Share this post


Link to post
Share on other sites
Your most straightforward option is probably to implement a generic get/set on the Property class, and have it traverse the property tree looking for the appropriate value to retrieve or modify:

[source lang="cpp"]bool Property::HasProperty(const PropertyID& id) const // Can pass PropertyIDs by value if they're just enums or ints or whatever
{
std::map<PropertyID, PropertyValueType>::const_iterator iter = DirectProperties.find(id);
if(iter != DirectProperties.end())
return true;


for(std::list<Property>::const_iterator treeiter = PropertyTree.begin(), treeend = PropertyTree.end(); treeiter != treeend; ++treeiter)
{
if(iter->HasProperty(id))
return true;
}

return false;}

template <typename T>
T Property::Read(const PropertyID& id) const
{
std::map<PropertyID, PropertyValueType>::const_iterator iter = DirectProperties.find(id);
if(iter != DirectProperties.end())
return static_cast<T>(iter->second); // Might need a more sophisticated conversion depending on what your underlying PropertyValueType is

for(std::list<Property>::const_iterator treeiter = PropertyTree.begin(), treeend = PropertyTree.end(); treeiter != treeend; ++treeiter)
{
if(iter->HasProperty(id))
return iter->Read<T>(id);
}

throw std::exception("Invalid property ID");
}

template <typename T>
void Property::Write(const PropertyID& id, const T& value)
{
// Traverse the tree *first* so we guarantee that the correct property is found and set before setting it on this node

for(std::list<Property>::iterator treeiter = PropertyTree.begin(), treeend = PropertyTree.end(); treeiter != treeend; ++treeiter)
{
if(iter->HasProperty(id))
{
iter->Write(id, value);
return;
}
}

DirectProperties[id] = static_cast<PropertyValueType>(value); // again, conversion may need to be more sophisticated here
}[/source]
1

Share this post


Link to post
Share on other sites
Have you considered offloading some of this to a scripting language? They often represent "objects" as recursive dictionaries, with some neat optimisations (e.g. special handling for dictionaries less than a given size). It might also save you from duplicating the data in XML, as you would be able to write your object configuration in the scripts themselves. For example, you might use the prototype pattern for objects.
1

Share this post


Link to post
Share on other sites
[quote name='biubidboy' timestamp='1309142176' post='4828085']
Thank you both for the replies.
Apoch: I am using C++, sorry about that omission :P
AllEightUp: I had considered that performance would be an issue, so I had planned on implementing a fixed size list of Properties for in-game objects (say 200 per object). All variably sized Property s (like current inventory contents) would then be sublists of the main list. I would then use a simple numerical index system to access the properties (e.g. the 8th entry in the main list is always current health, 100th entry is always a Property list containing the contents of the inventory etc.). Surely the overhead for such a system would just be the cost of accessing an element of an array?
[/quote]

The index system is not a bad one for performance but it does come at the cost that such a fixed scheme is often limiting and will always "grow" over time (you'll always want to use more properties) making it more and more expensive in memory size. I personally still like data block systems which are a cross between the property scheme and the index scheme. Basically it works something like:

[code]
class MyObject : public Go::Object
{
public:
.. whatever needed ..

// up in "Go::Object" I have functions:
PropRef< int > FindProperty( const std::string& name );

static void InitializeProperties()
{
mWhateverProperty = FindProperty( "Health" );
}
virtual bool Update( const float deltaT )
{
// Use it just like an int here.
mWhateverProperty( this ) += 1;
return true;
}

private:
// Handles to properties. Can be full structures if desired since the propref is a transparent passthrough.
static PropRef< int > mWhateverProperty;
};
[/code]

So, basically this is the same overhead as using the index solution you mention (except the one time lookup of the PropRef) but uses only the amount of memory space actually required. They are pretty tricky to implement the first time but once you figure it out, it's a very fast and effective method of mixing the concept of dynamic properties into the system without the overhead of a flat fixed sized item.
0

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