(svn r9167) [NoAI] -Fix: store the 'this' pointer in a nicer way (KUDr) noai
authortruelight
Wed, 14 Mar 2007 10:39:46 +0000
branchnoai
changeset 9378 995e55ab701d
parent 9377 3f1327677bcd
child 9379 6e89b780c2c9
(svn r9167) [NoAI] -Fix: store the 'this' pointer in a nicer way (KUDr)
-Fix: don't allow multiple instances of AIController inside one script
src/ai/squirrel/squirrel.cpp
--- a/src/ai/squirrel/squirrel.cpp	Wed Mar 14 10:30:34 2007 +0000
+++ b/src/ai/squirrel/squirrel.cpp	Wed Mar 14 10:39:46 2007 +0000
@@ -66,18 +66,20 @@
 	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, (SQUserPointer *)&self);
-
-	/* Now store it in this instance */
+	/* Find the 'this' pointer and store it inside the class */
+	self = (Squirrel *)sq_getforeignptr(vm);
+	if (self == NULL) {
+		DEBUG(ai, 0, "[squirrel] Please only make one instance of AIController!");
+		return SQ_ERROR;
+	}
 	sq_setinstanceup(vm, 1, self);
 	sq_setreleasehook(vm, 1, &Squirrel::SQDestructor);
 	/* Keep track of the internal Squirrel instance */
 	self->SQ_instance = SQ_instance;
 
+	/* Make sure we only register one AI in this script */
+	sq_setforeignptr(vm, NULL);
+
 	return 0;
 }
 
@@ -109,11 +111,8 @@
 {
 	SQInteger top = sq_gettop(this->vm);
 
-	/* Store 'this' in _main_ptr so we can find it back later on */
-	sq_pushroottable(this->vm);
-	sq_pushstring(this->vm, "_main_ptr", -1);
-	sq_pushuserpointer(this->vm, this);
-	sq_createslot(this->vm, -3);
+	/* Store 'this' so we can find it back later on */
+	sq_setforeignptr(this->vm, this);
 
 	/* Create AIController class (the 'main' of it all) */
 	sq_pushroottable(this->vm);