Advertisement

component object model using angelScriptA

Started by February 16, 2013 03:26 PM
1 comment, last by SiddharthBhat 11 years, 9 months ago

Hi all!

I've been using angelscript for around two to three moths now, and I'm having a blast.

I've been using it to develop a component based architecture for my game (rather than the usual inheritance thingie I usually do).

So, I have a component called as a "scriptComponent" that acts as a wrapper around components wriiten in angelScript. Plus, each object type has a "creator" script function that knows how to create the type that I need.

I'm having this weird architecture where I call a script function to initialize an actor of a certain type, This function uses the scriptComponent class (which is in C++) to wrap other script objects as components for the required actor. It then initializes the actor and attaches said scriptComponents to the actor. It then returns the fully created actor back to the code.

So, for example, if I wish to create an actor named "Enemy", I'd call a script function called "enemyCreator" which goes like this:


//This function in turn, creates a generic actor object and attaches script classes by using the scriptComponent like so:

Actor@ enemyCreator(vector2 pos, float angle){
    Actor @actor = Actor(pos, angle);

 scriptComponent@ sprite = scriptComponent("Sprite", "gameEnvt");
 scriptComponent @body = scriptComponent("Body", "gameEnvt");

actor.attach(sprite);
actor.attach(body);
return actor;
}

 



 

The scriptComponent searches for the required script class using the angelScript engine, and then instantiates it like this:


scriptComponent::scriptComponent(std::string scriptObjectName, std::string envtName){

scriptEngine *engine = scriptEngine::getInstance();
    AS_ScriptEnvt envt(engine->getEnvironment(envtName.c_str()));

    //the script object initializer is a simple struct that holds data need to initialize the script object
    AS_ScriptObjInitalizer *init = NULL;
    this->_getInitializer(scriptObjectName);
    
    this->scriptObject = engine->createScriptObj(*init);



   //script "runs" are a wrapper around asScriptContext. 
    engine->allocateScriptRun(&this->run); 
    //it asks the script object for the initialization function. the script object sets up the script run
    //with the correct function as well as the correct script object.
    this->scriptObject->getRunByDecl("void Init(componentActor @actor, string envtName)", *run);

    //it then passes the arguments and executes the function
    run->pushArgument(this->actor);
    run->pushArgument(envtName);
    run->Execute();
    run->Reuse();

    //it then takes all the asIScriptFunction* it wants by asking for the functions. 
    //which are then called during the required time by using the script run
    this->onUpdateFunc = this->scriptObject->getFunctionByName("Update");
    this->onDrawFunc = this->scriptObject->getFunctionByName("Draw");
    this->onHandleMessageFunc = this->scriptObject->getFunctionByName("handleMessage");
    this->onBeginColisionFunc = this->scriptObject->getFunctionByName("handleBeginCollision");
    this->onEndCollisionFunc =     this->scriptObject->getFunctionByName("handleEndCollision");
    this->onPreSolveFunc = this->scriptObject->getFunctionByName("handlePreSolveCollision");
    this->onPostSolveFunc = this->scriptObject->getFunctionByName("handlePostSolveCollision");
 

Does anyone see any problems with this architecture? I'm feeling that I'm missing something here that will have devastating consequences once I begin to create a lot of components. I've never been good at figuring out how this sort of this plays out when complexity increases

Thanks all!

PS- the names of the classes have been changed around a little, and the initialization function of the enemy and the constructor of the scriptComponent are watered down, so if you see some dangling refrence to other parts of the system that I missed to remove, or some syntax error, ignore it.

a WIP 2d game engine: https://code.google.com/p/modulusengine/

English is not my first language, so do feel free to correct me :)

I'd say you've come with a good way to use the scripting engine. I don't see any problems with your architecture.

This actually sounds very similar to what I show in the game sample that comes with the SDK. I'm not sure if you had a chance to look at that before you came up with your own implementation. Obviously the game sample is very simplified but the way the script and game engine are tied together is pretty much identical to the way you've done it.

Regards,

Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement

Ah, nice to know that there's no problem :)

And yes, I did go through the examples but I found them to be.. confusing. So I just stumbled along :)

Thanks Andreas! (for both the help and the awesome scripting language)

a WIP 2d game engine: https://code.google.com/p/modulusengine/

English is not my first language, so do feel free to correct me :)

This topic is closed to new replies.

Advertisement