-
Modular Game Design
Ok dose every one remember this article http://www.cprogramming.com/tutorial/gamedesign.html
I have made a few engines with the old school style and i like this approach better. But there is one thing i am having problems with.
Ok the article stipulates that Execute() member function returns a pointer to a Module, which is the next Module. This all make nice since because the next Module will have the Module base class and we are only concerned with accessing the functions that the Module pointer can see. But the returned Module is a pointer to the next Module, So do i just make however many Module pointer slots i need in the derived class and in the Initialize member function assign all of them to a bunch of pre-made instances of Pointers to Modules that are assigned to a 'new' derived class instance? This is exactly what i did but the classes are having trouble seeing those instances. Am i going about this all wrong or what? Thanks for the help
This is confusing to explain so here is the Test code that is not working
Code:
#include <iostream>
using namespace std;
class Module
{
public:
virtual void Initialize()=0;
virtual Module* Execute()=0;
virtual void Shutdown()=0;
}
class Test : public Module
{
protected:
int number;
char word;
Module* NextModule;
public:
//general methods
void Initialize();
Module* Execute();
void Shutdown();
Test();
~Test();
//accessor methods
int GetNumber() {return number;};
char GetChar() {return word;};
}
class TestB : public Module
{
protected:
int number;
char word;
Module* NextModule;
public:
//general methods
void Initialize();
Module* Execute();
void Shutdown();
TestB();
~TestB();
//accessor methods
int GetNumber() {return number;};
char GetChar() {return word;};
}
Module* pTestB = new TestB;
Module* pTest = new Test;
Module* CurrentModule;
Module* LastModule;
main()
{
int end = 0;
CurrentModule = pTest;
LastModule = CurrentModule;
while(end == 0)
{
if((CurrentModule=CurrentModule->Initialize()) != LastModule)
{
cout<<"inside if"<<endl;
LastModule->Shutdown();
CurrentModule->Initialize();
cout<<"Would You Like to Quit? " <<endl;
cin>>endl;
}
if(end == 0)
{
cout<<"Inside Second If" <<endl;
cout<<"Would You Like to Quit? " <<endl;
cin>>end;
}
delete Test;
delete pTestB;
}
}
void Test::Initialize()
{
number = 1;
word = 'F';
NextModule = pTestB;
Execute();
}
Module* Test::Execute()
{
cout <<number <<endl;
cout <<word <<endl;
return NextModule;
}
void Test::Shutdown()
{
number = 20;
word = 'D';
cout <<number <<endl;
cout <<word <<endl;
}
void TestB::Initialize()
{
number = 500;
word = 'S';
NextModule = pTest;
Execute();
}
Module* TestB::Execute()
{
cout <<number <<endl;
cout <<word <<endl;
return NextModule;
}
void TestB::Shutdown()
{
number = 20;
word = 'D';
cout <<number <<endl;
cout <<word <<endl;
}
-
you're never calling Execute. You also have several compiler errors. If your compiler isn't squaking at you, I would like to know which compiler you're using.
-
you're also not keeping track of the current and last module correctly. I would suggest something more along the lines of the following:
Code:
LastModule = CurrentModule;
CurrentModule = CurrentModule->Execute();
if(CurrentModule != LastModule)
{
LastModule->Shutdown();
CurrentModule->Initialize();
}
-
First off Execute is called within the Initialize function. Second I fixed a problem with the loop, but that is not the problem. I dont even know if i am going about this in the way intended, or a legal way for that matter. And the classes are not seeing the Modular Pointers to 'new' instances of derived Modules. help on that plz
-
Ah yes, you do call execute with in Initialize. But that doesn't make sense. The point of the Init, Exec, and Shutdown functions is to seperate those responsibilities.
I'm not quite sure what you mean by your classes not seeing the pointers. Are the pointers NULL, or perhaps it seems as if the expected functions aren't being called?
I think you have the right idea, but need to take a step back, look at the program as a whole, and organize it a bit.
-
Why did i seperate the Execute and Intialize functions you say? Well when are you going to intialize an object and not Execute it. really all this is just for seperation and orginization
-
lets say you have 5 modules. You don't want to execute them all at once, but you may want to Initialize them all at program startup so that you have less load times later on when switching between modules.
I'll also point out that you never capture the module returned by Execute, because you never explicitly call it, and Initialize does not return it.
This is not to say that you're wrong for putting Execute in Initialize, but keep in mind what's happening in your code because of this.
Code:
while(end == 0)
{
//we Initialize a Test object, which also executes it. So right from the get go,
//CurrentModule != LastModule
//Also, this should give you an error, because Initialize does not return anything
if((CurrentModule=CurrentModule->Initialize()) != LastModule)
{
cout<<"inside if"<<endl;
//shut down Test module, and then Initialize testB module, and execute it.
//LastModule is never changed, so the test object is always the last module.
LastModule->Shutdown();
CurrentModule->Initialize();
cout<<"Would You Like to Quit? " <<endl;
cin>>endl;
}
if(end == 0)
{
cout<<"Inside Second If" <<endl;
cout<<"Would You Like to Quit? " <<endl;
cin>>end;
}
}
I will also say once again that I'm surprised you get this code to compile. When I fixed the errors and compiled, the program runs as expected. Not as intended, as expected.
-
o yea i got it working and i call them separately now because i was experiencing those things and most compilers wont let you ignore a void return type by asking it to return. thanks for you input though