Repository: 4ian/GDevelop
File: GDJS/Runtime/CustomRuntimeObject.ts (TypeScript)

Code before:
          this._innerArea = {
            min: [0, 0, 0],
            max: [0, 0, 0],
          };
        }
        this._innerArea.min[0] = usedVariantData.areaMinX;
        this._innerArea.min[1] = usedVariantData.areaMinY;
        this._innerArea.min[2] = usedVariantData.areaMinZ;
        this._innerArea.max[0] = usedVariantData.areaMaxX;
        this._innerArea.max[1] = usedVariantData.areaMaxY;
        this._innerArea.max[2] = usedVariantData.areaMaxZ;
      }
      this._instanceContainer.loadFrom(customObjectData, usedVariantData);
    }

    protected abstract _createRender():
      | gdjs.CustomRuntimeObject2DRenderer
      | gdjs.CustomRuntimeObject3DRenderer;
    protected abstract _reinitializeRenderer(): void;

    override reinitialize(objectData: ObjectData & CustomObjectConfiguration) {
      super.reinitialize(objectData);

      this._reinitializeRenderer();
      this._initializeFromObjectData(objectData);

      // The generated code calls the onCreated super implementation at the end.
      this.onCreated();
    }

    override updateFromObjectData(
      oldObjectData: ObjectData & CustomObjectConfiguration,
      newObjectData: ObjectData & CustomObjectConfiguration
    ): boolean {
      const animator = this.getAnimator();
      if (animator) {
        animator.updateFromObjectData(
          oldObjectData.animatable || [],
          newObjectData.animatable || []
        );
      }
      if (oldObjectData.variant !== newObjectData.variant) {
        this._reinitializeRenderer();
        this._initializeFromObjectData(newObjectData);

        // The generated code calls the onCreated super implementation at the end.
        this.onCreated();
      }
      return true;
    }


Review comment (suggestion):
`_reinitializeRenderer` clear the layer rendered objects
`_initializeFromObjectData` add the new ones

Code after:
        if (!this._innerArea) {
          this._innerArea = {
            min: [0, 0, 0],
            max: [0, 0, 0],
          };
        }
        this._innerArea.min[0] = usedVariantData.areaMinX;
        this._innerArea.min[1] = usedVariantData.areaMinY;
        this._innerArea.min[2] = usedVariantData.areaMinZ;
        this._innerArea.max[0] = usedVariantData.areaMaxX;
        this._innerArea.max[1] = usedVariantData.areaMaxY;
        this._innerArea.max[2] = usedVariantData.areaMaxZ;
      }
      this._instanceContainer.loadFrom(customObjectData, usedVariantData);
    }

    protected abstract _createRender():
      | gdjs.CustomRuntimeObject2DRenderer
      | gdjs.CustomRuntimeObject3DRenderer;
    protected abstract _reinitializeRenderer(): void;

    override reinitialize(objectData: ObjectData & CustomObjectConfiguration) {
      super.reinitialize(objectData);

      this._reinitializeRenderer();
      this._initializeFromObjectData(objectData);

      // When changing the variant, the instance is like a new instance.
      // We call again `onCreated` at the end, like done by the constructor
      // the first time it's created.
      this.onCreated();
    }

    override updateFromObjectData(
      oldObjectData: ObjectData & CustomObjectConfiguration,
      newObjectData: ObjectData & CustomObjectConfiguration
    ): boolean {
      const animator = this.getAnimator();
      if (animator) {
        animator.updateFromObjectData(
          oldObjectData.animatable || [],
          newObjectData.animatable || []
        );
      }
      if (oldObjectData.variant !== newObjectData.variant) {
        const width = this.getWidth();
        const height = this.getHeight();
        const hasInnerAreaChanged =
          oldObjectData.isInnerAreaFollowingParentSize &&
          this._instanceContainer._initialInnerArea &&
          this._innerArea &&