PDA

View Full Version : Write to external file possible?



BlueApple
07-21-2008, 01:49 PM
Is there a method to create and/or write to an external text file using an LScript?

I have a script that replaces objects in a scene if certain conditions are met. I need to record the names of the items that did not meet the conditions and save them in a text file.

dump("myScriptName") appears to be an option, though not the most elegant. Is there a better way to do this?

Blochi
07-21-2008, 04:36 PM
That's what the file object agent is for.
You can start a new file, or step into a line and start overwriting from there. You only can't replace individual lines...

Blochi

BlueApple
07-22-2008, 09:05 AM
Been going through Mike Green's page describing File Object agents trying to get this to work. Below is what I have so far, but no file appears to get created in the directory. I am running this from a scene with an object in it, so the file path should be accurate. Any advice?


@version 2.3
@warnings
@script generic

generic
{
//Test variables that list different objects
sObjectName1 = "testObjA.lwo";

//Creates a string variable for the object directory
sObjectDirectory = getdir(OBJECTSDIR);

//Creates a new file object agent
sceneFile = File(sObjectDirectory+"temp.txt", "w");

//Creates a new file and opens it
sceneFile.open("temp.txt", "w");

//Writes data to the file
sceneFile.write(sObjectName1);
}

Blochi
07-22-2008, 09:54 AM
Some things I tried (didn't test the script):
- adding the separator to the path
- added error handling
- writing a full line instead of just one string
- removed the double opening
- closed the file




@version 2.3
@warnings
@script generic

generic
{
//Test variables that list different objects
sObjectName1 = "testObjA.lwo";

//Creates a string variable for the object directory
sObjectDirectory = getdir("Objects");

//Creates a new file object agent
sceneFile = File(""+sObjectDirectory+"\\"+"temp.txt", "w") || error("Could not save to <br>"+sObjectDirectory+"\\"+"temp.txt");

//The file is automatically open, no need for another sceneFile.open()

//Writes data to the file, one line
sceneFile.writeln(sObjectName1);

//Dont forget to close the file
sceneFile.close();
}

BlueApple
07-22-2008, 10:12 AM
Nailed it. Your script works great.

Thanks, Blochi.

BTW, I'm having some trouble constructing a plain old array. In Java or ActionScript writing
aMyArray = ["ham", "chicken", true; 643];
is perfectly legal, but doesn't seem to work in LScript. Is it necessary to declare the above array as follows?


aMyArray[1] = "ham";
aMyArray[2] = "chicken";
aMyArray[3] = true;
aMyArray[4] = 643;

Blochi
07-22-2008, 10:26 AM
The syntax above indicates a multidimensional array in LScript with associative indices.

You would do simple linear declaration like this:


aMyArray = @"ham", "chicken", true, [email protected];

BlueApple
07-22-2008, 10:30 AM
Got it. Thanks for your excellent assistance.
-Adam

Blochi
07-22-2008, 10:40 AM
No problem.
One more word of advice:
If your plan is to catch an entire scene in an array, process it and write it back to disk ... well, that approach is doomed. Been there. Works great on little test scenes, but on a decent production-size scene you will hit a wall with a very annoying LScript bug. Arrays can not grow over a certain size (something like 10000 elements), after that LW will just disappear.

A better way of processing scene files is line-by-line shoveling from one file to the other, or at least in chunks.

Blochi

BlueApple
07-22-2008, 10:53 AM
The array question was unrelated to this project, but I will certainly avoid overly large arrays in the future.

Thanks again.