May sound funny, but that's what the question is about.
Suppose you have this situation:
http://i48.tinypic.com/whl6pk.jpg
Red squares are data types. Hexagons are methods and Journal is the main issue here. Or more exactly, how the link between Game and Journal should be interpreted. Basically, a lot of game components (instantiated inside of Game) will have to gain access to the Journal functionality. The two options that I have thought of myself are the following:
Journal could be a namespace w/ 'globally' used methods and fields. Something like:
journal::AddEntry(someEntry);
OR I could make Journal a Class, just like Game, and instantiate it inside of Game, and then pass a reference to Journal to each property of Game that needs to use the journal. Something like:
GameProperty *property = new GameProperty(&ourJournal);
//...inside of property:
Journal *referenceToJournal;
But then, if I'd delete referenceToJournal, the actual ourJournal object would be deleted too, right? I'm pretty sure this just complicates things by a lot, and having lots of references towards the same object is a bad habit.
I'm kind of thinking that the:
namespace Journal
{
//Fields and methods go here
}
...
#include "Journal.h"
Journal::method();
is the best way to achieve global visibility for the Journal without much stress. But it kind of hurts, since I come from a C# & XNA background where everything is object oriented. Which is the main reason I have thought of a:
class Game()
{
void loadcontent();
void update();
void draw();
}
logic.
How would you attempt to achieve given the scenario? Instantiate a Journal inside of Game and then constantly pass a reference to Journal to all the other game components (instantiated inside of Game), or just treat everything (including Game) as namespaces to provide 'global' access to all required components? I'm also thinking about classes with public static fields and methods, but that'd be just like namespaces, at least from my point of view. Here is some more supportive code, to better describe my view on things (pseudocode for the namespace idea):
//FILE Journal.cpp (implementation of Journal.h)
namespace Journal
{
struct JournalEntry
{
INT QuestID;
String Description;
};
Vector<JournalEntry> Entries;
void AddEntry(JournalEntry theEntry)
{
Entries.Add(Entry);
}
/*
* Used at getting all text that has to be shown on the
*/
Vector<String> GetDescriptionsForEntriesWithQuestID(INT theQuestID)
{
Vector<String> Descriptions;
foreach (JournalEntry jEntry in Entries)
{
if (jEntry.QuestID == theQuestID)
{
Descriptions.Add(jEntry.Description);
}
}
return Descriptions;
}
};
//FILE GameComponent.cpp implementation of GameComponent.h
#include "Journal.h"
void GameComponent::ShowInfo()
{
Vector<String> info = Journal::GetDescriptionsForEntriesWithQuestID(3);
//go through the collection and do stuff
}
Thanks!