In my game all the npcs are wrapped within a class. For example, heres the code i use:
NpcClass* BuildNpc(const string& code, const string& className, const string& spriteName, int width, int height, const TileBlock& tileBlock, asIScriptEngine* engine)
{
string classCode = string("class ") + className + "{\r\n";
classCode += "float x;\r\n";
classCode += "float y;\r\n";
classCode += "float depth;\r\n";
classCode += code;
classCode += "}";
if(engine->AddScriptSection("npcs", className.c_str(), classCode.c_str(), classCode.length()) >= 0)
{
if(engine->Build("npcs") >= 0)
return new NpcClass(engine->GetTypeIdByDecl("npcs", className.c_str()), className, spriteName, width, height, tileBlock, engine);
}
return NULL;
}
NpcClass::NpcClass(int typeId, const string& className, const string& spriteName, int width, int height, const TileBlock& tileBlock, asIScriptEngine* engine)
{
this->typeId = typeId;
this->className = className;
this->spriteName = spriteName;
this->width = width;
this->height = height;
this->tileBlock = tileBlock;
methodIds[EVENT_CREATE] = engine->GetMethodIDByName(typeId, "OnCreate");
methodIds[EVENT_UPDATE] = engine->GetMethodIDByName(typeId, "OnUpdate");
methodIds[EVENT_DRAW] = engine->GetMethodIDByName(typeId, "OnDraw");
methodIds[EVENT_BEGINDRAW] = engine->GetMethodIDByName(typeId, "OnBeginDraw");
methodIds[EVENT_ENDDRAW] = engine->GetMethodIDByName(typeId, "OnEndDraw");
methodIds[EVENT_CARRYDRAW] = engine->GetMethodIDByName(typeId, "OnCarryDraw");
methodIds[EVENT_EXPLODED] = engine->GetMethodIDByName(typeId, "OnExploded");
methodIds[EVENT_CARRY] = engine->GetMethodIDByName(typeId, "OnCarry");
methodIds[EVENT_THROWN] = engine->GetMethodIDByName(typeId, "OnThrown");
methodIds[EVENT_SWORDCOLLISION] = engine->GetMethodIDByName(typeId, "OnSwordCollision");
methodIds[EVENT_ARROWCOLLISION] = engine->GetMethodIDByName(typeId, "OnArrowCollision");
methodIds[EVENT_WARPCOLLISION] = engine->GetMethodIDByName(typeId, "OnWarpCollision");
methodIds[EVENT_PLAYERCOLLISION] = engine->GetMethodIDByName(typeId, "OnPlayerCollision");
methodIds[EVENT_PLAYERCHATS] = engine->GetMethodIDByName(typeId, "OnPlayerChats");
methodIds[EVENT_PLAYERENTERS] = engine->GetMethodIDByName(typeId, "OnPlayerEnters");
methodIds[EVENT_PLAYERLEAVES] = engine->GetMethodIDByName(typeId, "OnPlayerLeaves");
methodIds[EVENT_PLAYERDIES] = engine->GetMethodIDByName(typeId, "OnPlayerDies");
methodIds[EVENT_PLAYERSPAWNS] = engine->GetMethodIDByName(typeId, "OnPlayerSpawns");
methodIds[EVENT_PLAYERHURT] = engine->GetMethodIDByName(typeId, "OnPlayerHurt");
methodIds[EVENT_TIMER] = engine->GetMethodIDByName(typeId, "OnTimer");
}
Im finding classes limited because you cannot create classes within classes and you cannot call functions or initialize class properties outside of another function like this:
class SomeNpc
{
float x = 32; //cannot do
float y = 32; //cannot do
float depth; //can do
DoSomething(); //Cannot do (not sure if you can even do this in modules)
void OnCreate()
{
x = 32; //can do
y = 32; //can do
DoSomething(); //can do
}
}
So maybe in the future you could make it so you can make "instances" of modules using engine->CreateScriptObject() or something. That way i dont have to wrap npcs inside a class and the npcs can do more things.
EDIT: Actually i think its "Making script sections like script objects"
[Edited by - 39ster on April 30, 2008 3:51:54 AM]