PDA

View Full Version : Saving and loading data...



Anti-Distinctly
01-14-2008, 05:17 AM
Hi,
I've, yet again, run into a brick wall with lscript. All I'm trying to do is save some data in the save function, and then load it again. But noooo.

Here's the offending section of code (it's a channel filter plugin)


load: what,io
{
if(what == SCENEMODE) // processing an ASCII scene file
{
debug();
lightName = io.read();
m = io.readNumber();
setdesc("Match light, multiplier: " + m);
}
}

save: what,io
{
if(what == SCENEMODE)
{
//debug();
io.writeln(l.name);
io.writeln(m);
}
}

options
{
reqbegin("Match channel");
debug();
c0 = ctlnumber("Multiply factor", m);

return if !reqpost();

// get requester control values here
m = getvalue(c0);


reqend();

setdesc("Match light, multiplier: " + m);
}


Right. The concern is with the value m. I cannot get this float/double value to save and be reloaded. The documentation does not provide any assistance either.
What happens is that io.writeln(m) will write the value as a string. Fine.
m = io.readNumber() will then fail. I don't know why., I just get an error.
m = io.read() works, but returns a string which then causes c0 = ctlnumber("Multiply factor", m) to fail whenever the options are clicked.

If I use io.writeNumber(m), or io.writeDouble(), all read functions fail when trying to load the data.

Please help, I'm being driven out of my tiny little mind.

jeremyhardin
01-14-2008, 08:59 AM
Your problem is how you read it. In ascii mode, (which the io functions are), you can only read and write strings.

So you'll need to read the string, the convert it to the float or int type you want.

You can do this directly by reading it, but specifying what to read it to...
io.read().asInt()
io.read().asNum()
and so on.

Anti-Distinctly
01-15-2008, 02:55 AM
That did the trick. Cheers Jezza :)
Now, just to get it to clone in the way I want...

Anti-Distinctly
01-15-2008, 03:05 AM
To be more specific, when the light to which the channel modifier script is attached is cloned, I want to reasses some values, as all the old variables will be pointing to the old light, not the cloned one.
I'm using the cloned{} function that is triggered when this cloning occurs, but am unsure of how to get the object agent of the light that it is now attached to...

walfridson
01-15-2008, 04:00 AM
Ok I have a similar question so I hope Mr Anti-Distinctly don't mind me jumping in.
What about saving object references?

Do I only need to save the objects name?
How do I load it correctly on load scene?

Thanks :)

Simple example doing it w(alfridson)rong

//-----------------------------------------
// LScript Item Animation template
//

@version 2.5
@script motion
@name Followtest
// global values go here

followme;
newpos;

//create: obj // with the Layout Object Agent argument
create // without the Layout Object Agent argument
{

}

destroy
{
// take care of final clean-up activities here
}

process: ma, frame, time
{
if(followme!=nil){
newpos = followme.getWorldPosition(time);
ma.set(POSITION,<newpos.x,newpos.y,newpos.z>);
}
}

load: what,io
{
if(what == SCENEMODE) // processing an ASCII scene file
{
if((string(io.read())=="followme")){
followme=string(io.read());
}
}
}

save: what,io
{
if(what == SCENEMODE)
{
if (followme!=nil){
io.writeln("followme");
io.writeln(followme.name);
}
}
}
options
{
reqbegin("Options");
reqsize(249,186);

c1 = ctlallitems("Follow:",followme);
ctlposition(c1,10,40,140,20);

return if !reqpost();

followme = getvalue(c1);

reqend();
}

Anti-Distinctly
01-15-2008, 05:24 AM
I'm very new to lscript (hence all the questions) but would saving the object ID work?
Or saving the name then just get the object agent by calling
light = Light(lightName)
or
light = Light(ID)
when loading.

walfridson
01-15-2008, 06:33 AM
Thanks but I get errors on both. Can't seem to find any clean scripts showing this..

Anti-Distinctly
01-15-2008, 08:47 AM
I'm not so sure about the Light(ID) one, but Light(lightName) should work fine. What are the errors? Try to strip the code down as much as possible to eliminate anything else as being the error.
I'm having cloning issues.
When the light is cloned, the create function is called whereby I store data such as the light's ID by using channel.parent.id or something like that.
I've made the lscript's desc function output the parent's id and the channel id, so I can make sure its referencing the correct object, which it seems to be..to start with.
I double click on it and click 'ok' (or do anything that calls the setdesc function again) and the values have all reverted back to the original object. Why are these values discarded!?

Please see the attached lscript...when I get to a computer that capable...

walfridson
01-15-2008, 09:37 AM
Well

if(what == SCENEMODE) // processing an ASCII scene file
{
saveID=integer(io.read());
followme.id = saveID;
}
saves it.

Looks like this in the lws:

Script C:\PROGRAM FILES (X86)\NEWTEK\LIGHTWAVE 3D 9.3\PLUGINS\FOLLOWTEST.LS
268435457
EndPlugin

Stuck on loading it, if that lws is correct :)...


loadID=integer(io.read());
followme.id = loadID;
Second line, unknown object reference

Anti-Distinctly
01-15-2008, 10:54 AM
try io.read().asInt(); if the loadID isn't correct in some way - write debug(); in the code and add loadID to the watch and see what's going on there.
Try saving the object name as opposed to the ID and loading the object like this:

followme = Mesh(objName);

Anti-Distinctly
01-15-2008, 10:56 AM
here's the channel filter I'm currently pulling my hair out about...

jeremyhardin
01-16-2008, 03:22 AM
Ok, so you want to have one value on the light follow another value on itself, then clone it and have light2 value one follow light2 value two, clone and have light3 value one follow light3 value 3.

Is that oversimplifying what you want?

Anti-Distinctly
01-16-2008, 05:46 AM
That's pretty much it in a nutshell. It's not just me being dumb is it? When you first clone the light, the create function has all the correct values, but at some point after that those values just end up pointing back to the original item.