# HG changeset patch # User truelight # Date 1173864622 0 # Node ID 8bbd77b18de41d7e2042cb9ac0d65bff205a6525 # Parent 61379e9b2393132d43ac54ce4a690eb561820bf0 (svn r9163) [NoAI] -Codechange: put AddMethod in his own function, so calling often is easier diff -r 61379e9b2393 -r 8bbd77b18de4 src/ai/squirrel/squirrel.cpp --- a/src/ai/squirrel/squirrel.cpp Wed Mar 14 02:29:26 2007 +0000 +++ b/src/ai/squirrel/squirrel.cpp Wed Mar 14 09:30:22 2007 +0000 @@ -82,6 +82,15 @@ return 1; } +inline void Squirrel::SQAddMethod(const char *name, SQFUNCTION proc, uint nparam, const char *params) +{ + sq_pushstring(this->vm, name, -1); + sq_newclosure(this->vm, proc, 0); + sq_setparamscheck(this->vm, nparam, params); + sq_setnativeclosurename(this->vm, -1, name); + sq_createslot(this->vm, -3); +} + void Squirrel::RegisterClasses() { SQInteger top = sq_gettop(this->vm); @@ -98,18 +107,9 @@ sq_newclass(this->vm, SQFalse); /* Create a constructor, so we can assign the right 'this' */ - sq_pushstring(this->vm, "constructor", -1); - sq_newclosure(this->vm, &Squirrel::SQConstructor, 0); - sq_setparamscheck(this->vm, 1, "x"); - sq_setnativeclosurename(this->vm, -1, "constructor"); - sq_createslot(this->vm, -3); - + this->SQAddMethod("constructor", &Squirrel::SQConstructor, 1, "x"); /* Assign other functions as AIController won't be done via the normal way */ - sq_pushstring(this->vm, "GetTick", -1); - sq_newclosure(this->vm, &Squirrel::SQGetTick, 0); - sq_setparamscheck(this->vm, 1, "x"); - sq_setnativeclosurename(this->vm, -1, "GetTick"); - sq_createslot(this->vm, -3); + this->SQAddMethod("GetTick", &Squirrel::SQGetTick, 1, "x"); /* Finally really create the class */ sq_createslot(this->vm, -3); diff -r 61379e9b2393 -r 8bbd77b18de4 src/ai/squirrel/squirrel.hpp --- a/src/ai/squirrel/squirrel.hpp Wed Mar 14 02:29:26 2007 +0000 +++ b/src/ai/squirrel/squirrel.hpp Wed Mar 14 09:30:22 2007 +0000 @@ -14,6 +14,11 @@ HSQUIRRELVM vm; ///< The Virtual Machine for squirrel /** + * Add a single method to a class (in creation) in Squirrel. + */ + void SQAddMethod(const char *name, SQFUNCTION proc, uint nparam, const char *params); + + /** * Registers all our classes, so it can be used from Squirrel. */ void RegisterClasses();