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

May 14, 2012

SaveIt

Filed under: SaveIt — mark @ 12:31

Overview:

Context API Reference:
SaveContext Class
VFSSaveContext Class

LoadContext Class
VFSLoadContext Class

TableSerializer Classes

Customizing SaveIt:
Customize Saving/Loading Objects

HowTo:
Save and load a GameObjects position

What are VFS Contexts?

Customizing Saving/Loading Objects

Filed under: SaveIt,SaveIT API Reference — mark @ 12:27

When saving an object all what the SaveContext of SaveIt does is to store it into an internal TableEntry. The real magic comes when saving a class with a Save method or a class with an associated Serializer, this article describes both:

First, the Save and the Load method.
When saving this class:

1
2
3
4
5
public class Foo
{
	public int myValue;
	public string myOtherValue;
}

SaveIt will use a ClassSerializer which basically serialized/deserializes all members of the class, which means that SaveIt will store the myValue and myOtherValue member. Sometimes this is not what you want, for this there are several possibilities, the first one is the usage of the Load and Save Method, for this we extend our existing class:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Foo
{
	public int myValue;
	public string myOtherValue;
 
	void Save(SaveContext context, ref bool allowAutoSerialization)
	{
	}
 
	void Load(LoadContext context, ref bool allowAutoDeserialization)
	{
	}
}

SaveIt will call those methods when serializing or deserializing an instance of the Foo class and you can now specify what should happen when those are called.
Now we can save everything on our way, for this example we save only our int myValue and ignore the string myOtherValue:

1
2
3
4
void Save(SaveContext context, ref bool allowAutoSerialization)
{
	context.Save(myValue);
}

Thats it, we stored the value myValue, BUT SaveIt will still serialize everything else like it will be done without this Save method.
To change this we can set the bool allowAutoSerialization argument to false. SaveIt will then skip all other automatically performed actions.

1
2
3
4
5
void Save(SaveContext context, ref bool allowAutoSerialization)
{
	allowAutoSerialization = false;
	context.Save(myValue);
}

Loading the data inside the Load method, is equally simple:

1
2
3
4
5
6
void Load(LoadContext context, ref bool allowAutoDeserialization)
{
	allowAutoDeserialization = false;
	myValue = context.Load<int>(myValue);
	// or: context.Load(out myValue);
}

The other option to specify custom serialization is by using a type serializer, type serializers inherit from the interface ITypeSerializer:

1
2
3
4
5
6
public interface ITypeSerializer
{
	bool IsUsable(Type type);
	void Serialize(object what, SaveContext context);
	void Deserialize(Type type, LoadContext context);
}

But in most cases you want to use the SaveIt.TypeSerializer.ClassSerializer class to start with, as it performs most operations needed and allows full control on what to serialize/deserialize. But for the next example we will create a fresh Serializer without using ClassSerializer.

Here an example type serializer for the Foo class which does exactly the same thing we did earlier with the Load/Save methods in our Foo class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class FooSerializer : ITypeSerializer
{
	public bool IsUsable(Type type)
	{
		// We only want that this serializer works for the type Foo
		return type == typeof(Foo);
	}
 
	public void Serialize(object what, SaveContext context)
	{
		// This is needed in case our class we want to serialize can contain the Save method
		if (context.RunObjectSaveMethod())
		{
			return;
		}
 
		Foo instance = (Foo)what;
		context.Save(instance.myValue);
	}
 
	public void Deserialize(Type type, LoadContext context)
	{
		// First we need to create the instance of Foo we want to fill with our data
		Foo instance = new Foo();
 
		// Store the new created instance into the context, so SaveIt knows about the created instance and wont create it anew
		context.CurrentInstance = instance;
 
		// This is needed in case our class we want to deserialize can contain the Load method
		if (context.RunObjectLoadMethod())
		{
			return;
		}
 
		instance.myValue = context.Load<int>(myValue);
	}
}

After the TypeSerializer has been created, you need to store it into a static list of TypeSerializers SaveIt may use:

1
ContextBase.TypeSerializers.Add(new FooSerializer());

ContextBase is the base class from the LoadContext and SaveContext class in the SaveIt namespace.

TableSerializer Classes

Filed under: SaveIt,SaveIT API Reference — mark @ 11:34

Namespace: SaveIt.TableSerializer
Use these to serialize and deserialize the stored data from the Save and LoadContexts.

SaveIt provides some basic TableSerializers, all based upon the BinaryTableSerializer class.
All inhertied classes are able to compress and uncompress the serialized/deserialized data by setting the UseCompression property to true.

Following BinaryTableSerializers exist:

File
Loads and Saves the data to a file specified in the constructor.
Leaving away a file extension delimiter (a simple dot) will add .saveIt as file extension.

PlayerPrefs
Loads and Saves the data to the PlayerPrefs entry with the given name.

Web
Loads and Saves the data to the given URL. The load uses a GET and the save uses the POST verb.

Smart
Loads and Saves the data to the most propably source. When running a WebPlayer the PlayerPrefs will be used.
When using an URL beginning with a http: or https: the Web will be used. Otherwise a file will be written.

All SaveIt contexts (SaveContext and LoadContext) will use the Smart TableSerializer when providing a string as the first
parameter for the contexts constructor.


Example:

C#:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using SaveIt;
...
 
// This will load/save the data from/to the file "MySaveGame.dat"
TableSerializer.File tableSerializer = new TableSerializer.File("MySaveGame.dat");
 
// This will load/save the data from/to the PlayerPrefs string entry "SaveData"
TableSerializer.PlayerPrefs tableSerializer = new TableSerializer.PlayerPrefs("SaveData");
...
 
SaveContext saveContext = new SaveContext(tableSerializer);
saveContext.Save(currentLevel, "currentLevel");
saveContext.Save(score, "score");
saveContext.Flush(); // NEVER FORGET THIS!
...
 
LoadContext loadContext = new LoadContext(tableSerializer);
currentLevel = loadContext.Load<int>("currentLevel");
score = loadContext.Save<int>("score");

JS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import SaveIt;
...
 
// This will load/save the data from/to the file "MySaveGame.dat"
var tableSerializer : TableSerializer.File = TableSerializer.File("MySaveGame.dat");
 
// This will load/save the data from/to the PlayerPrefs string entry "SaveData"
var tableSerializer : TableSerializer.PlayerPrefs = TableSerializer.PlayerPrefs("SaveData");
...
 
var saveContext : SaveContext = SaveContext(tableSerializer);
saveContext.Save(currentLevel, "currentLevel");
saveContext.Save(score, "score");
saveContext.Flush(); // NEVER FORGET THIS!
...
 
var loadContext : LoadContext = LoadContext(tableSerializer);
currentLevel = loadContext.Load.<int>("currentLevel");
score = loadContext.Save.<int>("score");

LoadContext Class

Filed under: SaveIt,SaveIT API Reference — mark @ 11:07

Namespace: SaveIt
C#:

using SaveIt;

JS:

import SaveIt;

static VFSLoadContext FromUnbufferedFile(string path, ResourceEntry[] resourceEntries = null)
Parameters:

    path – the filepath to read from
    resourceEntries [optional] – array of precreated resources to use instead to create it on demand

Creates a new VFSLoadContext instance which reads from the given file specified through the path variable.

C#:

VFSLoadContext context = LoadContext.FromUnbufferedFile("SaveItData");
VFSLoadContext context = LoadContext.FromUnbufferedFile("SaveItData", myResourceEntries);

JS:

var context : VFSLoadContext = LoadContext.FromUnbufferedFile("SaveItData");
var context : VFSLoadContext = LoadContext.FromUnbufferedFile("SaveItData", myResourceEntries);

static LoadContext FromFile(string path, ResourceEntry[] resourceEntries = null)
Parameters:

    path – the filepath to read from
    resourceEntries [optional] – array of precreated resources to use instead to create it on demand

Creates a new LoadContext instance which reads from the given file specified through the path variable.

C#:

LoadContext context = LoadContext.FromFile("SaveItData");
LoadContext context = LoadContext.FromFile("SaveItData", myResourceEntries);

JS:

var context : LoadContext = LoadContext.FromFile("SaveItData");
var context : LoadContext = LoadContext.FromFile("SaveItData", myResourceEntries);

static LoadContext FromPlayerPrefs(string name, ResourceEntry[] resourceEntries = null)
Parameters:

    name- the playerprefs entry name
    resourceEntries [optional] – array of precreated resources to use instead to create it on demand

Creates a new LoadContext instance which reads from the given playerprefs key specified through the name variable.

C#:

LoadContext context = LoadContext.FromPlayerPrefs("SaveItData");
LoadContext context = LoadContext.FromPlayerPrefs("SaveItData", myResourceEntries);

JS:

var context : LoadContext = LoadContext.FromPlayerPrefs("SaveItData");
var context : LoadContext = LoadContext.FromPlayerPrefs("SaveItData", myResourceEntries);

static LoadContext FromWeb(string url, ResourceEntry[] resourceEntries = null)
static LoadContext FromWeb(WWW www, ResourceEntry[] resourceEntries = null)
Parameters:

    url- the URL to the saved file data
    www- the WWW class to use for loading the data
    resourceEntries [optional] – array of precreated resources to use instead to create it on demand

Creates a new LoadContext instance which reads from the given URL or WWW class.

C#:

LoadContext context = LoadContext.FromWeb("http://yourdomain/savegames/MySaveGame");
LoadContext context = LoadContext.FromWeb("http://yourdomain/savegames/MySaveGame", myResourceEntries);

JS:

var context : LoadContext = LoadContext.FromWeb("http://yourdomain/savegames/MySaveGame");
var context : LoadContext = LoadContext.FromWeb("http://yourdomain/savegames/MySaveGame", myResourceEntries);

static LoadContext FromStream(MemoryStream stream, ResourceEntry[] resourceEntries = null)
Parameters:

    stream- the Stream to the saved file data
    resourceEntries [optional] – array of precreated resources to use instead to create it on demand

Creates a new LoadContext instance which reads from the given Stream.

C#:

LoadContext context = LoadContext.FromStream(memoryStream);
LoadContext context = LoadContext.FromStream(memoryStream, myResourceEntries);

JS:

var context : LoadContext = LoadContext.FromStream(memoryStream);
var context : LoadContext = LoadContext.FromStream(memoryStream, myResourceEntries);

LoadContext(ITableSerializer tableSerializer = null, ResourceEntry[] resourceEntries = null)
Parameters:

    tableSerializer [optional] – the TableSerializer which loads from a stream
    resourceEntries [optional] – array of precreated resources to use instead to create it on demand

Creates a new LoadContext instance. When no TableSerializer is provided a default one will be used, which loads from the file: “default.saveIt”.

C#:

LoadContext context = new LoadContext();
LoadContext context = new LoadContext(new TableSerializer.File("SaveItData"));
LoadContext context = new LoadContext(new TableSerializer.File("SaveItData"), myResourceEntries);

JS:

var context : LoadContext = LoadContext();
var context : LoadContext = LoadContext(TableSerializer.File("SaveItData"));
var context : LoadContext = LoadContext(TableSerializer.File("SaveItData"), myResourceEntries);

LoadContext(string dataName, ResourceEntry[] resourceEntries = null)
Parameters:

    dataName[optional] – the dataName to load from
    resourceEntries [optional] – array of precreated resources to use instead to create it on demand

Creates a new LoadContext instance. The given dataName will be used to specify the filename, playerprefs key or web request URL used to load the data from.

C#:

LoadContext context = new LoadContext("MySaveGame");
LoadContext context = new LoadContext("http://yourdomain/savegames/MySaveGame");

JS:

var context : LoadContext = LoadContext("MySaveGame");
var context : LoadContext = LoadContext("http://yourdomain/savegames/MySaveGame");

bool IsDeferredType(Type type)
Parameters:

    type – the Type to test for

Returns:

    true when the object is a deferred type, false otherwise

Returns wether the given type is a type which needs to be loaded with special means, for example all Components are deferred types. these types need
to be loaded with the Load methods who are providing a callbackMethod or with the WaitForLoad methods.
In most cases you know that without using this method.

C#:

if (context.IsDeferredType(typeof(Foo)))
{
	context.WaitForLoad<Foo>("myFooValue", loadedFoo => myFooValue = loadedFoo);
}
else
{
	myFooValue = context.Load<Foo>("myFooValue");
}

JS:

if (context.IsDeferredType(Foo))
{
	context.WaitForLoad.<Foo>("myFooValue", function (loadedFoo) { myFooValue = loadedFoo; });
}
else
{
	myFooValue = context.Load.<Foo>("myFooValue");
}

bool IsDeferredValue(string name)
Parameters:

    name – the name of the value in the currrently deserializing object to test for

Returns:

    true when the value is a deferred type, false otherwise

Returns wether the value stored at the given name in the currently serializing object is a type which needs to be loaded with special means, for example all Components are deferred types. these types need
to be loaded with the Load methods who are providing a callbackMethod or with the WaitForLoad methods.
In most cases you know that without using this method.

C#:

if (context.IsDeferredValue("myFooValue"))
{
	context.WaitForLoad<Foo>("myFooValue", loadedFoo => myFooValue = loadedFoo);
}
else
{
	myFooValue = context.Load<Foo>("myFooValue");
}

JS:

if (context.IsDeferredValue("myFooValue"))
{
	context.WaitForLoad.<Foo>("myFooValue", function (loadedFoo) { myFooValue = loadedFoo; });
}
else
{
	myFooValue = context.Load.<Foo>("myFooValue");
}

void Load(Action<object> onLoaded)
void Load<T>(Action<T> onLoaded)
Parameters:

    onLoaded – a callBack to call when the value has been loaded

This loads the next available slot with the generated name: “Slot.” + nextSlotIndex + “]” and calls the callBack onLoaded when finished.
This method is usefull when loading values you know not if these types are deferred or not and you didnt provide any name when you stored it with the SaveContext.

C#:

context.Load(loadedObject =>
{
	myComponent = (MyComponent)loadedObject;
});
context.Load<MyComponent>(loadedComponent =>
{
	myComponent = loadedComponent;
});

JS:

context.Load(function (loadedObject)
{
	myComponent = (MyComponent)loadedObject;
});
context.Load.<MyComponent>(function (loadedComponent)
{
	myComponent = loadedComponent;
});

Load(string name, Action<object> onLoaded)
Load<T>(string name, Action<T> onLoaded)
Parameters:

    name – the name of the value to load
    onLoaded – a callBack to call when the value has been loaded

This loads value stored at the given name and calls the callBack onLoaded when finished.
This method is usefull when loading values you know not if these types are deferred or not.

C#:

context.Load("healthPoints", loadedValue =>
{
	healthPoints = (int)loadedValue;
});
context.Load<int>("healthPoints", loadedInteger =>
{
	healthPoints = loadedInteger;
});

JS:

context.Load("healthPoints", function (loadedValue)
{
	healthPoints = (int)loadedValue;
});
context.Load.<int>("healthPoints", function (loadedInteger)
{
	healthPoints = loadedInteger;
});

object Load()
T Load()
Parameters:

    none

Returns:

    the loaded value

This loads the next available slot with the generated name: “Slot.” + nextSlotIndex + “]” and returns the loaded result.

C#:

int healtPoints = (int)context.Load();
int healtPoints = context.Load<int>();

JS:

var healtPoints : int = (int)context.Load();
var healtPoints : int = context.Load.<int>();

object Load(string name)
T Load(string name)
Parameters:

    name – the name of the value to load

Returns:

    the loaded value

This loads value stored at the given name and returns the loaded result.

C#:

int healtPoints = (int)context.Load("healthPoints");
int healtPoints = context.Load<int>("healthPoints");

JS:

var healtPoints : int = (int)context.Load("healthPoints");
var healtPoints : int = context.Load.<int>("healthPoints");

void Load(out T target)
Parameters:

    target – the target to which the loaded value should be assigned to

This loads the next available slot with the generated name: “Slot.” + nextSlotIndex + “]” and puts the result into the given target.

C#:

int healtPoints;
context.Load(out healthPoints);

void Load(string name, out T target)
Parameters:

    name – the name of the value to load
    target – the target to which the loaded value should be assigned to

This loads value stored at the given name and puts the result into the given target.

C#:

int healtPoints;
context.Load("healthPoints", out healthPoints);

void WaitForLoad(string name, Action<object> onLoaded)
void WaitForLoad(string name, Action< T > onLoaded)
Parameters:

    name – the name of the value to wait for
    onLoaded – a callBack to call when the value has been loaded

This waits unto the next available slot with the generated name: “Slot.” + nextSlotIndex + “]” has been loaded and calls the callBack onLoaded when finished.
This method is usefull when somwhere else is a Load call and you only want to know when this call has been finished. You want to use it for all deferred types which cant be constructed with a normal new call.

C#:

context.WaitForLoad("someComponent", loadedObject =>
{
	myComponent = (MyComponent)loadedObject;
});
context.WaitForLoad<MyComponent>("someComponent", myBehaviour =>
{
	myComponent = myBehaviour;
});

JS:

context.WaitForLoad("someComponent", function(loadedObject)
{
	myComponent = (MyComponent)loadedObject;
});
context.WaitForLoad.<MyComponent>("someComponent", function(myBehaviour)
{
	myComponent = myBehaviour;
});

object LoadToInstance(object instance)
T LoadToInstance(T instance)
Parameters:

    instance – the target instance to load the data to

Returns:

    the instance provided in the parameters

This loads the next available slot with the generated name: “Slot.” + nextSlotIndex + “]” into the given instance. Use this when you already created an instance and want it to be filled with stored data. This is usefull when loading componants, as these are not creatable without using a GameObject.

C#:

context.LoadToInstance(gameObject.transform);

JS:

context.LoadToInstance(gameObject.transform);

object LoadToInstance(string name, object instance)
T LoadToInstance(string name, T instance)
Parameters:

    name – the name of the value to load
    instance – the target instance to load the data to

Returns:

    the instance provided in the parameters

This loads the value with the given name into the given instance. Use this when you already created an instance and want it to be filled with stored data. This is usefull when loading componants, as these are not creatable without using a GameObject.

C#:

context.LoadToInstance("myTransform", gameObject.transform);

JS:

context.LoadToInstance("myTransform", gameObject.transform);

T LoadComponent(string name, GameObject gameObject)
T LoadComponent(UnityEngine.GameObject gameObject)
Component LoadComponent(GameObject gameObject, Type componentType)
Component LoadComponent(string name, GameObject gameObject, Type componentType)
Parameters:

    name – the name to load the component from
    gameObject – the GameObject to which the component to load will be attached
    Type – the type of the component to load

Returns:

    the loaded component

This loads a component for the given name (when no name is provided the next available slot with the generated name: “Slot.” + nextSlotIndex + “]” will be used), for the given gameObject.

C#:

context.LoadComponent<Transform>(gameObject);
context.LoadComponent<Transform>("myTransform", gameObject);
context.LoadComponent(gameObject, typeof(Transform));
context.LoadComponent("myTransform", gameObject, typeof(Transform));

JS:

context.LoadComponent.<Transform>(gameObject);
context.LoadComponent.<Transform>("myTransform", gameObject);
context.LoadComponent(gameObject, Transform);
context.LoadComponent("myTransform", gameObject, Transform);

bool Exists(string name)
Parameters:

    name – the name of the value to check for existance

Returns:

    true when the value with the given name exists and false otherwise

This method will check wether a value with the given name exists.

C#:

if (context.Exists("MagicNumber"))
{
	int magicNumber = context.Load("MagicNumber");
}

JS:

if (context.Exists("MagicNumber"))
{
	var magicNumber : int = context.Load("MagicNumber");
}

bool MemberExists(string memberName)
Parameters:

    memberName- the memberName of the value to check for existance

Returns:

    true when the value with the given memberName exists and false otherwise

This method will check wether a value with the given memberName exists.

C#:

if (context.MemberExists("mainTexture"))
{
	context.LoadMember("mainTexture");
}

JS:

if (context.MemberExists("mainTexture"))
{
	context.LoadMember("mainTexture");
}

SaveContext Class

Filed under: SaveIt,SaveIT API Reference — mark @ 08:07

Namespace: SaveIt
C#:

using SaveIt;

JS:

import SaveIt;

static VFSSaveContext ToUnbufferedFile(string path, ResourceEntry[] resourceEntries = null)
Parameters:

    path – the filepath to write to
    resourceEntries [optional] – array of precreated resources to use instead to create it on demand

Creates a new VFSSaveContext instance which writes to the given file specified through the path variable.

C#:

VFSSaveContext context = SaveContext.ToUnbufferedFile("SaveItData");
VFSSaveContext context = SaveContext.ToUnbufferedFile("SaveItData", myResourceEntries);

JS:

var context : VFSSaveContext = SaveContext.ToUnbufferedFile("SaveItData");
var context : VFSSaveContext = SaveContext.ToUnbufferedFile("SaveItData", myResourceEntries);

static SaveContext ToFile(string path, ResourceEntry[] resourceEntries = null)
Parameters:

    path – the filepath to write to
    resourceEntries [optional] – array of precreated resources to use instead to create it on demand

Creates a new SaveContext instance which writes to the given file specified through the path variable.

C#:

SaveContextcontext = SaveContext.ToFile("SaveItData");
SaveContextcontext = SaveContext.ToFile("SaveItData", myResourceEntries);

JS:

var context : SaveContext = SaveContext.ToFile("SaveItData");
var context : SaveContext = SaveContext.ToFile("SaveItData", myResourceEntries);

static SaveContext ToPlayerPrefs(string name, ResourceEntry[] resourceEntries = null)
Parameters:

    name- the playerprefs entry name
    resourceEntries [optional] – array of precreated resources to use instead to create it on demand

Creates a new SaveContext instance which writes to the given playerprefs key specified through the name variable.

C#:

SaveContext context = SaveContext.ToPlayerPrefs("SaveItData");
SaveContext context = SaveContext.ToPlayerPrefs("SaveItData", myResourceEntries);

JS:

var context : SaveContext = SaveContext.ToPlayerPrefs("SaveItData");
var context : SaveContext = SaveContext.ToPlayerPrefs("SaveItData", myResourceEntries);

static SaveContext ToWeb(string url, ResourceEntry[] resourceEntries = null)
static SaveContext ToWeb(WWW www, ResourceEntry[] resourceEntries = null)
Parameters:

    url- the URL to the saved file data
    www- the WWW class to use for saving the data
    resourceEntries [optional] – array of precreated resources to use instead to create it on demand

Creates a new SaveContext instance which writes to the given URL or WWW class.

C#:

SaveContext context = SaveContext.ToWeb("http://yourdomain/savegames/MySaveGame");
SaveContext context = SaveContext.ToWeb("http://yourdomain/savegames/MySaveGame", myResourceEntries);

JS:

var context : SaveContext = SaveContext.ToWeb("http://yourdomain/savegames/MySaveGame");
var context : SaveContext = SaveContext.ToWeb("http://yourdomain/savegames/MySaveGame", myResourceEntries);

static SaveContext ToStream(MemoryStream stream, ResourceEntry[] resourceEntries = null)
Parameters:

    stream- the Stream to write to
    resourceEntries [optional] – array of precreated resources to use instead to create it on demand

Creates a new SaveContext instance which writes to the given Stream.

C#:

SaveContext context = SaveContext.ToStream(memoryStream);
SaveContext context = SaveContext.ToStream(memoryStream, myResourceEntries);

JS:

var context : SaveContext = LoadContext.ToStream(memoryStream);
var context : SaveContext = LoadContext.ToStream(memoryStream, myResourceEntries);

SaveContext(ITableSerializer tableSerializer = null, ResourceEntry[] resourceEntries = null)
Parameters:

    tableSerializer [optional] – the TableSerializer which loads and saves from/to a stream
    resourceEntries [optional] – array of precreated resources to use instead to create it on demand

Creates a new SaveContext instance. When no TableSerializer is provided a default one will be used, which saves to the file: “default.saveIt”.

C#:

1
SaveContext context = new SaveContext();
1
SaveContext context = new SaveContext(new TableSerializer.File("SaveItData"));
1
SaveContext context = new SaveContext(new TableSerializer.File("SaveItData"), myResourceEntries);

JS:

1
var context : SaveContext = SaveContext();
1
var context : SaveContext = SaveContext(TableSerializer.File("SaveItData"));
1
var context : SaveContext = SaveContext(TableSerializer.File("SaveItData"), myResourceEntries);

SaveContext(string dataName, ResourceEntry[] resourceEntries = null)
Parameters:

    dataName[optional] – the dataName to save to
    resourceEntries [optional] – array of precreated resources to use instead to create it on demand

Creates a new SaveContext instance. The given dataName will be used to specify the filename, playerprefs key or web request URL used to save the data to

C#:

SaveContext context = new SaveContext ("MySaveGame");
SaveContext context = new SaveContext ("http://yourdomain/savegames/MySaveGame");

JS:

var context : SaveContext = SaveContext ("MySaveGame");
var context : SaveContext = SaveContext ("http://yourdomain/savegames/MySaveGame");

void SaveMember(string memberName, string name = null)
Parameters:

    memberName- the name of the member of the current serializing object
    name [optional]- the name to store the member

Stores the value of the given member memberName of the currently serializing object at the given name, when no name is provided
a name will be generated: “[member:" + memberName + "]“.
You can only use this method when serializing an object (Custom serializer or Load/Save methods inside the objects class layout).

C#:

1
context.SaveMember("localPosition");
1
context.SaveMember("localPosition", "myPosition");

JS:

1
context.SaveMember("localPosition");
1
context.SaveMember("localPosition", "myPosition");

void Save(T what, string name = null)
Parameters:

    what – the object to save
    name [optional] – the name to store the value what

Stores the given object what with with the given name. When no name is given a name is generated: “[Slot." + currentSlot + "]”
This enables the ability to write Save and Load calls which mirrors themself without worrying about the names.

C#:

1
2
3
context.Save(name); // Name is: [Slot.0]
context.Save(active, "isActive"); // Name is: isActive
context.Save(healthPoints); // Name is: [Slot.1]

JS:

1
2
3
context.Save(name); // Name is: [Slot.0]
context.Save(active, "isActive"); // Name is: isActive
context.Save(healthPoints); // Name is: [Slot.1]

void Flush()
Parameters:

    none

Saves all stored objects to the TableSerializer (for example to a File).
You must call this after you stored all your objects!

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();

May 1, 2012

Manapotion

Filed under: Random — mark @ 15:50

Instead of buying a gift for the birthday of my beautiful wife I decided to create one with my bare hands. As inspiration I used a post in reddit, you can look it up here.

 

Here are the results..

 

The whole thing without special effects.

Click

 

A closer lookup to the flakon, a little note here, the image does not reflect the original appearance, the reality is more shiny.

Click

 

Now some images with the first special effect, a red glow (I searched for a blue or white, which appears as light blue due to the blue fluid, but coukld not find any, but I think it works really good as a nice contrast).

Click

Click

Click

The images where taken in a really dark environment so, some details are blurry or too dark in the pictures.

 

Finally an image with a very long exposure time (30 seconds) as the last special effect is only visible in really dark areas. So imagine this image in total darkness with only the light bluish elements in the picture glowing (they glow blue/lightblue).

Click

 

I realized the last glow effect with night active color, NightTec-something, a phosphorescent color which glows blue in the dark (when charged with enough light). My first try was to dissolve the color in water, but the problem with it is that the color will drop to the ground as little particles, looks like fairydust (not the drug) but the water remains muddy.

The first glow effect is a simple red LED.

 

Powered by WordPress