Repository: 4ian/GDevelop
File: Extensions/Physics2Behavior/physics2runtimebehavior.ts (TypeScript)

Code before:
                // We could pass it a string, but lets do it right
                this.removeJoint(parseInt(jId, 10));
              }
            }
          }
        }

        // Remove the joint
        this.world.DestroyJoint(joint);
        delete this.joints[jointId];
      }
    }
  }
  gdjs.registerRuntimeSceneUnloadedCallback(function (runtimeScene) {
    if (
      // @ts-ignore
      runtimeScene.physics2SharedData &&
      // @ts-ignore
      runtimeScene.physics2SharedData.world
    ) {
      // @ts-ignore
      Box2D.destroy(runtimeScene.physics2SharedData.world);
    }
  });

  export class Physics2RuntimeBehavior extends gdjs.RuntimeBehavior {
    bodyType: string;
    bullet: boolean;
    fixedRotation: boolean;
    canSleep: boolean;
    shape: string;
    shapeDimensionA: any;
    shapeDimensionB: any;
    shapeOffsetX: float;
    shapeOffsetY: float;
    polygonOrigin: string;
    polygon: gdjs.Polygon | null;
    density: float;
    friction: float;
    restitution: float;
    linearDamping: float;
    angularDamping: float;
    gravityScale: float;
    layers: integer;
    masks: integer;
    shapeScale: number = 1;

    /**
     * Array containing the beginning of contacts reported by onContactBegin. Each contact
     * should be unique to avoid recording glitches where the object loses and regain
     * contact between two frames. The array is updated each time the method

Review comment (suggestion):
In the future, a `destroy()` method on the shared data would be I think safer (in the sense: it's part of the class, so there is less chance you forget to update it when needed) and we we should probably made the "shared data" something first class that is handled by the runtime scene (rather than something that is manually handled by the behavior like now).

Code after:
              if (
                this.joints[jId].GetType() === Box2D.e_gearJoint &&
                (Box2D.getPointer(
                  (this.joints[jId] as Box2D.b2GearJoint).GetJoint1()
                ) === Box2D.getPointer(joint) ||
                  Box2D.getPointer(
                    (this.joints[jId] as Box2D.b2GearJoint).GetJoint2()
                  ) === Box2D.getPointer(joint))
              ) {
                // We could pass it a string, but lets do it right
                this.removeJoint(parseInt(jId, 10));
              }
            }
          }
        }

        // Remove the joint
        this.world.DestroyJoint(joint);
        delete this.joints[jointId];
      }
    }
  }
  gdjs.registerRuntimeSceneUnloadedCallback(function (runtimeScene) {
    const physics2SharedData = runtimeScene.physics2SharedData;
    if (physics2SharedData && physics2SharedData.world) {
      Box2D.destroy(physics2SharedData.world);
      Box2D.destroy(physics2SharedData._tempb2Vec2);
      Box2D.destroy(physics2SharedData._tempb2Vec2Sec);
    }
  });

  export class Physics2RuntimeBehavior extends gdjs.RuntimeBehavior {
    bodyType: string;
    bullet: boolean;
    fixedRotation: boolean;
    canSleep: boolean;
    shape: string;
    shapeDimensionA: any;
    shapeDimensionB: any;
    shapeOffsetX: float;
    shapeOffsetY: float;
    polygonOrigin: string;
    polygon: gdjs.Polygon | null;
    density: float;
    friction: float;
    restitution: float;
    linearDamping: float;
    angularDamping: float;
    gravityScale: float;
    layers: integer;
    masks: integer;