Dras.biz Just another WordPress site

June 27, 2012

What are VFS Contexts?

Filed under: SaveIt,SaveIT API Reference — mark @ 16:20

VFS stands for Virtual File System. SaveIt provides a VFSLoadContext and a VFSSaveContext who use this technique.

Basically the VFSSaveContext makes it possible to incremental write data to a single file without the need to rewrite the whole file
and without the need to batch all incoming save calls until Flush() needs to be called.

For example you need to use the plain SaveContext like this:

C#:

1
2
3
4
SaveContext context = SaveContext.ToFile("MyFile.dat");
context.Save(gameObject.name, "name");
context.Save(42, "magicNumber");
context.Flush();

JS:

1
2
3
4
var context : SaveContext = SaveContext.ToFile("MyFile.dat");
context.Save(gameObject.name, "name");
context.Save(42, "magicNumber");
context.Flush();

You always need to call the Flush method, which saves everything into the file named “MyFile.dat”.
The VFS eliminates the need of this:

C#:

1
2
3
VFSSaveContext context = SaveContext.ToUnbufferedFile("MyFile.dat");
context.Save(gameObject.name, "name");
context.Save(42, "magicNumber");

JS:

1
2
3
var context : VFSSaveContext = SaveContext.ToUnbufferedFile("MyFile.dat");
context.Save(gameObject.name, "name");
context.Save(42, "magicNumber");

After each Save call, the data is written into the file, without the need to rewrite the whole file. So you are
able to write some data and continue later to write some more data. For example small world changes.

The VFSLoadContext on the other hand is able to read from files created by the VFSSaveContext. The difference to the plain LoadContext
is that it is able to load the data on demand. For example, the LoadContext will read the whole file when created. The VFSLoadContext will only
load the data when a Load call is made and the data has not been loaded previously.

The VFS Context system works best when you save multiple classes or structs.

VFSLoadContext Class

Filed under: SaveIt,SaveIT API Reference — mark @ 16:05

Namespace: SaveIt.SingleFileVFS
C#:

using SaveIt.SingleFileVFS;

JS:

import SaveIt.SingleFileVFS;

VFSLoadContext(string fileName, ResourceEntry[] resourceEntries = null)
Parameters:

    fileName – the filename to the file to which this context should read
    resourceEntries [optional] – array of precreated resources to use instead to create it on demand

Creates a new VFSLoadContext instance. The main difference to the non VFS Context is that this context
will only read data from the disc when it needs to do.

C#:

1
VFSLoadContext context = new VFSLoadContext("SaveItData");
1
VFSLoadContext context = new VFSLoadContext("SaveItData", myResourceEntries);

JS:

1
var context : VFSLoadContext = VFSLoadContext("SaveItData");
1
var context : VFSLoadContext = VFSLoadContext("SaveItData", myResourceEntries);

VFSSaveContext Class

Filed under: SaveIt,SaveIT API Reference — mark @ 16:04

Namespace: SaveIt.SingleFileVFS
C#:

using SaveIt.SingleFileVFS;

JS:

import SaveIt.SingleFileVFS;

VFSSaveContext(string fileName, ResourceEntry[] resourceEntries = null)
Parameters:

    fileName – the filename to the file to which this context should write
    resourceEntries [optional] – array of precreated resources to use instead to create it on demand

Creates a new VFSSaveContext instance. The main difference to the non VFS Context is that you will not have to use the Flush()
command to store your data to disc. Instead the VFSSaveContext will save the data immediately as you call a Save method or the Delete command.

C#:

1
VFSSaveContext context = new VFSSaveContext("SaveItData");
1
VFSSaveContext context = new VFSSaveContext("SaveItData", myResourceEntries);

JS:

1
var context : VFSSaveContext = VFSSaveContext("SaveItData");
1
var context : VFSSaveContext = VFSSaveContext("SaveItData", myResourceEntries);

void Flush()
Parameters:

    none

Defragments the file and rewrites its content to disc. Its not needed to be called.
Use this method when you feel that the created file needs to much disc space.

C#:

1
2
3
context.Save(gameObject.name, "name");
context.Save(42, "magicNumber");
context.Flush();

JS:

1
2
3
context.Save(gameObject.name, "name");
context.Save(42, "magicNumber");
context.Flush();

void Delete(string name)
Parameters:

    name – an entry name which should be deleted

Deletes the entry with the given name from the context, reduces the ammount of memory needed for the whole file.

C#:

1
2
3
context.Save(gameObject.name, "name");
context.Save(42, "magicNumber");
context.Delete("name");

JS:

1
2
3
context.Save(gameObject.name, "name");
context.Save(42, "magicNumber");
context.Delete("name");

June 10, 2012

SaveIt How To save and load a GameObjects position

Filed under: SaveIt — mark @ 10:55

..save and load the position of a GameObject
To save or load the position you have several options:

All samples are based upon this:

C#:

1
var smart = new TableSerializer.Smart("MySaveData");
1
var loadContext = new LoadContext(smart);
1
var saveContext = new SaveContext(smart);

JS:

1
var smart : TableSerializer.Smart = TableSerializer.Smart("MySaveData");
1
var loadContext : LoadContext = LoadContext(smart);
1
var saveContext : SaveContext = SaveContext(smart);

1. Save only the position value from the transform component and load it when needed.
Saving:

C#:

1
saveContext.Save(myGameObject.transform.position);
1
saveContext.Save("ThePosition", myGameObject.transform.position); // an alternative

JS:

1
saveContext.Save(myGameObject.transform.position);
1
saveContext.Save("ThePosition", myGameObject.transform.position); // an alternative

Loading:

C#:

1
myGameObject.transform.position = loadContext.Load<Vector3>();
1
myGameObject.transform.position = loadContext.Load<Vector3>("ThePosition"); // an alternative

JS:

1
myGameObject.transform.position = loadContext.Load.<Vector3>();
1
myGameObject.transform.position = loadContext.Load.<Vector3>("ThePosition"); // an alternative

2. Save the whole Transform component (including, rotation, scale, eg.).
Saving:

C#:

1
saveContext.Save(myGameObject.transform);
1
saveContext.Save("TheTransformComponent", myGameObject.transform); // an alternative

JS:

1
saveContext.Save(myGameObject.transform);
1
saveContext.Save("TheTransformComponent", myGameObject.transform); // an alternative

Loading:

C#:

1
loadContext.LoadComponent<Transform>(myGameObject);
1
loadContext.LoadComponent<Transform>("TheTransformComponent", myGameObject); // an alternative
1
loadContext.LoadToInstance(myGameObject.transform); // an alternative
1
loadContext.LoadToInstance("TheTransformComponent", myGameObject.transform); // an alternative

JS:

1
loadContext.LoadComponent.<Transform>(myGameObject);
1
loadContext.LoadComponent.<Transform>("TheTransformComponent", myGameObject); // an alternative
1
loadContext.LoadToInstance(myGameObject.transform); // an alternative
1
loadContext.LoadToInstance("TheTransformComponent", myGameObject.transform); // an alternative

3. Save the whole GameObject.
Saving:

C#:

1
saveContext.Save(myGameObject);
1
saveContext.Save("TheGameObject", myGameObject); // an alternative

JS:

1
saveContext.Save(myGameObject);
1
saveContext.Save("TheGameObject", myGameObject); // an alternative

Loading:

C#:

1
var myGameObject = loadContext.Load<GameObject>();
1
var myGameObject = loadContext.Load<GameObject>("TheGameObject"); // an alternative

JS:

1
var myGameObject : GameObject = loadContext.Load.<GameObject>();
1
var myGameObject : GameObject = loadContext.Load.<GameObject>("TheGameObject"); // an alternative

Powered by WordPress