Ver Fonte

hotfix: if you store Actions, you need to serialize them

(cherry picked from commit 0b043b7fd11f2ed2df845450d1d93d5568138501)
Stephan Fuchs há 2 meses atrás
pai
commit
1991dbef09
2 ficheiros alterados com 25 adições e 2 exclusões
  1. 24 1
      sugarcube/src/actions/action.ts
  2. 1 1
      sugarcube/src/interfaces.d.ts

+ 24 - 1
sugarcube/src/actions/action.ts

@@ -8,7 +8,7 @@ class Action{
 
 	captured = {};
 
-	constructor(settings){
+	constructor(settings={}){
 		Object.keys(settings).forEach(function (pn) {
 			this[pn] = clone(settings[pn]);
 		}, this);
@@ -20,6 +20,29 @@ class Action{
 		}
 		$.wiki(this.contents);
 	}
+
+	//#region System
+		_init(action){
+			Object.keys(action).forEach(function (pn) {
+				this[pn] = clone(action[pn]);
+			}, this);
+
+			return this;
+		}
+
+		clone = function () {
+			return (new setup.Action())._init(this);
+		};
+
+		toJSON = function () {
+			var ownData = {};
+			Object.keys(this).forEach(function (pn) {
+				if(typeof this[pn] !== "function")
+					ownData[pn] = clone(this[pn]);
+			}, this);
+			return JSON.reviveWrapper('(new setup.Action())._init($ReviveData$)', ownData);
+		};
+	//#endregion
 }
 
 setup.Action ??= Action;

+ 1 - 1
sugarcube/src/interfaces.d.ts

@@ -1,6 +1,6 @@
 declare module "twine-sugarcube" {
 	export interface SugarCubeSetupObject {
-		Action: { new(parameter:any): Action };
+		Action: { new(parameter?:any): Action };
 		Appearance: { new(playerCharacter: PlayerCharacter): Appearance };
 		BodyDescription: { new(character:Character): BodyDescription };
 		Consumable: { new(): Consumable };