Return Values to script methods
I know this should be a simple thing to do but I have yet to figure out how to do it. Please don't rip me for being stupid, I've complained before about the lack of examples/documentation for C++ Builder. I can't find this info anywhere. How do you set the return value to a script user function? Every time I try I get the "Incompatable types Integer, char *..." error. Why does your handler have to return a char *? I've tried using a Varient type for my return value got the but same result.
Here is my code:
...
...
fsScript1->AddMethod("Integer Func(x:Integer)",ScriptFunction,NULL,NULL);
...
...
...
Variant __fastcall TForm1::ScriptFunction(TObject *Object, TMetaClass *MetaClass, AnsiString FuncName, Variant &Params)
{
if(FuncName=="func")
{
return Params.GetElement(0)+1;
}
...
...
}
In my script I have this:
{
int i=0;
i=func(1);
}
Here is my code:
...
...
fsScript1->AddMethod("Integer Func(x:Integer)",ScriptFunction,NULL,NULL);
...
...
...
Variant __fastcall TForm1::ScriptFunction(TObject *Object, TMetaClass *MetaClass, AnsiString FuncName, Variant &Params)
{
if(FuncName=="func")
{
return Params.GetElement(0)+1;
}
...
...
}
In my script I have this:
{
int i=0;
i=func(1);
}
Comments
fsScript1->AddMethod("Integer Func(x:Integer)",ScriptFunction,NULL,NULL);
should be
fsScript1->AddMethod("procedure Func(x:Integer): Integer",ScriptFunction,NULL,NULL);
if(FuncName=="func")
should be
if(FuncName=="FUNC")
Anyway...After changing the AddMethod as per your suggestion but setting the return type to Variant and using ???Variant??? instead if ???int??? in my script, everything works OK.
//in source code
...
...
fsScript1->AddMethod("procedure func(x:Integer):Variant",
ScriptFunction,
NULL,
NULL);
...
...
//in script
//this works
{
Variant x=0;
x=func(5);
ShowMessage(x);
}
//this throws an exception when Compile() is called
{
int x=0;
x=func(5);
ShowMessage(x);
}
My concern is that most of my users are entry level techs that aren't really programmers and probably only know some very basic C code if lucky. They might be thrown off by having to use a Variant type instead of just a good ol??? int. (And more importantly, I don't have the time to teach them!!!) There are a handful of us that will be able to fully utilize all the great functionality in this tool and can't wait to do so. But, the sad reality is, I have to get the 'scaled down' version working first.
If using a Variant for user defined methods is the way it has to be then I guess I'll byte the bullet and make it work. But, if there is still something else I???m missing and I should be able to use int, please let me know. Thanks again.
Anyway...
fsScript1->AddMethod("function Func(x:Integer): Integer", OnUserFunction, NULL, NULL);
works good.
fsScript1->AddMethod("procedure Func(x:Integer): Integer", OnUserFunction, NULL, NULL);
throws an exception when fsScript1->Compile() is called