Conceptual Programming - Weak Objects and Interact

posted in Old Stuff
Published September 27, 2008
Advertisement
I've been trying to think of some "different" approaches to representing objects in classes. I began thinking about of the basic class members and functions that are normally implemented. For example, consider a simple player class that had such member variables as hitpoints, x, y, ammo, and function such as Move, Fire, Jump, etc... in those cases there is a strong mapping of variables and functions; they are explicitly defined. Interacting with the objects of that type just involves using the public functions. Pretty standard design.

So, with this new conceptual programming I am playing with things are setup like this: All objects derive from cEntity. The cEntity object has tow maps of values, strings and doubles. The cEntity class has API functions to Get, Set, Modfiy, or Remove attributes. The cEntity class also has private API functions that are invoked when those public API functions are used. Here is that class:

class cEntity{private:	CRITICAL_SECTION cs;	std::map classStrings;	std::mapdouble
> classNumbers;

protected:
bool SetNumber(const std::string & name, double value);
bool RemoveNumber(const std::string & name);
bool ModifyNumber(const std::string & name, double value, double * outValue);
bool GetNumber(const std::string & name, double & outValue);

bool SetString(const std::string & name, std::string value);
bool RemoveString(const std::string & name);
bool GetString(const std::string & name, std::string & outValue);

virtual bool OnSetNumber(cEntity * entity, const std::string & name, double & value);
virtual bool OnRemoveNumber(cEntity * entity, const std::string & name);
virtual bool OnModifyNumber(cEntity * entity, const std::string & name, double value, double * outValue);
virtual bool OnGetNumber(cEntity * entity, const std::string & name, double & outValue);

virtual bool OnSetString(cEntity * entity, const std::string & name, std::string value);
virtual bool OnRemoveString(cEntity * entity, const std::string & name);
virtual bool OnGetString(cEntity * entity, const std::string & name, std::string & outValue);

public:
cEntity();
virtual ~cEntity();
cEntity(const cEntity & rhs);
cEntity & operator =(const cEntity & rhs);

bool SetNumber(cEntity * entity, const std::string & name, double value);
bool RemoveNumber(cEntity * entity, const std::string & name);
bool ModifyNumber(cEntity * entity, const std::string & name, double value, double * outValue);
bool GetNumber(cEntity * entity, const std::string & name, double & outValue);

bool SetString(cEntity * entity, const std::string & name, std::string value);
bool RemoveString(cEntity * entity, const std::string & name);
bool GetString(cEntity * entity, const std::string & name, std::string & outValue);

virtual void Interact(cEntity * entity);

virtual void Update(double delta);
};



Now this class is still being worked on but that is the basics of it all. The Interact function is an interface function that represents one entity interacting with another. The update would represent real time "processing" of all entities, but is not used yet.

Here is an example use of this setup.

#ifndef ENTITY_H_	#include "Entity.h"#endif//-----------------------------------------------------------------------------class cBox : public cEntity{private:public:	cBox()	{		SetString("name", "Box");		SetNumber("x", 0);		SetNumber("y", 0);		SetNumber("z", 0);		SetNumber("gravity", 1);	}};class cSpaceBox : public cEntity{private:public:	cSpaceBox()	{		SetString("name", "SpaceBox");		SetNumber("x", 0);		SetNumber("y", 0);		SetNumber("z", 0);	}};class cImpossibleBox : public cEntity{private:public:	cImpossibleBox()	{		SetString("name", "ImpossibleBox");		SetNumber("x", 0);		SetNumber("y", 0);		SetNumber("z", 0);		SetNumber("gravity", 1);	}	bool OnModifyNumber(cEntity * entity, const std::string & name, double value, double * outValue)	{		if(!entity || entity == this) return false;		if(name == "y")		{			std::string ename;			if(entity->GetString(this, "name", ename))			{				if(ename == "Gravity")					return false;			}		}		return true;	}};class cGravity : public cEntity{private:public:	cGravity()	{		SetString("name", "Gravity");		SetNumber("g", -9.17);	}	void Interact(cEntity * entity)	{		double g = 0;		double y = 0;		double gravity = 0;		if(entity->GetNumber(this, "gravity", gravity))		{			if(entity->GetNumber(this, "y", y))			{				GetNumber("g", g);				printf("Position started at: %f\n", y);				if(entity->ModifyNumber(this, "y", g * gravity, &y))				{					entity->GetNumber(this, "y", y);					printf("Position is now at: %f\n", y);				}				else				{					printf("Entity denied the affects of gravity.\n");				}			}			else			{				printf("Entity has not properties that can be affected by gravity.\n");			}		}		else		{			printf("Entity cannot be affected by gravity.\n");		}	}};//-----------------------------------------------------------------------------int main(int argc, char * argv[]){	cGravity gravity;	cBox box1;	cSpaceBox box2;	cImpossibleBox box3;	gravity.Interact(&box1);	printf("\n");	gravity.Interact(&box2);	printf("\n");	gravity.Interact(&box3);	printf("\n");	return 0;}//-----------------------------------------------------------------------------


After running the program, the output is:

Position started at: 0.000000Position is now at: -9.170000Entity cannot be affected by gravity.Position started at: 0.000000Entity denied the affects of gravity.Press any key to continue . . .


Whoa. Just imagine being able to have such control over any interactions in real life haha! Good bye gravity! Anyways, the design is meant to allow freedom and flexibility in terms of what an object represents as well as how it is modified. From a technological standpoint, anything would be possible as seen with preventing gravity. You could make gravity inversed so you actually float up or reduce/strengthen the effects of it. Of course, I am just using gravity as the prime example, any "force" would work the same, including things that we do not have, such as "side ways" gravity, which more or less would be a constant wind.

Moving on, I don't have any practical things to do with this concept, just experimenting. R&D so to speak. There are some problems that I've yet to really address. For example, let's assume I have my small universe of objects. Naturally due to how computers work, I will be handling Interactions and Updates in a sequential manner. Because of this, I do not have a way of implementing "order" or "priority" in the world. This would mean the implementation of the world is up to the actual program and not the class itself. I.e. one program might implement a number property that is a 1,2,3 and establishes a priority like that and another program might use a different system.

My idea was, if even possible, somehow have a game where players create their interactions with world objects. This is a bit vague so let me make up an example of what could be possible.

Start out with a cEntity that is a virtual tree. Let's say it has a number property of "wood" as 5000. Now, the player has their own cEntity that represents them. By intuition, the player Interacts with the tree and learns it has wood that is harvestable. For the sake of simplicity, let's say the player calls the ModifyNumber function of the tree with the "wood" property and a value of 1. The tree object verifies the operation in its OnModifyNumber and either allows the operation or denies it. Let's say it was successful and it then sets the wood property for the player to 1.

Not the best example and a lot of questions result from that, but that's the gist of it all. I think it'd be interesting if properly implemented, so I am going to keep playing with the idea until I get distracted by something else or make a breakthrough.
0 likes 5 comments

Comments

snk_kid
This seems a lot like a component-based game object system (or trying to be).
September 27, 2008 01:10 PM
Telastyn
Indeed, and a... overly flexible component based system at that. Personally, I think going somewhere with mix-in inheritance and more meta-programming sort of approach would be better. But that reminds me that I need to make a good COP example for my hobby language.
September 27, 2008 04:17 PM
Drew_Benton
Quote:Original post by snk_kid
This seems a lot like a component-based game object system (or trying to be).

Quote:Original post by Telastyn
Indeed, and a... overly flexible component based system at that. Personally, I think going somewhere with mix-in inheritance and more meta-programming sort of approach would be better. But that reminds me that I need to make a good COP example for my hobby language.


I wasn't quite thinking about component based designs when I was working on this, but I see the similarities now. See I really wanted to integrate in a scripting language rather than have to make "programs" that would interact the objects, but Lua was driving me mad. Might invest in learning that more solidly or a diff language before I try to jump in with it (yet another thing on the todo list)

That reminds me, I really should probably start reading this Design Patterns book because I am pretty weak on that knowledge overall. Looking back at some of my old ideas (oh jeeze I cringe at some of my posts to this day), such as

On Static Class/Global Variables
Void* Messaging for Classes
Self Managing Objects
Self Tracking Object Design

I've always been a fan and intrigued by such designs and I persist in trying to find something "fun" to do with it, but lately I understand why most of those will fail as I have them: concurrency. My new cEntity class is thread safe, from the ctor, copy ctor, assignment operator, and all member functions. However, there is a problem if I try making them track themselves, I must have a way to initialize a critical section before the 1st class instance is created and destructed after all class instances have been destructed. Some sort of manager or simple factory could encapsulate that, but then the objects themselves don't need to know of each other explicitly.

I'm too much of a "it needs to be right" person to settle with an implementation that I knowingly see as misimplemented, so I'm thinking it's just not possible in a sense of thread safety. Oh well, I tried [grin]

Anyways, I'm just wandering around because I see an unsettling parallel between life for most people and general programming. That is, as you grow up and go through school, your knowledge of things increases quite a lot. Once you get to your job and work, it seems to not quite flatline, but it slowly crawls up for most people it seems. "Learning" is no longer the focus as much as just "working" to solve whatever current problem there is.

Programming seems that same way in that, it would seem we are all mostly just end users of it and are not trying to make it better or learn more on it for the sake of improving it. Sure, we can use it more efficiently now, but then what? You have people that are really good binded to a language so as that language gets old and phases out, that's a knowledge pool that is decimated. And then the cycle repeats.

I dunno, I wonder if we are really even moving "forwards" in the programming field. I mean the whole C# XNA thing is heading in the direction of trying to make programming an everyday thing for anybody, and that's definitely not a good thing. Just look at their logo image.

For the masses

Wow, that makes me cringe. It's not that I'm worried about job uncertainty or being replaced so to speak, but I don't think we are moving in the forward direction anymore. But who am I to even speculate on what's best [rolleyes] Back to work I go.
September 28, 2008 03:05 AM
snk_kid
Quote:Original post by Drew_Benton
Quote:Original post by snk_kid
This seems a lot like a component-based game object system (or trying to be).

Quote:Original post by Telastyn
Indeed, and a... overly flexible component based system at that. Personally, I think going somewhere with mix-in inheritance and more meta-programming sort of approach would be better. But that reminds me that I need to make a good COP example for my hobby language.


I wasn't quite thinking about component based designs when I was working on this, but I see the similarities now. See I really wanted to integrate in a scripting language rather than have to make "programs" that would interact the objects, but Lua was driving me mad. Might invest in learning that more solidly or a diff language before I try to jump in with it (yet another thing on the todo list)

That reminds me, I really should probably start reading this Design Patterns book because I am pretty weak on that knowledge overall. Looking back at some of my old ideas (oh jeeze I cringe at some of my posts to this day), such as

On Static Class/Global Variables
Void* Messaging for Classes
Self Managing Objects
Self Tracking Object Design

I've always been a fan and intrigued by such designs and I persist in trying to find something "fun" to do with it, but lately I understand why most of those will fail as I have them: concurrency. My new cEntity class is thread safe, from the ctor, copy ctor, assignment operator, and all member functions. However, there is a problem if I try making them track themselves, I must have a way to initialize a critical section before the 1st class instance is created and destructed after all class instances have been destructed. Some sort of manager or simple factory could encapsulate that, but then the objects themselves don't need to know of each other explicitly.

I'm too much of a "it needs to be right" person to settle with an implementation that I knowingly see as misimplemented, so I'm thinking it's just not possible in a sense of thread safety. Oh well, I tried [grin]

Anyways, I'm just wandering around because I see an unsettling parallel between life for most people and general programming. That is, as you grow up and go through school, your knowledge of things increases quite a lot. Once you get to your job and work, it seems to not quite flatline, but it slowly crawls up for most people it seems. "Learning" is no longer the focus as much as just "working" to solve whatever current problem there is.

Programming seems that same way in that, it would seem we are all mostly just end users of it and are not trying to make it better or learn more on it for the sake of improving it. Sure, we can use it more efficiently now, but then what? You have people that are really good binded to a language so as that language gets old and phases out, that's a knowledge pool that is decimated. And then the cycle repeats.

I dunno, I wonder if we are really even moving "forwards" in the programming field. I mean the whole C# XNA thing is heading in the direction of trying to make programming an everyday thing for anybody, and that's definitely not a good thing. Just look at their logo image.

For the masses

Wow, that makes me cringe. It's not that I'm worried about job uncertainty or being replaced so to speak, but I don't think we are moving in the forward direction anymore. But who am I to even speculate on what's best [rolleyes] Back to work I go.


I hope you didn't take my comment in the wrong way, I was just trying highlight similarities and possibly that you're trying to solve the same problem. Have you seen this article before? A Data-driven Game Object System, it also takes into account of incorporating a scripting language.

A friend of mine has over over-engineered a 2D game & game engine (on purpose lol), everything is totally and I mean totally data-driven, you define game elements in XML and the game objects are component based, they communicate using message passing architecture.

P.S: While the GOF design book is great it wont talk about things like component based object systems
September 28, 2008 06:01 AM
Drew_Benton
Quote:I hope you didn't take my comment in the wrong way, I was just trying highlight similarities and possibly that you're trying to solve the same problem. Have you seen this article before? A Data-driven Game Object System, it also takes into account of incorporating a scripting language.


Oh no, of course not [smile] It was about 3am in the morning when I wrote that so I don't even remember what I was writing. Thanks for the link, I've not read that one, so I'll take a look. I like reading up on data driven systems. Last one I remember was the one in the Game Programming Gems I book.

I don't have much "logic" behind my reasoning for exploring this stuff. It's more just a curiosity and trying to see if I can make something out of it. It's like we are taught and its emphasized to do things a certain way, yet I want to break away from that because my goal right now is not an end use product, I just want to see what else can be done in ways that aren't typically used.

Quote:A friend of mine has over over-engineered a 2D game & game engine (on purpose lol), everything is totally and I mean totally data-driven, you define game elements in XML and the game objects are component based, they communicate using message passing architecture.


How did that work out overall? I couldn't honestly imagine doing that, but something like that sounds like it is sorta doable with XAML in terms of your programs GUI. You get physical separation of the GUI code and logic, which is quite interesting. I've not actually read up on the whole concept myself, so this is 2nd hand information.

It would be quite something though, to make a simple driver program that simply loads up data files and churns out different things. I kind of like the idea, even though you start to run into some issues. For me, having been on C++ for so long now, I only know how to do things the C/C++ way in terms of pointers, references, and just general code architecture. When moving to a different platform, when I want to do something, I still have the C mindset, so I can't quite understand the other ways to write something.

That's why I need to start committing to some higher level languages that are different in nature to C++ just so I can program with different perspectives, because right now I feel like I'm hampering myself with C/C++ only.

Quote:P.S: While the GOF design book is great it wont talk about things like component based object systems


I caught on to that this morning sadly. It's still a great book though, I bought it because it was simply amazing to see code + explanations + reasoning all at once [lol]. I mean most of the time I've read books that cover things but not to the depth or detail this one has. Simple amazing, was worth every penny.

Thanks for the reply!
September 28, 2008 12:27 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement