Hi there,
I wanted to pass a float by reference from c++ to a script function, I did not find a direct way to do that.
(I am using SQPlus).
so i wrote some code that works.
I am posting it here for feedback/improvement suggestions.
I couldnt find the tag needed to display the code nicely either.
using the bbcode 'code' tag didnt work
Header File
template<typename T>
class ScriptArgByRef {
public:
ScriptArgByRef(T* pRef = NULL) : mRef(pRef) {}
ScriptArgByRef(T& ref) : mRef(&ref) {}
public:
static void declareInVM(SquirrelVM& target) {
SqPlus::SQClassDef< ScriptArgByRef<T> >(getScriptArgByRefClassName<T>()).
func(&ScriptArgByRef::set, L"set").
func(&ScriptArgByRef::get, L"get");
}
inline void set(T val) { mRef.dref() = val; }
inline const T& get() { return mRef.dref(); }
protected:
SoftPtr<T> mRef;
};
template<typename T>
inline const TCHAR* getScriptArgByRefClassName();
template<>
inline const TCHAR* getScriptArgByRefClassName<float>() { return L"FloatByRef"; }
template<>
inline const TCHAR* getScriptArgByRefClassName<int>() { return L"IntByRef"; }
typedef ScriptArgByRef<float> ScriptArgByRef_float;
typedef ScriptArgByRef<int> ScriptArgByRef_int;
Cpp File
DECLARE_INSTANCE_TYPE_NAME(WOF::ScriptArgByRef<float>, FloatByRef);
DECLARE_INSTANCE_TYPE_NAME(WOF::ScriptArgByRef<int>, IntByRef);
Usage Example
c++
float passMeByRef = 0.0f;
SquirrelFunction<void> callFunc(_T("callMe"));
callFunc(ScriptArgByRef_float(passMeByRef ));
squirrel script
function callMe(val) {
local currVal = val.get();
val.set(1.0);
}