3 lines
8.6 KiB
JavaScript
3 lines
8.6 KiB
JavaScript
var gdjs;(function(n){const l=new n.Logger("Variables");class h{constructor(e){this._type="number";this._value=0;this._str="0";this._bool=!1;this._children={};this._childrenArray=[];this._undefinedInContainer=!1;this._playerNumber=0;this.reinitialize(e)}reinitialize(e){if(this._type="number",this._value=0,this._str="0",this._bool=!1,this._children={},this._childrenArray=[],this._undefinedInContainer=!1,e!==void 0){if(this._type=e.type||"number",this._type==="number")this._value=parseFloat(e.value||"0"),this._value!==this._value&&(this._value=0);else if(this._type==="string")this._str=n.Variable.useDeprecatedZeroAsDefaultStringVariable?""+e.value||"0":e.value!==void 0?""+e.value:"";else if(this._type==="boolean")this._bool=!!e.value;else if(this._type==="structure"){if(e.children!==void 0)for(var t=0,r=e.children.length;t<r;++t){var i=e.children[t];i.name!==void 0&&(this._children[i.name]=new n.Variable(i))}}else if(this._type==="array"&&e.children)for(const s of e.children)this._childrenArray.push(new n.Variable(s))}}static isPrimitive(e){return e==="string"||e==="number"||e==="boolean"}static copy(e,t,r){if(r||t.clearChildren(),t.castTo(e.getType()),e.isPrimitive())t.setValue(e.getValue());else if(e.getType()==="structure"){const i=e.getAllChildren();for(const s in i)i.hasOwnProperty(s)&&t.addChild(s,i[s].clone())}else if(e.getType()==="array")for(const i of e.getAllChildrenArray())t.pushVariableCopy(i);return t}getNetworkSyncData(e){if(this.isUndefinedInContainer())return;const t=this.getPlayerOwnership();if(e.shouldExcludeVariableFromData){if(e.shouldExcludeVariableFromData(this))return}else{const s=e.playerNumber,a=e.isHost;if(t===null||s!==void 0&&t===0&&!a||t!==0&&s!==t)return}const r=this.getType();return{value:r==="structure"||r==="array"?"":this.getValue(),type:r,children:this.getStructureNetworkSyncData(this),owner:t}}getStructureNetworkSyncData(e){if(e.getType()==="array"){const t=[];return e.getAllChildrenArray().forEach(r=>{const i=r.getType(),s=i==="structure"||i==="array"?"":r.getValue(),a=r.getPlayerOwnership();r.isUndefinedInContainer()||a===null||t.push({name:"",value:s,type:i,children:this.getStructureNetworkSyncData(r),owner:a})}),t}if(e.getType()==="structure"){const t=e.getAllChildren();if(!t)return;const r=[];return Object.entries(t).forEach(([i,s])=>{const a=s.getType(),o=a==="structure"||a==="array"?"":s.getValue(),u=s.getPlayerOwnership();s.isUndefinedInContainer()||u===null||r.push({name:i,value:o,type:a,children:this.getStructureNetworkSyncData(s),owner:u})}),r}}updateFromNetworkSyncData(e,t){const r=e.owner,i=n.Variable.getVariableDataFromNetworkSyncData(e);if(!(t.shouldExcludeVariableFromUpdate&&t.shouldExcludeVariableFromUpdate(this))){if(!t.ignoreVariableOwnership){const s=n.multiplayer.getCurrentPlayerNumber(),a=this.getPlayerOwnership();if(s===a)return;r&&r!==a&&this.setPlayerOwnership(r)}this.reinitialize(i)}}fromJSObject(e){if(e===null)this.setString("null");else if(typeof e=="number")Number.isNaN(e)?(l.warn("Variables cannot be set to NaN, setting it to 0."),this.setNumber(0)):this.setNumber(e);else if(typeof e=="string")this.setString(e);else if(typeof e!="undefined")if(typeof e=="boolean")this.setBoolean(e);else if(Array.isArray(e)){this.castTo("array"),this.clearChildren();for(const r in e)this.getChild(r).fromJSObject(e[r])}else if(typeof e=="object"){this.castTo("structure"),this.clearChildren();for(var t in e)e.hasOwnProperty(t)&&this.getChild(t).fromJSObject(e[t])}else typeof e=="symbol"?this.setString(e.toString()):typeof e=="bigint"?(e>Number.MAX_SAFE_INTEGER&&l.warn("Error while converting JS variable to GDevelop variable: Integers bigger than "+Number.MAX_SAFE_INTEGER+" aren't supported by GDevelop variables, it will be reduced to that size."),this.setNumber(parseInt(e,10))):typeof e=="function"?l.error("Error while converting JS variable to GDevelop variable: Impossible to set variable value to a function."):l.error("Error while converting JS variable to GDevelop variable: Cannot identify type of object "+e);return this}fromJSON(e){try{var t=JSON.parse(e)}catch(r){return
|
|||
|
|
//# sourceMappingURL=variable.js.map
|