muitiple save files - no game attribute (HTML5)

 Introduction
In a rare case, when a StencylWork grown to a very large size and have many save variables and over-sized save data. It also reduce the severity of the save file corruption. also enables to show loading progress while the player and other data is loading. Sometimes, the game contains games inside  
The video above shows the tyiing feature which saved and loaded in multiple files. 

For example
to save a so-called "endless" scene of any kind https://stencyl-nator.blogspot.com/p/endless-scenes.html which expects a large save file. then this may split into many save files For example, 1 variable = 1 file. 

Pre-requisites
import flash.net.SharedObject;  
Contents
This logic involves 2 actor behaviors. 1 for saving, 1 for loading.  This Stencyl tutorial contains 2 vetsions, 
Version 1: uses Reflect
Version 2: without Reflect

WARNING: 
this page contains code depends on other scripts. Copying codes from this page without editing will result in complation errors (types not found).  These codes uses; 
rectangle drawing without using G: https://stencyl-nator.blogspot.com/p/rectangle-drawing-arbitrary-code-for.html . These drawing drawing are replacable with the original Stencyl Drawi ng
Sce4_behav stuffs, public static vars for the scene behavior (code mode).  

Versions
Version 1: works the same as standard save
load button
package scripts;
import com.stencyl.behavior.ActorScript; //all actors behavior extend ActorScripts
import flash.net.SharedObject;  //SharedOvject is for loading and saving

class Savt_load_behav extends ActorScript
{
public var rh:R = new R(); public var mos:Bool = false
public var id:Int = 1; 
public var h:Float; public var w:Float; public var x:Float; public var y:Float; 
public function new(a,b,c){super(b);}
override public function init()
{
actor.handlesCollisions = false; // this is to reduce lag for buttons. Do not apply to all actors especially objects for collision.
this.h = actor.cacheHeight; this.w = actor.cacheWidth; 
         this.x=actor.getX(); this.y=actor.getY(); 
actor.whenDrawingListeners.push(dwg); actor.mouseOverListeners.push(mis);
}

public function dwg()
{
if(this.mos){this.rh.draw_rect(this.x,this.y,this.w,this.h);}
}

public function mis(ms:Int)
{
if(ms == -1){this.mos = false;}
else if(ms == 1){this.mos = true;}
else if(ms == 3){this.load();}
}

public function load()
{
var shared_object:SharedObject = SharedObject.getLocal("test_game_save"+this.id);
var test_data = shared_object.data;  
if(this.id == 1){Sce4_behav.v1 = Reflect.field(test_data,"text"); Sce4_behav.last_loaded_save_id = Reflect.field(test_data,"tex");}
else if(this.id == 2){Sce4_behav.v2 = Reflect.field(test_data,"text");Sce4_behav.last_loaded_save_id = Reflect.field(test_data,"tex");}
else if(this.id == 3){Sce4_behav.v3 = Reflect.field(test_data,"text");Sce4_behav.last_loaded_save_id = Reflect.field(test_data,"tex");}
}
}

save button 
import com.stencyl.behavior.ActorScript;
import flash.net.SharedObject; 

class Savt_save_behav extends ActorScript
{
public var rect:R = new R(); public var mos:Bool = false public var id:Int =1;
public var h:Float; public var w:Float; public var x:Float; public var y:Float; 
public function new(a,b,c){super(b);}
override public function init()
{
actor.handlesCollisions = false; // this is to reduce lag for buttons. Do not apply to all actors especially objects for collision.
this.h = actor.cacheHeight; this.w = actor.cacheWidth; this.x=actor.getX(); this.y=actor.getY(); 
actor.whenDrawingListeners.push(dwg); actor.mouseOverListeners.push(mis);
}
public function dwg()
{
if(this.mos){this.rect.draw_rect(this.x,this.y,this.w,this.h);}
}
public function mis(ms:Int)
{
if(ms == -1){this.mos= false;}
else if(ms == 1){this.mos = true;}
else if(ms == 3){this.save();}
}

public function save()
{
var shared_object:SharedObject = SharedObject.getLocal("test_game_save"+this.id);
if(this.id == 1){Reflect.setField(shared_object.data,"text",Sce4_behav.v1); Reflect.setField(shared_object.data,"tex",1);}
if(this.id == 2){Reflect.setField(shared_object.data,"text",Sce4_behav.v2); Reflect.setField(shared_object.data,"tex",2);}
if(this.id == 3){Reflect.setField(shared_object.data,"text",Sce4_behav.v3); Reflect.setField(shared_object.data,"tex",3);}
shared_object.flush();
}

}

Version 2:
 the SharedObject data is an Array<String>. Not as useful as version 1 but to further optimise the saving and loading great use for inventory. and your gender list given that your character has multiple genders (Hint: they do not have to be politically correct). Everything else is the same as version 1 which are mouse interactions, and drawings. 

load
var sob:SharedObject = SharedObject.getLocal("test_game_sav"+this.id);
if(this.id == 1){Sce4_behav.v1 = sob.data[0]; }
else if(this.id == 2){Sce4_behav.v2 = sob.data[0];}
else if(this.id == 3){Sce4_behav.v3 = sob.data[0];}

save
var sob:SharedObject =  SharedObject.getLocal("test_game_sav"+this.id);
if(this.id == 1){sob.data[0] = Sce4_behav.v1;}
if(this.id == 2){sob.data[0] = Sce4_behav.v2;}
if(this.id == 3){sob.data[0] = Sce4_behav.v3;}
sob.flush(); 

No comments:

Post a Comment