What happen to SharedObject?


#1

my project is on
openfl 4.2.0
lime 3.2.1

i was trying the new
openfl 6.2.0
lime 5.6.0

There is a new way of save local data now? SharedObject is not working, i got this error “Cannot create sharedobject”


#2

so%20error


#3

By “new”, you mean “half a year old”? Because the latest versions are OpenFL 8.1.1 and Lime 6.3.1. :slight_smile: Have you tried those?


#4

openfl 8.1.1 and lime 6.3.1 haxe 3.4.4

Uncaught exception - Error #2134: Cannot create SharedObject.

Same error, what happen to SharedObject? there is another way to store local files now?


#5

I don’t think you can create a new instance of SharedObject. Instead, treat it as a static class and call getLocal ( i.e. SharedObject.getLocal(“objectName”); ) to get a local SharedObject instance.


#6

this work on openfl 4.2.0 but now in openfl 8.1.1 is not working anymore


#7

You’re not supposed to be able to do a new SharedObject, if you are using it that way, the constructor should be private.

If SharedObject.getLocal does not work, please edit your openfl/net/SharedObject.hx file and add additional traces to see what’s going on, and we can help


#8

I use SharedObject like this:

         package game;
import openfl.net.SharedObject;

class DataManager
{
	// satic var to implement singleton
	public static var instance:DataManager;

	// current slot to write / load data
	private var currentSlot:String;
	// data from shared object
	private var slot:SharedObject;
	// current instance data
	private var gameData:Dynamic;
	private var playerData:Dynamic;

	public function new()
	{
		// all game data config
		this.gameData =
		{
			currentLevel:1
		};
		// all players vars
		this.playerData =
		{
			heal:3,
			items:[],
			weapon:0,
			bullet:false,
			shield:0,
			attack:1,
			defense:1,
			coins:10
		};
	}

	public static function getInstance():DataManager
	{
		// if the instance is null, create a new one
		if(instance == null)
		{
			DataManager.instance = new DataManager();
		}
		// if is not null, return the current instance
		return DataManager.instance;
	}
	// reset for test
	public function resetForTest():Void
	{
		this.playerData.heal = 3;
		this.playerData.items = [];
		this.playerData.weapon = 0;
		this.playerData.bullet = false;
		this.playerData.shield = 0;
		this.playerData.attack = 1;
		this.playerData.defense = 1;
		this.playerData.coins = 10;
	}
	// game general data
	public function getGameData():Dynamic
	{
		return this.gameData;
	}
	// set current level
	public function setCurrentLevel(value:Int):Void
	{
		this.gameData.currentLevel = value;
	}
	// player Data
	public function getPlayerData():Dynamic
	{
		return this.playerData;
	}
	// heal
	public function setPlayerHeal(value:Int):Void
	{
		// apply new heal value
		this.playerData.heal += value/20;
		// control max value
		if(this.playerData.heal > 5)
		{
			this.playerData.heal = 5;
		}
	}
	// items
	public function setPlayerItems(value:Dynamic):Void
	{
		this.playerData.items = value;
	}
	public function pushPlayerItem(itemId:Int):Void
	{
		this.playerData.items.push(itemId);
	}
	public function removePlayerItem(position:Int):Void
	{
		this.playerData.items.remove(this.playerData.items[position]);
	}
	// weapon
	public function setPlayerWeapon(value:Int):Void
	{
		this.playerData.weapon = value;
	}
	public function setAttackValue(value:Int):Void
	{
		this.playerData.attack = value;
	}
	// bullet mode
	public function setBulletMode(value:Bool):Void
	{
		this.playerData.bullet = value;
	}
	// shield
	public function setPlayerShield(value:Int):Void
	{
		this.playerData.shield = value;
	}
	public function setDefenseValue(value:Int):Void
	{
		this.playerData.defense = value;
	}
	// coins
	public function setPlayerCoins(value:Int):Void
	{
		this.playerData.coins += value;
	}
	// reset
	public function resetSlot(slotName:String):Void
	{
		this.currentSlot = slotName;
		this.slot = SharedObject.getLocal(this.currentSlot);
		// reset current level
		this.slot.data.currentLevel = null;
		// reset heal
		this.slot.data.heal = null;
		// reset items
		this.slot.data.items = null;
		// reset weapons
		this.slot.data.weapon = null;
		this.slot.data.bullet = null;
		this.slot.data.shield = null;
		this.slot.data.attack = null;
		this.slot.data.defense = null;
		// reset money
		this.slot.data.coins = null;
		// write data
		this.slot.clear();
		this.slot.flush();
		this.slot = null;
	}
	// dowload slot from openfl.net.SharedObject;
	public function loadSlot(slot:String):Void
	{
		// set current slot
		this.currentSlot = slot;
		// get data from slot
		this.slot = SharedObject.getLocal(this.currentSlot);
		// get data from shared object if data is not null
		if(this.slot.data.currentLevel == null)
		{
			// do nothing
			this.gameData.currentLevel = 1;
			this.playerData.heal = 3;
			this.playerData.items = [];
			this.playerData.weapon = 0;
			this.playerData.bullet = false;
			this.playerData.shield = 0;
			this.playerData.attack = 1;
			this.playerData.defense = 1;
			this.playerData.coins = 10;
		}
		else
		{
			this.gameData.currentLevel = this.slot.data.currentLevel;
			this.playerData.heal = this.slot.data.heal;
			this.playerData.items = this.slot.data.items;
			this.playerData.weapon = this.slot.data.weapon;
			this.playerData.bullet = this.slot.data.bullet;
			this.playerData.shield = this.slot.data.shield;
			this.playerData.attack = this.slot.data.attack;
			this.playerData.defense = this.slot.data.defense;
			this.playerData.coins = this.slot.data.coins;
		}
	}
	// save slot on openfl.net.SharedObject;
	public function saveSlot():Void
	{
		// // get data from slot
		this.slot = SharedObject.getLocal(this.currentSlot);
		// set data
		this.slot.data.currentLevel = this.gameData.currentLevel;
		this.slot.data.heal = this.playerData.heal;
		this.slot.data.items = this.playerData.items;
		this.slot.data.weapon = this.playerData.weapon;
		this.slot.data.bullet = this.playerData.bullet;
		this.slot.data.shield = this.playerData.shield;
		this.slot.data.attack = this.playerData.attack;
		this.slot.data.defense = this.playerData.defense;
		this.slot.data.coins = this.playerData.coins;
		// // write data
		this.slot.flush();
	}
}      

#9

just replace with the old SharedObjec.hx from openfl 4.2.0 to the new openfl and it works :sweat_smile: