(svn r9166) [NoAI] -Add: added destructor code, so we can do things there in the future noai
authortruelight
Wed, 14 Mar 2007 10:30:34 +0000
branchnoai
changeset 9377 3f1327677bcd
parent 9376 7c12a15c945a
child 9378 995e55ab701d
(svn r9166) [NoAI] -Add: added destructor code, so we can do things there in the future
-Add: store the instance-pointer of the squirrel class
bin/ai/SQNoAI/main.sq
src/ai/squirrel/squirrel.cpp
src/ai/squirrel/squirrel.hpp
--- a/bin/ai/SQNoAI/main.sq	Wed Mar 14 09:53:24 2007 +0000
+++ b/bin/ai/SQNoAI/main.sq	Wed Mar 14 10:30:34 2007 +0000
@@ -11,6 +11,7 @@
 
 /* Initialize the AI in the global space */
 ai <- SQNoAI();
+print("I am " + ai);
 
 /* Loop the GameLoop to our own class */
 function GameLoop() { ai.GameLoop(); }
--- a/src/ai/squirrel/squirrel.cpp	Wed Mar 14 09:53:24 2007 +0000
+++ b/src/ai/squirrel/squirrel.cpp	Wed Mar 14 10:30:34 2007 +0000
@@ -50,19 +50,33 @@
 	printf("\n");
 }
 
+SQInteger Squirrel::SQDestructor(SQUserPointer p, SQInteger size)
+{
+	return 1;
+}
 
 SQInteger Squirrel::SQConstructor(HSQUIRRELVM vm)
 {
-	SQUserPointer self = NULL;
+	Squirrel *self = NULL;
+	HSQOBJECT object;
+	SQUserPointer SQ_instance;
+
+	/* Get the instance pointer */
+	sq_getclass(vm, 1);
+	sq_getstackobj(vm, 1, &object);
+	SQ_instance = object._unVal.pUserPointer;
 
 	/* We stored the real 'this' in _main_ptr */
 	sq_pushroottable(vm);
 	sq_pushstring(vm, "_main_ptr", -1);
 	sq_get(vm, -2);
-	sq_getuserpointer(vm, -1, &self);
+	sq_getuserpointer(vm, -1, (SQUserPointer *)&self);
 
 	/* Now store it in this instance */
 	sq_setinstanceup(vm, 1, self);
+	sq_setreleasehook(vm, 1, &Squirrel::SQDestructor);
+	/* Keep track of the internal Squirrel instance */
+	self->SQ_instance = SQ_instance;
 
 	return 0;
 }
@@ -102,7 +116,7 @@
 	sq_createslot(this->vm, -3);
 
 	/* Create AIController class (the 'main' of it all) */
-	sq_settop(this->vm, top);
+	sq_pushroottable(this->vm);
 	sq_pushstring(this->vm, "AIController", -1);
 	sq_newclass(this->vm, SQFalse);
 
--- a/src/ai/squirrel/squirrel.hpp	Wed Mar 14 09:53:24 2007 +0000
+++ b/src/ai/squirrel/squirrel.hpp	Wed Mar 14 10:30:34 2007 +0000
@@ -12,6 +12,7 @@
 class Squirrel: public AIController {
 private:
 	HSQUIRRELVM vm;  ///< The Virtual Machine for squirrel
+	SQUserPointer SQ_instance; ///< The internal instance of squirrel
 
 	/**
 	 * Add a single method to a class (in creation) in Squirrel.
@@ -25,11 +26,17 @@
 
 	/**
 	 * Constructor for Squirrel AIController. We assign the real 'this' value to
-	 *  the instance pointer, so we can easy look up this->
+	 *  the instance pointer, so we can easy look up this->.
 	 */
 	static SQInteger SQConstructor(HSQUIRRELVM vm);
 
 	/**
+	 * Destructor for Squirrel AIController. This is called if the class is
+	 *  destroyed inside the script.
+	 */
+	static SQInteger SQDestructor(SQUserPointer p, SQInteger size);
+
+	/**
 	 * A wrapper around the AIController class, as that can't be automated.
 	 */
 	static SQInteger SQGetTick(HSQUIRRELVM vm);