Classes and Separating Script Files
nicstt
Posts: 11,715
Just wondering if it is possible to seperate a script into separate files?
I know a function expression is used to declare a constructor functions; can and if so, how, can these be separated between script files?
Would make files much easier to write and debug.
Comments
Yes, it is. This post shows a simplified example: How best to structure a large script project?
Be aware that (so far, in my experience), if a file contains a Constructor (as the above example MyTypeDefA.dsa and MyTypeDefB.dsa files do), then the "include" command must be placed outside (i.e. before) the main routine in the .dsa file that "includes" the file - otherwise the Constructors are "not found" within the main routine. If a .dsa file contains no Constructors (e.g. just a bunch of utility functions), then it can be "included" within the main routine.
Hope this helps.
P.
Some details of a more practical example: My current main project has:
Cheers appreciate the post.
I'm curious; can they be encrypted?
I don't know - I've never tried encrypting scripts.
But I'd be very surprised if they can't be encrypted.
P.
I seem to be having trouble; I even tested it on two very basic files.
The file to be include:
The file that includes the file
Stack trace "can't find the variable"; they are both in the same path/folder
Don't encrypted scripts use a different file type? I thought they were like "test.dse" or such?
The 'include' statement probably needs the encrypted file name, not the unencrypted.
Or are you doing this with no encryption? If that's the case, I'd probably use a relative directory path, like
Or you could use the content manager methods to locate the file and get its absolute path and try that.
Most likely this is a file location issue. See http://docs.daz3d.com/doku.php/public/software/dazstudio/4/referenceguide/scripting/api_reference/object_index/global - if you give a relative path, as you do above, it is going to be added to the path to your scripts folder.
As Richard said, the include() command defaults to use locations relative to the DS Scripts directory.
At present I'm using absolute paths, like this:
There is a way to not have to specify whether the include file is a .dsa (un-encrypted) or .dse (encrypted), using DzScript
String : getScriptFile( String filenameWithoutExtension )
Parameter(s):
filenameWithoutExtension - The path to the file, minus the '.' and the extension
Return Value:
The path of the file, with extension, of the script found. Otherwise, an empty String.
I have not tried it, but the Samples contain some examples of its use, e.g.:
Samples: Lesson Strips
http://docs.daz3d.com/lib/exe/fetch.php/public/software/dazstudio/4/referenceguide/scripting/api_reference/samples/education/lesson_strips/ds_4_lesson_strip_example.zip
e.g. The .zip above contains a 0.dsa containing this code:
Thank you everyone.
Absolute paths works; I'll check out the links, and no, not encrypted at this stage.