Return Values to script methods

edited 2:59AM in FastScript
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);

}

Comments

  • edited 2:59AM
    Two errors:

    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")
  • edited 2:59AM
    Thanks for the reply Alex. I think maybe I led you a little astray. First off the func name = ="func" was just a mis-type by me. I know everything is set to uppercase. As far as the AddMethod...I tried your suggestion and I still get the same thing. But where I led you astray is I said I got an error. I don't get a compile error. The code compiles and builds fine. But, when I run it I get an Exception when I call fsScript1->Compile(); I???ve tried it with both C++ and Pascal script and I get the same thing.
    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.
  • edited 2:59AM
    I've found out the problem. If you use 'procedure' in AddMethod you can't return an int. If you use 'function' any type works. Is this a pascal thing with function vs. procedure?

    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
  • edited 2:59AM
    Yes, pascal procedures can't return a value, you should use 'function' word.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.