(svn r9163) [NoAI] -Codechange: put AddMethod in his own function, so calling often is easier noai
authortruelight
Wed, 14 Mar 2007 09:30:22 +0000
branchnoai
changeset 9375 8bbd77b18de4
parent 9374 61379e9b2393
child 9376 7c12a15c945a
(svn r9163) [NoAI] -Codechange: put AddMethod in his own function, so calling often is easier
src/ai/squirrel/squirrel.cpp
src/ai/squirrel/squirrel.hpp
--- 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);
--- 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();