Playing around with a component based design pattern in 3dgs. An entity can have multiple components(scripts are also components). This is an example of a script component attached to an entity:
Php Code:
CPTransform transformation; //cache the transformation 
//@TODO caching only works for one instance at the moment. need to add the possibility to store variables for every instance

void PlayerStart()
{
	transformation = CPGetComponent("transform"); //CPGetComponent is always refering to components of the current entity(the one with the player script in this case)
	printf("player initialized");
}

void PlayerUpdate()
{
	transformation->rotation->pan+=1*time_step;
	CPTransformTranslate(transformation, vector(1*time_step, 0, 0));
} 




At the moment you have to attach those components per script:
Php Code:
#define PRAGMA_PATH "Components"
#include "CPManager.h"
#include "CPTransform.h"
#include "CPScript.h"

#define PRAGMA_PATH "Scripts"
#include "Player.c"

int main()
{
	level_load(NULL);
	CPcurrentEntity = ent_create(CUBE_MDL, vector(10, 15, 20), NULL);
	CPSetComponent("transform",  CPTransformCreate());
	CPSetComponent("player",  CPScriptCreate("Player"));
	return 0;
} 



But this could also be done with an interface like in Unity,

That's all you need to make it work.

Would you use it? If yes I will continue working on this and publish it(probably with a small editor to attach components)

Last edited by krial057; 09/27/12 21:45.