Jump to content
  • Advertisement
Sign in to follow this  
Rip7

Unity Ruleset

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I had already an discussion (http://www.gamedev.net/community/forums/topic.asp?topic_id=322249) about how to make a set of rules for different roles some time ago. Now I have finally got the time to work on it. I wrote some test code and everything is working. I only don't like this part:
#include <iostream>

using namespace std;

#ifndef __COMMANDS_H__
#define __COMMANDS_H__

class Policy;
class Role;
	
class Command
{
 public:
 typedef int Result;
 virtual Command::Result Process () = 0; //result = backend, par = tokenizer
 virtual bool SetPolicy (Policy* Name) = 0;
 virtual bool Verify (Role* Rolename) = 0;
	 
};

class All;
class Attack : public Command
{
        virtual bool SetPolicy (Policy* Name);
	virtual bool Verify (Role* Rolename);
	 
	virtual Command::Result Process () {cout << "ATTACK :)" << endl;}
	 
	private:
	All *mPolicy;
};

class Move : public Command
{
        virtual bool SetPolicy (Policy* Name);
	virtual bool Verify (Role* Rolename);
	 
	virtual Command::Result Process () {cout << "MOVE :)" << endl;}
	 
	private:
	OtherPolicy *mPolicy;
};
#endif // __COMMANDS_H__
As you can see, virtual bool SetPolicy (Policy* Name); virtual bool Verify (Role* Rolename); must be repeated for every command (and the implementation is alway's the same). I tried to solve it by using templates but it became only a mess. Maybe anyone knows a way to simplify this, because I don't wan't to change all the code (It's maybe not so good, but I like it). Please look @ the 'full' source code to get a good idee of the implementation http://rip7.servehttp.com/Revolutions/roles.tar (Maybe it's also interesting for other people) I'd love to hear other comments or idea's to make the implementation better. Thanks [Edited by - Rip7 on August 19, 2005 2:34:04 PM]

Share this post


Link to post
Share on other sites
Advertisement
Make generic functions and stick them in your class Command:


class Command
{
public:
typedef int Result;
enum Type
{
Attack = 0,
Move = 1,
//...
};
Type commandType;

Command::Result process()
{
switch (commandType)
{
case Attack:
// Blah blah
break;

case Move:
// Blah blah
break;

// ...
}
};

bool SetPolicy(Policy *name);
bool Verify(Role *roleName);
// ...
};


This will also eliminate the need for creating excess classes, allowing an easier time for you to black box your code. It makes the code a little trickier, but will end up being a tad easier in the future. Or, you could use function pointers:


class Command
{
public:
typedef int Result;
typedef Command::Result (*cmdsetpolicyptr)(Policy *name) LPFN_COMMAND_SETPOLICY;
typedef Command::Result (*cmdverifyptr)(Role *roleName) LPFN_COMMAND_VERIFY;

LPFN_COMMAND_SETPOLICY SetPolicy;
LPFN_COMMAND_VERIFY Verify;

// ...
};


Then, for each class (Attack, Move, etc.), you still need to write each function, just so long as they're different. But in your headers, you don't need to include the definitions for the virtual SetPolicy or Verify functions. Niether is probably as elegant as you'd like, though.

Share this post


Link to post
Share on other sites
Yeah agreed,

If SetPolicy() and Verify() always have the same implementation across all child classes, there is no need for them to be pure virtuals. If you want the possibility for them to be overriden by a child class then leave them as virtual.



class Command
{
protected:
virtual result SetPolicy(Policy* name); // no = 0
virtual bool Verify(Role* role); // same same
};

Command::result Command::SetPolicy(Policy* name)
{
// generic implementation here
return 0;
}

bool Command::Verify(Role* role)
{
// generic implementation here
return true;
}

// So now you can still inherit from this class
class Attack : public Command
{
// and overide if you so wish
result SetPolicy(Policy* name);
};

// or not override the methods
class Move : public Command
{
virtual Command::Result Process () {cout << "MOVE :)" << endl;}
};



It seems you probably know this, though by your use of a normal virtual in the Move::Process() method. So quite possibly I have misunderstood your question. Yet, I hope this has helped.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!