Changeset - r27351:5b8cef9d2152
[Not reviewed]
master
0 2 0
Rubidium - 13 months ago 2023-05-04 20:57:11
rubidium@openttd.org
Add: method to call script functions with std::string
2 files changed with 10 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/script/squirrel.cpp
Show inline comments
 
@@ -439,12 +439,21 @@ bool Squirrel::CallStringMethodStrdup(HS
 
	if (ret._type != OT_STRING) return false;
 
	*res = stredup(ObjectToString(&ret));
 
	StrMakeValidInPlace(const_cast<char *>(*res));
 
	return true;
 
}
 

	
 
bool Squirrel::CallStringMethod(HSQOBJECT instance, const char *method_name, std::string *res, int suspend)
 
{
 
	HSQOBJECT ret;
 
	if (!this->CallMethod(instance, method_name, &ret, suspend)) return false;
 
	if (ret._type != OT_STRING) return false;
 
	*res = StrMakeValid(ObjectToString(&ret));
 
	return true;
 
}
 

	
 
bool Squirrel::CallIntegerMethod(HSQOBJECT instance, const char *method_name, int *res, int suspend)
 
{
 
	HSQOBJECT ret;
 
	if (!this->CallMethod(instance, method_name, &ret, suspend)) return false;
 
	if (ret._type != OT_INTEGER) return false;
 
	*res = ObjectToInteger(&ret);
src/script/squirrel.hpp
Show inline comments
 
@@ -157,12 +157,13 @@ public:
 
	 * Call a method of an instance, in various flavors.
 
	 * @return False if the script crashed or returned a wrong type.
 
	 */
 
	bool CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend);
 
	bool CallMethod(HSQOBJECT instance, const char *method_name, int suspend) { return this->CallMethod(instance, method_name, nullptr, suspend); }
 
	bool CallStringMethodStrdup(HSQOBJECT instance, const char *method_name, const char **res, int suspend);
 
	bool CallStringMethod(HSQOBJECT instance, const char *method_name, const std::string *res, int suspend);
 
	bool CallIntegerMethod(HSQOBJECT instance, const char *method_name, int *res, int suspend);
 
	bool CallBoolMethod(HSQOBJECT instance, const char *method_name, bool *res, int suspend);
 

	
 
	/**
 
	 * Check if a method exists in an instance.
 
	 */
0 comments (0 inline, 0 general)