Archived

This topic is now archived and is closed to further replies.

klio

c++ classes of game objects

Recommended Posts

Ok... Now can anybody tell me how are the RTS game entities (units, buildings, resources, etc...) are implemented in c++ (i mean the game objects, i do not need c++ tutorials)? Does anybody have such kind of sources? What i mean is - every unit in almost any RTS has its own features and characteristics: health, power, speed, etc. The general questions are: - What are the best practices using C++ and objects for game development? - What game elements are best created with C++ classes, and what elements are best created with good old C?

Share this post


Link to post
Share on other sites
this is one way (which i think is best, but that's not exactly the same as th ebest way):

you could make an abstract base class for units (let's say CUnit), which has data members for things that every unit has (i.e. health, speed, whatever), and all the member functions that will be necessary to make the units do what they must do (i.e. goToPosition(), attack(), etc). make the member functions virtual.

then, you derive unit types from that base class, and override the virtual functions. for example, the infantry attack() function might shoot a machine gun at a target, the grenadeer (sp?) unit throws grenades; all this goes in the overridden virtual functions.

then, you create your units when you need them and add them to your list/array/whatever where you store all the units with pointers to the base class; because the member functions are virtual, the computer decides which to use when you call the function from the base pointer:


// psuedocode
CUnit* UnitArray[NumUnits];
for(loop through all units, using int t as the index)
{
CUnitPointer[t]->attack(); // have all units attack
// in their own special way
// if this pointer points to an
// infantry, the computer calls the infantry attack() method.
// if this pointer points to a grenadeer (sp?),
// it calls the grenadeer attack().
// etc...
};


or something to that effect. sorry if that doesn't make sense, i am quite overtired at the moment. read up on virtual functions in classes and inheritance.

HTH

[edited by - krez on December 26, 2002 8:55:50 PM]

Share this post


Link to post
Share on other sites
I don''t need c++ programming howtos, i know c++ and its concepts... I want to know about the concrete data structures and classes. How are classes organized in other games?

Share this post


Link to post
Share on other sites
quote:
Original post by klio
I want to know about the concrete data structures and classes.


I want to know about weaponsmithing. I won''t tell you what I want to know about it, you should figure that out for yourself.

See where I''m going with this? If not, give up programming forever.


swift URL | browse my bookmarks [server may not always be on]

Sensimed | Info/download

Share this post


Link to post
Share on other sites
wtf?

thats totally up to you, thats the design. here is a crappy example:


+RTSUnit
+-+Vehicles
| +-+Jeep
| +-+Tank
| +-+Truck
+-+Planes
| +-+Jet
| +-+Transport
| +-+Helicopter
+-+People
| +-+Riflemen
| +-+Worker
| +-+Laserdude


[edited by - billybob on December 26, 2002 9:44:37 PM]

[edited by - billybob on December 26, 2002 9:45:16 PM]

Share this post


Link to post
Share on other sites
Um, krez essentially covered that, buddy.

But since you can''t think for yourself, here''s a psuedo-example for you:


robotUnit {
Health
Armor
primaryAttack
secondAttack
rangeOfVision
Move
}

robotUnit Tank {
Move {
//roll foward
}
}

Share this post


Link to post
Share on other sites
klio''s question is rather ambiguous based on his replies to other posters offering help. the hierarchy request is rather wildly different from the initial post, making it hard to pinpoint wat u really need.

i think a simple and working method of implementing and manipulating the class hierarchy is to do it the Krez-way One mixing C++ and C, thats largely a user-preference. However, if u''re doing an inheritance based hierarchy anyway, might as well go C++ all the way... but u''re the programmer.

u''re final post bout requesting for an RTS unit hierarchy.. it sounds more like u need a C&C game manual.. haha. Not sure if i''m interpreting it rite, but if u want a unit-by-unit list, u''re better of researching based on real games instead of on this forum.
But of course, if i were wrong (and i may very well be) .. i would request u rephrase ur requests more explicitly. We''re not SMS-ing here... u need help, be detailed so that u get ur answers straight.

Share this post


Link to post
Share on other sites
Ok. There''s my question again. Where do i find a RTS game writen in c++ and which is open-sourced? (I don''t mean GPL or BSD license, i mean that i want to be able to look through the sources).

Share this post


Link to post
Share on other sites
Ok. There''s my question again. Where do i find a RTS game writen in c++ and which is open-sourced? (I don''t mean GPL or BSD license, i mean that i want to be able to look through the sources).

Share this post


Link to post
Share on other sites


  

class CUnit
{
public:
CVector3 mPosition;
CVector3 mVelocity;
CQuaternion mRotation
...
};

class CTruck : public CUnit
{
public:
enCargoType mCargo;
ulong mCargoAmount;
...
};

class CTank : public CUnit
{
public:
enCannonType mCannon;
ulong mAmunitationAmnt.
...
};


There you have 2 units. Everyone have answered your question more or less. Use a base class for the generic behaviour/data which applies to all units. Derive the baseclass and put specific data/methods in the children.

Easy peasy and standard C++ programming. That is how you implement RTS units.




What does God want?
Does God want goodness or the choice of goodness?
Is a man who chooses the bad perhaps in some way better than a man who has the good imposed upon him?

Share this post


Link to post
Share on other sites
quote:
Original post by klio
Ok. There''s my question again. Where do i find a RTS game writen in c++ and which is open-sourced? (I don''t mean GPL or BSD license, i mean that i want to be able to look through the sources).


... If your making your own game, what you need other peoples sources for? Why not just copy their completed product and tell people you made that?



What does God want?
Does God want goodness or the choice of goodness?
Is a man who chooses the bad perhaps in some way better than a man who has the good imposed upon him?

Share this post


Link to post
Share on other sites
Have you ever heard of such a thing as education and learning. Or have you ever looked through somebody''s sources to understand how things work? I don''t need to copy and paste.

Share this post


Link to post
Share on other sites
But there was plenty of answers. This is basic C++, which you have claimed that you know already.

You put generic stuff in a parent class and derive it. The child classes have unit specific code. Just overload/override methods that needs special functionality, which isnt in the baseclass.

Not to disencourage you or sound harsh, but maybe you should reread your c++ books and/or go back to basics before you start with a huge project like a (mmo)rts?



What does God want?
Does God want goodness or the choice of goodness?
Is a man who chooses the bad perhaps in some way better than a man who has the good imposed upon him?

Share this post


Link to post
Share on other sites
quote:
Original post by klio
i know c++ indeed. you''ve misunderstood the question...


Humbug, if you knew it, you wouldnt have to ask such a trivial question. And even if you did, you would have got the picture by the answers you already got.





What does God want?
Does God want goodness or the choice of goodness?
Is a man who chooses the bad perhaps in some way better than a man who has the good imposed upon him?

Share this post


Link to post
Share on other sites
You got 5 answers how to implement the structure, even a description how it works.

It doesnt matter if its C or C++, the idea is the same. Surely your able to translate from C to C++? Just read in a C++ book or online resource about inherits and polymorphism and then your done.



What does God want?
Does God want goodness or the choice of goodness?
Is a man who chooses the bad perhaps in some way better than a man who has the good imposed upon him?

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.