// @ts-check /* * GDevelop JS Platform * Copyright 2013-2016 Florian Rival (Florian.Rival@gmail.com). All rights reserved. * This project is released under the MIT License. */ /** * VariablesContainer stores variables, usually for a a RuntimeGame, a RuntimeScene * or a RuntimeObject. * * @memberof gdjs * @class VariablesContainer * @param {Array} [initialVariablesData] Optional array containing representations of the base variables. */ gdjs.VariablesContainer = function(initialVariablesData) { if ( this._variables === undefined ) this._variables = new Hashtable(); if ( this._variablesArray === undefined ) this._variablesArray = []; if ( initialVariablesData !== undefined ) this.initFrom(initialVariablesData); }; gdjs.VariablesContainer._deletedVars = gdjs.VariablesContainer._deletedVars || []; /** * Initialize variables from a container data.
* If `keepOldVariables` is set to false (by default), all already existing variables will be * erased, but the new variables will be accessible thanks to getFromIndex.
* if `keepOldVariables` is set to true, already existing variables won't be erased and will be * still accessible thanks to getFromIndex. * * @param {Array} data The array containing data used to initialize variables. * @param {Boolean} [keepOldVariables] If set to true, already existing variables won't be erased. */ gdjs.VariablesContainer.prototype.initFrom = function(data, keepOldVariables) { if ( keepOldVariables === undefined ) keepOldVariables = false; if ( !keepOldVariables ) { gdjs.VariablesContainer._deletedVars = gdjs.VariablesContainer._deletedVars || []; this._variables.keys(gdjs.VariablesContainer._deletedVars); } var that = this; var i = 0; for(var j = 0;j= this._variablesArray.length ) { //Add automatically non-existing variables. var variable = new gdjs.Variable(); this._variables.put(name, variable); return variable; } else { /** @type {gdjs.Variable} */ var variable = this._variablesArray[id]; if ( variable.isUndefinedInContainer() ) { //Reuse variables removed before. gdjs.Variable.call(variable); } return variable; } }; /** * Check if a variable exists in the container. * @param {string} name The variable's name * @return {boolean} true if the variable exists. */ gdjs.VariablesContainer.prototype.has = function(name) { var variable = this._variables.get(name); return variable && !variable.isUndefinedInContainer(); }; /** * "Bad" variable container, used by events when no other valid container can be found. * This container has no state and always returns the bad variable ( see gdjs.VariablesContainer.badVariable ). * @static * @private */ gdjs.VariablesContainer.badVariablesContainer = { has: function() {return false;}, getFromIndex : function() { return gdjs.VariablesContainer.badVariable; }, get : function() { return gdjs.VariablesContainer.badVariable; }, remove : function() { return; }, add : function() { return; }, initFrom : function() { return; } }; /** * "Bad" variable, used by events when no other valid variable can be found. * This variable has no state and always return 0 or the empty string. * @static * @private */ gdjs.VariablesContainer.badVariable = { getChild : function() { return gdjs.VariablesContainer.badVariable; }, hasChild: function() {return false;}, isStructure: function() {return false;}, isNumber: function() {return true;}, removeChild : function() { return; }, setNumber : function() { return; }, setString : function() { return; }, getAsString : function() { return ""; }, getAsNumber : function() { return 0; }, getAllChildren : function() { return {}; }, add : function() { return; }, sub : function() { return; }, mul : function() { return; }, div : function() { return; }, concatenate : function() { return; }, setUndefinedInContainer : function() { return; }, isUndefinedInContainer : function() { return; } };