Advertisement

Making modules like script objects

Started by April 30, 2008 03:35 AM
0 comments, last by WitchLord 16 years, 7 months ago
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]
I have plans to implement a method like "CloneModule".

What this will do is to create an exact copy of an existing module. The functions will share the bytecode, to avoid unnecessary memory usage, but each module will have its own set of global variables.

This ought to make it possible to use unique script modules for each game entity, without the extra memory overhead that currently happens.


I won't allow statements in the body of a class declaration, as that should be done in the constructor or an init function. I might allow default initialization of properties, but only if the value can be written as a constant expression (just as global variables are initialized).

I also won't allow statements in the global scope of a module, since the global scope is unordered (the declarations can be made in any order). You need to do this with an init function that you call after compiling the module.

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

This topic is closed to new replies.

Advertisement