473,785 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about large objects

I have been writing games and I also read about good programming
techniques. I tend to create large objects that do lots of things. A
good example I have is a unit object. The object controls and holds
everything a unit in my game is supposed to do. What are some some
cures for this kind of large object or are they OK because they
represent one thing. If not what are better ways to design objects
that behave the same way. Would it be better to use inheritance or
friends and where can I look to be able to do the same thing in a
better way?

Here is a unit from a completed game I created. The header file at
least to give an idea of one of my larger objects.

#include<fstrea m>
#include<vector >
#include<cmath>
#include<map>

#include"coord. h"
#include"color. h"
#include"graphi cs.h"
#include"tbox.h "

#ifndef UNIT_H
#define UNIT_H

class unit{
protected:

HWND hwnd;
coord loc;
coord currentLoc;
graphics * gr;
tbox * combatBox;
std::vector<col or>colors;
std::map<char, coordkeys; //movement engine
coord n; //directions
coord s;
coord e;
coord w;
char kind;

float attack;
float dattack;
float defence;
int move;
int moved;
int dmove;
int range;
bool canmove;
bool selected;
bool mark;
bool disburse;
int col;

void create();
void displayBox(HDC, coord);
int convert(const int n){return n * 16;}

public:
unit();
unit(HWND,graph ics*,int,int,in t);
unit(HWND,graph ics*,int,int,in t,int,int);
unit(HWND,graph ics*,DWORD,floa t, float, int, int, int);
unit(HWND,graph ics*,DWORD,floa t, float, int, int, int, bool);
unit(unit*);
~unit();

DWORD sideColor(){ret urn colors[0].getCode();}
int intAttack(){ret urn attack;}
int intDefence(){re turn defence;}
float Attack(){return attack;}
float Defence(){retur n defence;}
int Moved(){return moved;}

void display(HDC); //display at crrent loc
void display(HDC, int, int); //display at specific loc
void display(HDC, int); //display with offset for stacking
void displayBig(HDC, int, int);
void change(HWND);
void update(HWND, coord);

coord nextCoord(char) ;
void mover(coord&, int);
void reset();
void tomark(); //marks unit for death;
bool marked(){return mark;}
coord getCoord(){retu rn loc;}
coord getCurrent(){re turn currentLoc;}
bool canMove(){retur n canmove;}
bool isSelect(){retu rn selected;}
bool inRange(coord);
void selectOn(){sele cted = true;}
void selectOff(){sel ected = false;}
void isArt(){range = 12;}
void cBoxOn(){combat Box = new tbox;};
void disbersed();
void unDisberse(){di sburse = false;}
void write(std::ofst ream&);
void load(std::ifstr eam&);
void addk(char k){kind = k;}
};

#endif

Apr 20 '07
14 2154
On 20 huhti, 18:53, JoeC <enki...@yahoo. comwrote:
What are some some
cures for this kind of large object or are they OK because they
represent one thing.
They are perfectly ok, but if there are actions that could be
moved to a separate class (and used elsewhere) then do so.
Your class wasn't even that large. Some of the classes in
my game project are about three times bigger.

Apr 24 '07 #11
On Apr 23, 10:48 pm, Kirit Sælensminde <kirit.saelensm i...@gmail.com>
wrote:
On Apr 24, 9:10 am, JoeC <enki...@yahoo. comwrote:
On Apr 20, 11:40 pm, Kirit Sælensminde <kirit.saelensm i...@gmail.com>
wrote:
On Apr 20, 10:53 pm, JoeC <enki...@yahoo. comwrote:I have been writing games and I also read about good programming
techniques. I tend to create large objects that do lots of things.A
good example I have is a unit object. The object controls and holds
everything a unit in my game is supposed to do. What are some some
cures for this kind of large object or are they OK because they
represent one thing. If not what are better ways to design objects
that behave the same way. Would it be better to use inheritance or
friends and where can I look to be able to do the same thing in a
better way?
Here is a unit from a completed game I created. The header file at
least to give an idea of one of my larger objects.
[snip huge class]
You're using the class more like a namespace here. Objects should be
cohesive, which is a word that takes a look of thinking about to
really understand. A simple definition is that they should do only one
thing and do that one thing well. The smaller the scope of the one
thing they do the better.
Look athttp://c2.com/cgi/wiki?ClassicOoA ntiPatternsande speciallyhttp://c2.co...
K
This is what I am asking. I use namespace std other than that I have
little experience or information on creating or using one. Most of
the stuff in my unit objects affect other thing in that object. I can
understand the graphics part and it has graphics and it would be good
to create a movable object and derive that into my class. I came up
with this after the game was done when I went back and tried to learn
lessons from my project.

As an exercise see how small you can make your class by putting things
into helper classes. Look for things that are logically grouped. It'll
take you a while to work through the dependencies to get them right,
but your aim is to try to reduce your main class into the absolute
minimum that you can.

When you do it you should try to arrange the classes such that the
members of the unit class don't need to know that unit exists. I.e.
that every class you use as an attribute doesn't know anything about
the class that uses it. This is an ideal and isn't always possible,
but think very carefully before breaking it -- never do it until
you've exhausted every other possibility.

K
I have looked into java a bit and everything is an object there.

For this object the best I can do is seperate out the movment into a
movable class. It seems like every variable should be an object.
Object, set and get. It just seems like a great deal of unnecessary
overhead. I know it is a simple question but what is the rational for
such small classes? Most of the class are these facors:

float attack;
float dattack;
float defence;
int move;
int moved;
int dmove;
int range;
bool canmove;
bool selected;
bool mark;
bool disburse;
int col;

These are the numbers that control the unit.

std::map<char, coordkeys; //movement engine
coord n; //directions
coord s;
coord e;
coord w;
char kind;

This should have been separated out and I will make that a separate
class in my next project.

These are my graphics and helper classes.

HWND hwnd;
coord loc;
coord currentLoc;
graphics * gr;
tbox * combatBox;
std::vector<col or>colors;

Too bad I can't post a picture here what it all looks like but here is
a link:
http://planetsourcecode.com/Upload_P...1202309521.JPG

Apr 24 '07 #12
On Apr 24, 2:11 am, Krice <pau...@mbnet.f iwrote:
On 20 huhti, 18:53, JoeC <enki...@yahoo. comwrote:
What are some some
cures for this kind of large object or are they OK because they
represent one thing.

They are perfectly ok, but if there are actions that could be
moved to a separate class (and used elsewhere) then do so.
Your class wasn't even that large. Some of the classes in
my game project are about three times bigger.
That is good perspective. I only know about programming what I read
in books. I don't have much contact with other programmers and my
programs don't really get seen by anyone although I look for places to
post them. I try to ask good questions and find ways to improve what
I am doing.

Apr 24 '07 #13

JoeC <en*****@yahoo. comwrote in message ...
[snip]
Good advice. I am trying to take what I read and create my own
projects. I like the ask questions on what I have done so see if I am
implementing concepts correctly. Here is my next generation of
graphics objects it is much more divided:
// again, put the guards around all:
#ifndef GRBIN_H
#define GRBIN_H
#include<window s.h>
#include<vector >
#include "graphics.h "
#include "color.h"
#include "coord.h"
// #ifndef GRBIN_H
// #define GRBIN_H
>
class grBin{ protected:
graphics * gr;
std::vector<col or>colors;
coord loc;
public:
~grBin(){delete gr;}
void display(HWND, const int, const int);
void displayClear(HW ND, const int, const int);
void displayBig(HWND , const int, const int);
int getX(){return loc.x;}
int getY(){return loc.y;}
};
>
This game is a different concept and is still in progress. In my last
game I did create a handle in case I wanted different kinds of units:
#ifndef HUNIT_H
#define HUNIT_H
#include<window s.h>
#include<fstrea m>
#include "unit.h"
#include "grboard.h"
// #ifndef HUNIT_H
// #define HUNIT_H
>
class hunit{
unit * p;
int * cnt;
void make(HWND, char, DWORD, int, int, bool);
public:
hunit() : cnt(new int(1)), p(new unit) {}
hunit(HWND, char, DWORD);
hunit(HWND, char, DWORD, int, int);
hunit(HWND, char, DWORD, int, int, bool);
You may be able to reduce the number of constructors here by using
default values.

// hunit(HWND, char, DWORD);
// hunit(HWND, char, DWORD, int, int);
// following line not syntax correct (for posting)

hunit(HWND, char, DWORD, int I1 = 0, int I2 = 0, bool flag = false);

Then you can 'call' it with 3, 4, 5 or 6 parms. as needed. It's a
style/design thing. If you don't understand, experiment a little, then
decide.

IMHO, I'd also watch out for how/where you use 'HWND' and 'DWORD'. If you
ever go to a GNU/Linux box, you will get a great headache changing all
instances of those. (you do know about wxWidgets, don't you?). 'DWORD'
sounds to me like a define/typedef of 'long' (32bit+ type), so, maybe use
that if you can. Just something to think about, the choice is always yours.

--
Bob R
POVrookie
Apr 25 '07 #14
On Apr 24, 11:16 pm, JoeC <enki...@yahoo. comwrote:
On Apr 23, 10:48 pm, Kirit Sælensminde <kirit.saelensm i...@gmail.com>
wrote:
On Apr 24, 9:10 am, JoeC <enki...@yahoo. comwrote:
On Apr 20, 11:40 pm, Kirit Sælensminde <kirit.saelensm i...@gmail.com>
wrote:
On Apr 20, 10:53 pm, JoeC <enki...@yahoo. comwrote:I have been writing games and I also read about good programming
techniques. I tend to create large objects that do lots of things. A
good example I have is a unit object. The object controls and holds
everything a unit in my game is supposed to do. What are some some
cures for this kind of large object or are they OK because they
represent one thing. If not what are better ways to design objects
that behave the same way. Would it be better to use inheritance or
friends and where can I look to be able to do the same thing in a
better way?
Here is a unit from a completed game I created. The header file at
least to give an idea of one of my larger objects.
[snip huge class]
You're using the class more like a namespace here. Objects should be
cohesive, which is a word that takes a look of thinking about to
really understand. A simple definition is that they should do only one
thing and do that one thing well. The smaller the scope of the one
thing they do the better.
Look athttp://c2.com/cgi/wiki?ClassicOoA ntiPatternsande speciallyhttp://c2.co...
K
This is what I am asking. I use namespace std other than that I have
little experience or information on creating or using one. Most of
the stuff in my unit objects affect other thing in that object. I can
understand the graphics part and it has graphics and it would be good
to create a movable object and derive that into my class. I came up
with this after the game was done when I went back and tried to learn
lessons from my project.
As an exercise see how small you can make your class by putting things
into helper classes. Look for things that are logically grouped. It'll
take you a while to work through the dependencies to get them right,
but your aim is to try to reduce your main class into the absolute
minimum that you can.
When you do it you should try to arrange the classes such that the
members of the unit class don't need to know that unit exists. I.e.
that every class you use as an attribute doesn't know anything about
the class that uses it. This is an ideal and isn't always possible,
but think very carefully before breaking it -- never do it until
you've exhausted every other possibility.
K

I have looked into java a bit and everything is an object there.

For this object the best I can do is seperate out the movment into a
movable class. It seems like every variable should be an object.
Object, set and get. It just seems like a great deal of unnecessary
overhead. I know it is a simple question but what is the rational for
such small classes? Most of the class are these facors:
I know what you mean about it seeming to be a lot of work. The flip
side is that collapsing the small classes into the larger class is in
effect an optimisation and we all know that premature optimisation is
evil :-) This is why you get away with it on small projects, but on
larger projects it will start to cause problems. Eventually it can get
so bad that it is effectively impossible to continue to maintain the
software.

Take a read of a couple of my papers on encapsulation and accessors.

http://www.kirit.com/On%20following%...Thing%E2%84%A2

This first one describes why encapsulation came about and then
describes what sorts of attributes should get read and write
accessors.

http://www.kirit.com/C%2B%2B%20kille...et%20accessors

The second shows a simple template technique that can be used to write
accessors with virtually no typing overhead. By keeping the syntax in
a certain form they can be replaced with more complex accessors later
on without having to edit all the code that uses the class.

For the attributes that you listed I'm not really sure what they're
meant to do as the names aren't always that illuminating (for example
"move", "moved", "dmove"), so I'll give another example that may be
relevant.

Let's assume we split off a small class for storing the location of
the unit which has x() and y() members. So if we have an instance
"unit" we can do this:

unit.position() .x();
unit.position() .y();

Let's then assume that you write some A* path finding so that the unit
can come up with a plan for how it will move over the next few
minutes. It can store this in a list now:

Attribute< std::list< Location plan;

*unit.plan().be gin(); // The next waypoint the unit is moving towards.

By simply splitting this little class out you've gained the ability to
handle a bit of forward planning for your units. If you'd kept the x/y
co-ordinates within the unit directly then this extra bit would never
have occurred to you. You could still manage the same thing, but it
would have been much more messy.

To update the location you could have this:

unit.position() .x( unit.position() .x() + 1 );
unit.position() .y( unit.position() .y() + 1 );

This is what you'd do if you didn't have the Location class, but with
it you can do something a bit neater. What if Location contained a
moveTowards() member?

unit.position() .moveTowards( *unit.plan().be gin(), unit.speed() );

Now you've tied together the position, the speed and the plan in a
fairly neat way. You also get the benefit that the movement
calculations are in one place (moveTowards).

These benefits may not be obvious to start with, but if you try
working in this way you'll see that what you're doing is keeping a lid
on the complexity. It means that you can tackle something much larger
and much more complex because everything is managed into smaller
chunks.
K

Apr 26 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2915
by: DJTB | last post by:
zodb-dev@zope.org] Hi, I'm having problems storing large amounts of objects in a ZODB. After committing changes to the database, elements are not cleared from memory. Since the number of objects I'd like to store in the ZODB is too large to fit in RAM, my program gets killed with signal 11 or signal 9... Below a minimal working (or actually: it doesn't work because of memory
3
4454
by: WinstonSmith | last post by:
Hello everyone, I got a problem about GC when creating large fields (some MB), set reference to null and call GC.Collect. Not all virtual mem is released. Situation improved in .net 1.1 but not in a full satisfying way. Has anyone a solution to handle large object in safe code? (Unsafe mem management works, but has serious disadvantages on serialization.)
6
3972
by: Jeff Williams | last post by:
Ok, everyone loves to talk about dynamic arrays and ptr's etc, they provide endless conversation :) so here goes: Will this leak memory (my intuition says yes): void foo(vector<int*>& vec) { int* pInts = new int; for(int i = 0; i < 100; i++) vec.push_back(pInts);
0
1997
by: pruebauno | last post by:
Hello all, I am having issues compiling Python with large file support. I tried forcing the configure script to add it but then it bombs in the make process. Any help will be appreciated. Information: Architecture: PowerPc on AIX version 5 Compiler:
3
3064
by: pertheli | last post by:
Hello, I have a large array of pointer to some object. I have to run test such that every possible pair in the array is tested. eg. if A,B,C,D are items of the array, possible pairs are AB, AC, AD, BC and CD. I'm using two nested for loop as below, but it is running very slow whenever I access any data in the second loop.
18
1669
by: Kamen Yotov | last post by:
hi all, i first posted this on http://msdn.microsoft.com/vcsharp/team/language/ask/default.aspx (ask a c# language designer) a couple of days ago, but no response so far... therefore, i am pasting it here as well... enjoy! (you can skip to the source at the end of the message if you like...) Consider:
8
1554
by: Chris | last post by:
I had a OO model worked out which involved forms populating new instances of a class and being stored in an ArrayList. I'm now moving this over to a DB backend rather than a file - I've been wondering if I even need to create objects now? Could I just work directly with the DB e.g. form data will pass directly to db handled in click events, the listview of objects can be read straight from db (no need to instantiate classes). I guess I'm...
7
6443
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are now thown out of the array released properly by the CLI?
5
2280
by: J055 | last post by:
Hi The following code works on my develeopment machine using the VS web server. When I run the application on 2 other Windows 2003/IIS 6 servers no caching seems to take place at all. Can someone explain what I might be doing wrong or what to look out for? What's the differece between IIS and VS web server? The IIS servers seem to have enough memory. public DataTable GetAll() {
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10329
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8974
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.