Multiple script files

djigneodjigneo Posts: 283

Is it possible to call functions defined in other script files?

For example:
helperScript.dsa


function sayHello(){
print("hello world!");
}

testScript.dsa

import "helperScript.dsa"

sayHello();

I have attempted to use an import command with no success. It seems to be an unrecognized command, but the error message is fairly cryptic:

SyntaxError: Parse error
Stack Trace:
 ()@:1

(I indeed know what a syntax error is, but it's unclear if the command is unrecognized or if i'm consuming a supported command with invalid syntax. A column number would help immensely with this sort of thing.)

I have pored through the examples, and I'm not seeing anything similar. I've also perused Qt documentation (which is what put me on the idea of the import command).
I appreciate the insight.

Post edited by djigneo on

Comments

  • Richard HaseltineRichard Haseltine Posts: 100,938
    edited December 1969

    There is a way to include additional scripts, but it isn't documented and I've a feeling that the forum discussion was on the old, no longer available, forum. What syntax are you currently using that DS is failing to parse?

  • millighostmillighost Posts: 261
    edited December 1969

    djigneo said:
    Is it possible to call functions defined in other script files?
    ....
    I have pored through the examples, and I'm not seeing anything similar. I've also perused Qt documentation (which is what put me on the idea of the import command).
    I appreciate the insight.

    The function to use is include:
    
    include ("helperScript.dsa");
    

    It does not automatically search all paths, so i often use the following idiom:
    
    var dir_self = new DzDir (new DzFileInfo (getScriptFileName ()).path ());
    include (dir_self.filePath ("helperScript.dsa"));
    
  • djigneodjigneo Posts: 283
    edited July 2014

    Thanks for pointing me towards include.

    I have gotten a "Hello world" working with a hard-coded absolute path thanks to your example.

    Is there a way to check if the include found something? Until the path was correct, I was getting a
    ReferenceError: Can't find variable: sayHello

    ("sayHello" is the function name in the other script).
    It would be nice to be able to spit out a useful error message about not finding the other script.

    I haven't quite gotten the relative paths working yet. It appears it's looking in the program files directory, rather than my library directory. I haven't played nearly enough with the file path logic in dazScript, so I probably just don't understand what your example code is doing yet.
    [Edit] I think i'm only confused about the usage of getScriptFileName(). it returns an empty string for me.

    Post edited by djigneo on
  • rbtwhizrbtwhiz Posts: 2,250
    edited December 1969

    The error is provided by the interpreter... DAZ Studio is displaying/logging what the interpreter handed it. The error is telling you that the interpreter encountered an anonymous function on line 1, that it cannot parse. It's telling you this because import is a reserved identifier, but is not implemented [in the version of JavaScriptCore used for the interpreter in the version of Qt used by the version of DAZ Studio]. It is for this reason that we added Global::include().

    Global::getScriptFileName(), when run from the Script IDE, will yield an empty string.

    -Rob

  • djigneodjigneo Posts: 283

    For the benefit of anyone else playing with this, the secret is that the Script file needs to be saved before execution. If you fail to save, the import command seems to look in a temporary directory and will not (in most cases) find the script to be imported.

  • As an alternative, there's also the Sub Script sample ("a script that remotely executes another script, passing in predefined arguments to control the operation of the target script.") in the DAZ Studio 4.x API Reference - I used this method a while back and it works fine.

    But millighost's 'include' seems much simpler... (I wonder, what is the practical difference between the 'include' and 'Sub Script' approach?)

  • P.S. I used the following code to search through all the mapped DAZ Studio content directories for the script file I want, and then use 'o_Script.loadFromFile(sFile,true)' as in the Sub Script sample:

        sFile="";
        i=0;
        n=App.getContentMgr().getNumContentDirectories()
        while (i<n && sFile=="")
        {
            s=App.getContentMgr().getContentDirectoryPath(i++)+"/scripts/3DCheapskate/SmartPlus/GenericPropGotoPlus.dsa";
            oNewFile = new DzFile(s);
            if (oNewFile.exists())
                sFile=s;
        }

     

  • djigneodjigneo Posts: 283
    edited September 2015

    3dcheapskate said:

    I wonder, what is the practical difference between the 'include' and 'Sub Script' approach?

    Presumably, the main difference is that Include() allows you to call functions, where as DzScript.loadFromFile() will invoke the "main" entry point of the script.

    Post edited by djigneo on
  • rbtwhizrbtwhiz Posts: 2,250

    I wonder, what is the practical difference between the 'include' and 'Sub Script' approach?

    In a word... scope.

    Global::include() loads the contents of another script into the context (DzScriptContext) of the calling script - variables/functions/objects that are declared/defined within the included script are directly accessible within the encompassing script as if it was all in the same script to begin with, somewhat like a C/C++ preprocessor directive.

    DzScript::loadFromFile() loads the contents of a script into its own context, as an object with its own methods - the caller and callee do not share scope, rather information is passed from caller to callee through an array of arguments to DzScript::exec() or DzScript::call(), somewhat like command line utilities.

    -Rob

  • Widdershins StudioWiddershins Studio Posts: 539
    edited November 2015

    Can't get this to work either. I have two files...

     

    speak.dsa :

    include ("hello.dsa");sayHello();

     

    hello.dsa :

    function sayHello(){	MessageBox.information("hello world!", "DEBUG", "TRUE");}

     

    Both scripts are saved in the same folder.

    It just gives and error opening file.

    Any thoughts ?

    Post edited by Widdershins Studio on
  • Sorry should add the error message is this :

    Executing Script...

    Script Error: Line 4

    ReferenceError: Can't find variable: sayHello

    Stack Trace: ()@C:/Users/rogad/Documents/DAZ 3D My Creations/W_Groceries/Props/Widdershins/Groceries/speak.dsa:4

    Error executing script on line: 4

    Script executed in 0 secs 2 msecs.

  • V3DigitimesV3Digitimes Posts: 3,150

    Would'nt it be linked to the caller script, callee script thing that Rob mentionned here ?

    http://docs.daz3d.com/doku.php/public/software/dazstudio/4/referenceguide/scripting/api_reference/samples/remote_operation/sub_script/start

    Please note that I'm not sure AT ALL what I'm saying, but if I tried to do the same, I would have a look at this first to see if it corresponds to my case of figure (well if I can do something with that).

  • Yeah I can't get that example to work.

    Even when I use the absolute path it gives me file not found.

  • Ahh found it... this little bit of information I had missed...

    getScriptFile String filenameWithoutExtension )

    was putting the extension on the end. surprise

  • What I find a little confusing is that there is this :

    include String scriptPath )

     But I can't figure out how to use it.

    It seems a simpler solution, but how to use it...

  • Did you try using a full path in your include? It's not a feature I've used to say if that would help or not, just a suggestion.

  • No not yet, I found I could strip down that code in the previous example.

    I'm actually going another way at the moment, but I will come back to it.

Sign In or Register to comment.