Im using angel script in my game and it allows people to create their own npcs within the game. To do this each NPC they create gets it own class and whenever they add an NPC into the level it uses CreateScriptObject to create an instance of the NPC class. I've got it to compile and create instances of NPCs but when i try to call a method (the npcs are event orientated) it wont work (but it still return >= 0).
Heres the code for the Npc class:
CNpcClass* loadNpc(const CString& fName, asIScriptEngine* engine)
{
CString className = removeFileExt(fName);
CByteStream codeStream;
if(codeStream.loadFile(fName.text()))
return buildNpc(className, codeStream.text(), engine);
return NULL;
}
CNpcClass* buildNpc(const CString& className, const CString& code, asIScriptEngine* engine)
{
CString scriptCode;
scriptCode << "class " << className << "\n";
scriptCode << "{\n";
scriptCode << "float x;\n";
scriptCode << "float y;\n";
scriptCode << code << "\n";
scriptCode << "}\n";
printf("Code: %s\n", scriptCode.text());
if(engine->AddScriptSection("npcs", className.text(), scriptCode.text(), scriptCode.length()) >= 0)
{
if(engine->Build("npcs") >= 0)
return new CNpcClass(className, engine->GetTypeIdByDecl("npcs", className.text()), engine);
}
return NULL;
}
CNpcClass::CNpcClass(const CString& className, int typeId, asIScriptEngine* engine)
{
name = className;
this->typeId = typeId;
methodIds[evOnCreate] = engine->GetMethodIDByName(typeId, "onCreate");
methodIds[evOnDraw] = engine->GetMethodIDByName(typeId, "onDraw");
methodIds[evOnExploded] = engine->GetMethodIDByName(typeId, "onExploded");
methodIds[evOnSwordCollision] = engine->GetMethodIDByName(typeId, "onSwordCollision");
methodIds[evOnArrowCollision] = engine->GetMethodIDByName(typeId, "onArrowCollision");
methodIds[evOnWarpCollision] = engine->GetMethodIDByName(typeId, "onWarpCollision");
methodIds[evOnPlayerCollision] = engine->GetMethodIDByName(typeId, "onPlayerCollision");
methodIds[evOnPlayerChats] = engine->GetMethodIDByName(typeId, "onPlayerChats");
methodIds[evOnPlayerEnters] = engine->GetMethodIDByName(typeId, "onPlayerEnters");
methodIds[evOnPlayerLeaves] = engine->GetMethodIDByName(typeId, "onPlayerLeaves");
methodIds[evOnPlayerDies] = engine->GetMethodIDByName(typeId, "onPlayerDies");
methodIds[evOnPlayerSpawns] = engine->GetMethodIDByName(typeId, "onPlayerSpawns");
methodIds[evOnPlayerHurt] = engine->GetMethodIDByName(typeId, "onPlayerHurt");
methodIds[evOnTimer] = engine->GetMethodIDByName(typeId, "onTimer");
for(int i = 0; i < eventCount; ++i)
printf("Event: %i:%i\n", i, methodIds);
}
int CNpcClass::getMethodId(unsigned int eventId) const {
if(eventId < eventCount)
return methodIds[eventId];
else return -1;
}
int CNpcClass::getTypeId() const {
return typeId;
}
And heres the code for the npcs:
CNpc::CNpc(float pX, float pY, CNpcClass* npcClass, asIScriptEngine* engine)
{
context = engine->CreateContext();
this->npcClass = npcClass;
instance = (asIScriptStruct*)engine->CreateScriptObject(npcClass->getTypeId());
x = (float*)instance->GetPropertyPointer(0);
y = (float*)instance->GetPropertyPointer(1);
printf("X: %f\n"
"Y: %f\n", *x, *y);
*x = pX;
*y = pY;
}
bool CNpc::callEvent(unsigned int eventId)
{
int methodId = npcClass->getMethodId(eventId);
if(methodId >= 0)
{
context->SetObject((void*)instance);
if(context->Prepare(methodId) >= 0)
return context->Execute() >= 0;
}
return false;
}
Whenever i call "callEvent" it returns true but nothing happens.
Also heres the angel script code. The script is called bomb.txt:
bomb()
{
x = 64;
y = 96;
}
void onCreate()
{
print("test");
}
And the output:
Code: class bomb
{
float x;
float y;
bomb()
{
x = 64;
y = 96;
}
void onCreate()
{
print("test");
}
}
Event: 0:57
Event: 1:-6
Event: 2:-6
Event: 3:-6
Event: 4:-6
Event: 5:-6
Event: 6:-6
Event: 7:-6
Event: 8:-6
Event: 9:-6
Event: 10:-6
Event: 11:-6
Event: 12:-6
Event: 13:-6
X: 64.000000
Y: 96.000000
Also i know the method isnt being called and its not just a glitch in print because i tried an infinite loop and it didnt freeze.
npcs[0]->callEvent(evOnCreate)
Also the constructor for the NPC works. It prints the correct x, y values in CNpc::CNpc()