Fix getting the length of an object array always returning 0

This commit is contained in:
Florian Rival
2021-06-22 10:16:43 +02:00
parent 23c1f2423d
commit 965f7aab32
2 changed files with 11 additions and 10 deletions

View File

@@ -635,10 +635,7 @@ namespace gdjs {
* @static
*/
static getVariableChildCount(variable: gdjs.Variable): integer {
if (variable.isStructure() == false) {
return 0;
}
return Object.keys(variable.getAllChildren()).length;
return variable.getChildrenCount();
}
/**

View File

@@ -13,6 +13,7 @@ namespace gdjs {
* A Variable is an object storing a value (number or a string) or children variables.
*/
export class Variable {
// TODO: convert this to an integer to speed up the type checks at runtime.
_type: VariableType = 'number';
_value: float = 0;
_str: string = '0';
@@ -455,7 +456,7 @@ namespace gdjs {
}
/**
* Return the object containing all the children of the variable
* Return the object containing all the children of the variable.
* @return All the children of the variable
*/
getAllChildren(): Children {
@@ -467,7 +468,7 @@ namespace gdjs {
}
/**
* Return an Array containing all the children of the variable
* Return an Array containing all the children of the variable.
*/
getAllChildrenArray(): gdjs.Variable[] {
return this._type === 'structure'
@@ -478,11 +479,14 @@ namespace gdjs {
}
/**
* Return the length of the collection
* Return the length of the collection.
*/
getChildrenCount() {
if (this.isPrimitive()) return 0;
return this.getAllChildrenArray().length;
getChildrenCount(): integer {
return this._type === 'structure'
? Object.keys(this._children).length
: this._type === 'array'
? this._childrenArray.length
: 0;
}
/**