src/ai/squirrel/engine.hpp
branchnoai
changeset 9404 ef9e171617a3
parent 9399 84a3d84c19b2
--- a/src/ai/squirrel/engine.hpp	Thu Mar 15 13:28:11 2007 +0000
+++ b/src/ai/squirrel/engine.hpp	Thu Mar 15 13:36:45 2007 +0000
@@ -62,9 +62,17 @@
 	void AddClassEnd();
 
 	/**
-	 * Call a method of an instance.
+	 * Call a method of an instance, in various flavors.
 	 */
-	void CallMethod(HSQOBJECT instance, const char *method_name);
+	void CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret);
+	void CallMethod(HSQOBJECT instance, const char *method_name) { this->CallMethod(instance, method_name, NULL); }
+	const char *CallStringMethod(HSQOBJECT instance, const char *method_name) { HSQOBJECT ret; this->CallMethod(instance, method_name, &ret); return ObjectToString(&ret); }
+	int CallIntegerMethod(HSQOBJECT instance, const char *method_name) { HSQOBJECT ret; this->CallMethod(instance, method_name, &ret); return ObjectToInteger(&ret); }
+
+	/**
+	 * Check if a method exists in an instance.
+	 */
+	bool MethodExists(HSQOBJECT instance, const char *method_name);
 
 	/**
 	 * Creates a class instance.
@@ -80,12 +88,40 @@
 	 * @note This will only work just after a function-call from within Squirrel
 	 *  to your C++ function.
 	 */
-	static bool GetInstance(HSQUIRRELVM vm, SQUserPointer *ptr) { return SQ_SUCCEEDED(sq_getinstanceup(vm, 1, ptr, 0)); }
+	static bool GetRealInstance(HSQUIRRELVM vm, SQUserPointer *ptr) { return SQ_SUCCEEDED(sq_getinstanceup(vm, 1, ptr, 0)); }
 
-	HSQUIRRELVM GetVM() {
-#warning Please stop using this function ASAP
-		return this->vm;
-	}
+	/**
+	 * Get the Squirrel-instance pointer.
+	 * @note This will only work just after a function-call from within Squirrel
+	 *  to your C++ function.
+	 */
+	static bool GetInstance(HSQUIRRELVM vm, HSQOBJECT *ptr) { sq_getclass(vm, 1); sq_getstackobj(vm, 1, ptr); return true; }
+
+	/**
+	 * Convert a Squirrel-object to a string.
+	 */
+	static const char *ObjectToString(HSQOBJECT *ptr) { return sq_objtostring(ptr); }
+
+	/**
+	 * Convert a Squirrel-object to an integer.
+	 */
+	static int ObjectToInteger(HSQOBJECT *ptr) { return sq_objtointeger(ptr); }
+
+	/**
+	 * Sets a pointer in the VM that is reasable from where ever you are, what
+	 *  ever your state is. Useful to keep track of the main instance.
+	 */
+	void SetGlobalPointer(void *ptr) { sq_setforeignptr(this->vm, ptr); }
+
+	/**
+	 * Get the pointer as set by SetGlobalPointer.
+	 */
+	static void *GetGlobalPointer(HSQUIRRELVM vm) { return sq_getforeignptr(vm); }
+
+	/**
+	 * Throw a Squirrel error that will be nicely displayed to the user.
+	 */
+	void ThrowError(const char *error) { sq_throwerror(this->vm, error); }
 };
 
 #endif /* SQUIRREL_CORE_HPP */