-
Notifications
You must be signed in to change notification settings - Fork 0
Capture_en
Capture is a amphipathic command which can be executed as a executable command and a query. In some cases, you don't want to save the whole object data because the main goal of the class isn't to save data and you want to save unrepeatable data such as settings or player profiles. Let's take the Game class for example:
public class Game {
private int score;
private Node world;
private Node player;
private String playerName;
public void start(){
//some code that loads the world and the player character and start the game
}
public void killEnemy(){
score++;
}
}When you create a new instance of this class and start it (by calling the start method) it will load the player character, the world and preparing the game. This class goal isn't storing data but it would be effort-consuming to write a new class and to move the data between the new class and the Game class especially that there will not be more than one instance of this new class to be saved in the database. Capture class will facilitate saving such data by capturing specific fields from the Game class and saving it in the database. When you try to save more than one capture of the same class the command will remove the previous capture if exists and will save the new one to prevent more than one capture of the same class in the database. To specify the fields that will be saved you should just annotate it with the Capturable annotation. This annotation is in the org.sofof.annotation package. This example will show you how to specify the savable data in the class:
public class Game {
@Capturable
private int score;
private Node world;
private Node player;
@Capturable
private String playerName;
public void start(){
//some code that loads the world and the player character and start the game
}
public void killEnemy(){
score++;
}
}After the player exit the game, the following code should be executed to save a capture from the Game instance:
Capture.capture(session, gameObject);This method will return 1 when there is a capture of the same class have been removed else it will return 0. What actually happens when calling this method is that it will search for any Capturable annotated field in the object class and it's parents and to save their values in the database under the SofofCapture binding name. To load the capture you can call the static method load on the Capture class that receives the session and the class that you want to load its capture and it will return null if there wasn't any capture for the passed class or an instance of the class Capture. This object has the cpatureTime() method that will return the capturing time in millisecond. To load the data from the Capture instance you should pass an instance of the class you want to load the data into it (a Game instance in this cause) to the copyTo method and it will load the data into it. The code will be like this:
Game game = new Game();
Capture capture = Capture.load(session, Game.class);
capture.coptTo(game);