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
[Translation from C++ code to c#]Problem with building an FSM type ofI am reading the book "Programming Game AI by Example" and w
Page 1 of 1
5 Replies - 3306 Views - Last Post: 20 February 2008 - 09:51 AM
#1
[Translation from C++ code to c#]Problem with building an FSM type of
Posted 13 February 2008 - 10:28 AM
Replies To: [Translation from C++ code to c#]Problem with building an FSM type of
#2
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
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
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
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
#3
Re: [Translation from C++ code to c#]Problem with building an FSM type of
Posted 16 February 2008 - 02:27 PM
The State class:
EenterMineAndDigForNugget Class that inherits State
Implementation of Instance() function that returns a pointer to static varible of instance of the same class.
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:
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 ?
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 ?
#4
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
EnterMineAndDigForNugget.cs
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.
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.
#5
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:
That looked too wired to me to work
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:
And I create it in my miner class.
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.
Stack<State<Miner>> state = new Stack<State<Miner>>();
That looked too wired to me to work
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.
#6
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.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|