Recast reference is not defined from RecastJSPlugin

i’m following ‘Create a Navigation Mesh’ tutorial from official docs
But when i added the plugin and initialized it, i got an error that i mentioned in title.
Is there additional steps that i need to prepare before using the plugin?

import
{
  RecastJSPlugin,
} from "@babylonjs/core";

let navigationPlugin = new RecastJSPlugin();

Did you await Recast(); before instantiating the plugin ?

No, Is it included in babylonjs package? or do i need to build it myself to use it?

Here is the example - https://github.com/RaananW/babylonjs-webpack-es6/blob/master/src/scenes/navigationMeshRecast.ts

You already did

import { RecastJSPlugin } from "@babylonjs/core/Navigation/Plugins/recastJSPlugin";

Need to add

import Recast from "recast-detour";

And then

        const recast = await Recast()

Oh thanks, i will try it nows

This is my import statements

import {
  MeshBuilder,
  Scene,
  Color3,
  AbstractMesh,
  SceneLoader,
  ActionManager,
  ExecuteCodeAction,
  Mesh,
  Vector3,
  RecastJSPlugin,
  StandardMaterial,
} from "@babylonjs/core";
import { Button, AdvancedDynamicTexture } from "@babylonjs/gui";
import Recast from "recast-detour";

And this is the part of my level code to set up navigation plugin.

 public async SetupNavigationPlugin() {
    const recast = await Recast();
    let tempGround = MeshBuilder.CreateGround("test_ground", {
      width: 10,
      height: 10,
      subdivisions: 20,
    });
    let navigationPlugin = new RecastJSPlugin();
    navigationPlugin.createNavMesh(
      [tempGround, this._outlineCollisionBox],
      NAV_PARAMETERS
    );

    var navmeshdebug = navigationPlugin.createDebugNavMesh(this.scene);
    navmeshdebug.position = new Vector3(0, 0.01, 0);

    var matdebug = new StandardMaterial("matdebug", this.scene);
    matdebug.diffuseColor = new Color3(0.1, 0.2, 1);
    matdebug.alpha = 0.2;
    navmeshdebug.material = matdebug;
  }

But i still getting this error whenever i run the code.
Errors below:
“recast.es6.js:9 Uncaught (in promise) TypeError: Cannot set properties of undefined (setting ‘Recast’)
at recast.es6.js:9:565757
at Level.SetupNavigationPlugin (Level.ts:103:26)
at new Level (Level.ts:99:10)”

at World.ts:57:21

Adding a “new” between await and recast solves the problem.

const recast = await new Recast();

I know it’s weird, and typescript gives an error. But it works for me

this shouldnot be needed indeed, maybe in your case it has already been awaited

My friend had the same problem a while back. We all use vite, and I’m not sure if it’s because of vite.

might totally be and I think @RaananW is trying to see what we can do for this ???

1 Like

Is this a typescript-only issue, or does the js side not working as well without the “new”? If it is ts-only it might just be a declaration issue that can be easily resolved. If you can somehow reproduce it to me I can test it myself.

The latter. Without “new” , this code will give an error on the js side.

The repository for reproducing the problem is here