OOP quesiton

Started by
15 comments, last by Pseudo_Code 23 years, 1 month ago
All the suggestions that have been posted so far have been most useful. Loads of thanks!
Advertisement
Hmm... Performing an action between 2 ''characters'' will probably require some middle tier for managing the action. In the existing model, how does ''object 1''( which is your character), know about ''object 2'' which it wants to interact with( ''object 2'' may be a door, or a chest, or another character[ NPC or PC ] ) ? Traditionally, the ability for 1 game object to act on another game object would depend on both objects properties. So, if the character wanted to ''attack'' another character, a middle tier( like an interaction engine, or something that is generic ) would get a pointer to both characters, and manage the data checking. Using this method, the Character object doesn''t have to know about all the other objects that it can interact with( but the middle tier, which may be the whole engine would know about all the objects ).
I have to disagree with you Houdini, though I hope I''m doing so civilly (I''m certainly trying). My guidelines for passing variables to/from functions:
For inputs (will not be changed by function):
- always pass built-in types (char/bool/int/double/etc.) by value
- always pass user types (struct/class/union) by const reference or const pointer

You are wrong about a const reference to an int being the same as passing an integer.

Functionally, the int passed by value is a copy and can be used in a function, where as the const reference cannot. For instance, you would be able to do something like this:
void peform (int strength){  while (--strength)  {    do_some_action ();  }} 

You cannot do this with a const reference, because you aren''t allowed to change the value of strength.

Furthermore, and more importantly, it''s less efficient to use a const reference. The reference must be dereferenced before it is used, necessitating an extra move instruction; I have individually verified this with MSVC in release mode. If you use references for all parameters, your code will be slower.
Inheritence


:: Jamie Interactive ::
This is a little off-topic, but this was the first thing that came to mind... maybe its usefull...maybe it doesn''t make much sense...

  class CCharacter{  int iStrength;protected:  virtual void OnPerform(int iStrength) { }};class CFighter : public CCharacter{protected:  void OnPerform(int iStrength)  {  }};  


Why exactly are you making an Attack class? Do you have many types of attacking, that differ that much? It looks kinda strange making a class for an attack.

Can you explain some more?...

Each attack will have a different image, animation, type, ect. Some will take two "energy bars," others will take one. Some are specials, while others may be supers that consume built energy. There are a lot of varying aspects to different attacks.

Each character will also have three special attacks, and two super attacks.

I just thought that a class was appropriate.

Edited by - pseudo_code on March 16, 2001 7:22:51 PM
Sounds to me like Attack::perform () also needs to modify the Character (consuming energy, etc.), so passing strength directly to perform will not work. Anyway, in the end perform might have ended up with half of the Character in the params

But since perform () needs both read and write access to both Attack and Character class , it seems to me that it rather belongs in the Character class. The Attack class should only have methods which solely concern the attack, like initialization.

class Character
{
private:
int strength;
Attack attack[6];

public:
performAttack (int nAttack) {/**/}
}


It might not be 100% POO, but IMHO the code will be a lot cleaner, since you don''t have to use in perform() the reference to the parent Character class and those 100% POO read/write Character methods.

This topic is closed to new replies.

Advertisement