• FEATURED

View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

component object model using angelScriptA

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

#1bollµ  Members

Posted 16 February 2013 - 09:26 AM

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

#2Andreas Jonsson  Moderators

Posted 16 February 2013 - 03:24 PM

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

#3bollµ  Members

Posted 16 February 2013 - 10:06 PM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.