How to lazily load physics plugin?

In the examples I’ve seen, the physics engines are specified via CDN in script tags like so:

<script src="https://preview.babylonjs.com/cannon.js"></script>
<script src="https://preview.babylonjs.com/ammo.js"></script>

And then subsequently available to be plugged in like so:

let physicsPlugin = new BABYLON.AmmoJSPlugin();
// let physicsPlugin = new BABYLON.CannonJSPlugin()
this.scene.enablePhysics(gravityVector, physicsPlugin)

Is there a way to not require the script tag upfront, but instead lazily load the plugin entirely in js? That way if I have different games that use different physics plugins, if the user only plays one game I didn’t need to load all possible physics plugins up front.

Hello you can leverage BABYLON.Tools.LoadScript() for that:
Tools | Babylon.js Documentation

Sweet. Thank you! I will check it out.

1 Like