[Translation from C++ code to c#]Problem with building an FSM type of

I am reading the book "Programming Game AI by Example" and w

Page 1 of 1

5 Replies - 3644 Views - Last Post: 20 February 2008 - 09:51 AM Rate Topic: -----

#1 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

[Translation from C++ code to c#]Problem with building an FSM type of

Post icon  Posted 13 February 2008 - 10:28 AM

Hello all. I have a problem with rebuilding a FSM code from a book on c#.

The structure of the C++ code is as follows:

class BaseGameEntity // abstract class to inherit from for all monsters/creatures etc.

class Miner : BaseGameEntity // specific creature
class Wife : BaseGameEntity // specific creature (the wife of the miner)

class State // abstract class for a state

class EnterMineAndDigForNugget : State // particular state
class VisitBankAndDepositGold : State // particular state

Now in the c++ code the State class and its children are implemented with template class and are static! One instance of each state.
Problem is in C# I have problem having static classes and inheritance (and generics). I want to make use of inheritance and insure that there is going to be only one instance of a State and I want to have a global access to that state (that is why I wanted to be static) but then I couldnt make it generic and have inheritance.

Another thing on my mind: I can avoid making it generic by basic general object and then casting it. But I still want to have inheritance and static states accessed globally.

I am not sure if I managed to explain myself. I was thinking of making a static class to manage the states but I do not seem to be able to get the write syntax working. All tips are appreciated! Seems pretty unclear so ask questions if u feel like helping.

Evgeni

Is This A Good Question/Topic? 0
  • +

Replies To: [Translation from C++ code to c#]Problem with building an FSM type of

#2 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: [Translation from C++ code to c#]Problem with building an FSM type of

Posted 14 February 2008 - 03:36 PM

IIRC there is no such thing as a static class in c++. You can have static: variables, functions, class data members and class functions. Some one correct me if I'm wrong

In C# you can have a static class. This means that you cannot create an instance of the class. Also every member of the class must be static
static public MyClass
{
....
};



As far as I know there is no such equivalent with c++. I can do this
and nothing is stopping me from creating an instance
template<class T>
class MyBaseClass
{
public :
	static T x;
	
};

template<class T> T MyBaseClass<T>::x;

int main()
{
	MyBaseClass<int> *b1 = new MyBaseClass<int>();
	MyBaseClass<int> *b2 = new MyBaseClass<int>();

	b1->x = 44;
	std::cout << "b1: " << b1->x << " b2: " << b2->x << std::endl;
	b2->x = 100;
	std::cout << "b1: " << b1->x << " b2: " << b2->x << std::endl;

	delete b1;
	delete b2;
}


It would be helpful if you explained or showed the code regarding
Now in the c++ code the State class and its children are implemented with template class and are static!.

If you only one want one instance of a class and have global access to it, you should look at the singleton pattern

This post has been edited by skaoth: 14 February 2008 - 03:38 PM

Was This Post Helpful? 0
  • +
  • -

#3 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

Re: [Translation from C++ code to c#]Problem with building an FSM type of

Posted 16 February 2008 - 02:27 PM

The State class:
 template <class entity_type>
class State
{
public:

  virtual void Enter(entity_type*)=0;

  virtual void Execute(entity_type*)=0;

  virtual void Exit(entity_type*)=0;

  virtual ~State(){}
};


EenterMineAndDigForNugget Class that inherits State
class EnterMineAndDigForNugget : public State
{
private:

  EnterMineAndDigForNugget(){}

  //copy ctor and assignment should be private
  EnterMineAndDigForNugget(const EnterMineAndDigForNugget&);
  EnterMineAndDigForNugget& operator=(const EnterMineAndDigForNugget&);

public:

  //this is a singleton
  static EnterMineAndDigForNugget* Instance();
  
  virtual void Enter(Miner* miner);

  virtual void Execute(Miner* miner);

  virtual void Exit(Miner* miner);
};



Implementation of Instance() function that returns a pointer to static varible of instance of the same class.

EnterMineAndDigForNugget* EnterMineAndDigForNugget::Instance()
{
  static EnterMineAndDigForNugget instance;

  return &instance;
}



In C# I want to have a generic class so I can take not only references of class miner but of anything. I want to inherit from that class and make all my states for the different entities. What I have done now is:
 public abstract class State
	{
		public abstract void Enter(Miner entity);
		public abstract void Execute(Miner entity);
		public abstract void Exit(Miner entity);
	}



But now I need to add another entity to my simulation. What I am going to do is change Miner to generic Object class and cast it later when I inherit from it. But I bet there is a better way. But when I make the class generic I have problems with it. (Maybe I have some syntax problem.) In short: How to inherit from generic class ?
Was This Post Helpful? 0
  • +
  • -

#4 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: [Translation from C++ code to c#]Problem with building an FSM type of

Posted 17 February 2008 - 12:47 AM

Its good that you've thought of a work around. Sometimes you just have to get
things to work. Its good to hear that you have a feeling that there is a better way.
I get that feeling quit often also

Hopefully this will help.
If you are looking to mimic the C++ code you provided here is what your class should look like

State.cs
	public abstract class State<T>
	{
		public abstract void Enter(T entity);
		public abstract void Execute(T entity);
		public abstract void Exit(T entity);
	};



EnterMineAndDigForNugget.cs
 public  class EnterMineAndDigForNugget : State<Miner>
	{

		// State object implementation
		public override void Enter(Miner entity) {//TODO: code}
		public override void Execute(Miner entity) {}//TODO: code}
		public override void Exit(Miner entity){//TODO: code}
	};



You will of course need to define the Miner class for this to work. I don't program games generally but it makes sense that miner be a base classand you can create a hierarcy; say a GoldMiner class, CoalMiner class etc... but its really up to you.

Hope that helps. If not post the classes your having problems with.
Was This Post Helpful? 0
  • +
  • -

#5 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

Re: [Translation from C++ code to c#]Problem with building an FSM type of

Posted 17 February 2008 - 09:54 AM

!!! Yay! Here we go... it works. I was just nub about the c# syntax. It gets kinda confusing. I get some stuff like that:

 Stack<State<Miner>> state = new Stack<State<Miner>>();


That looked too wired to me to work :D But it works perfectly. Kudos.

Now another thing. How to create that singleton class that the guy makes in his c++ code?
What I am doing now is make a class that holds an instance of each state:
  public class StateManager
	{
		public State<Miner> enterMineAndDigForNugget;
		public State<Miner> visitBankAndDepositGold;
		public State<Miner> goHomeAndSleepTilRested;
		public State<Miner> quenchThirst;
		public StateManager()
		{
			enterMineAndDigForNugget = new EnterMineAndDigForNugget();
			visitBankAndDepositGold = new VisitBankAndDepositGold();
			goHomeAndSleepTilRested = new GoHomeAndSleepTilRested();
			quenchThirst = new QuenchThirst();
		}
	}


And I create it in my miner class.
public Miner(string name) : base (name)
		{
			stateManager = new StateManager();
			state.Push(stateManager.enterMineAndDigForNugget);
		}


But what If I want to have a second miner? Create another class like that? What if I need 100 miners? So I need it static. Any suggestions how I can make it static would be great. I cannot make it so far. I need a globally accessible class (static) that will give me access to instance of each state.
Was This Post Helpful? 0
  • +
  • -

#6 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

Re: [Translation from C++ code to c#]Problem with building an FSM type of

Posted 20 February 2008 - 09:51 AM

You can close this thread I guess. I have managed to make it work. :) Thank you for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1