Babylon Unity Toolkit - accessing other typeScript files within a Scene

Hello,

I am trying to access other typeScript files within my scene, so that I can trigger certain elements.
I’ve tried to access via: import { Example } from ‘exampleClass’;
unfortunately I get this error:
/Assets/Scripts/exampleClass.ts’ is not a module.

Is there maybe a different way of accessing other typeScript files within my scene?

Maybe @MackeyK24 can help here again :slight_smile:

Yup @MackeyK24 is the unity toolkit guru :slight_smile:

First off, the toolkit use a tsconfig.json that gets created on first build. So all your script component are aware of each other and you dont need to do imports in your script component.

To get reference to script components attached to entities at runtime you would use either the script component shortcut function to get the component. So from a BABYLON.ScriptComponent:

const animator:BABYLON.AnimationState = this.getComponent("BABYLON.AnimationState");

would get a reference to the Unity Style Animator component that is also on the same game object your script is on. Just like how unity does GetComponent.

A more general purpose function you could always use to get a reference for any script component attached to any game object that you could call from anywhere in your project (not just the entity your script is attached to):

const animator:BABYLON.AnimationState = BABYLON.SceneManager.FindScriptComponent(transformNode, "BABYLON.AnimationState");

You can then make function calls to the Unity Style Animator component to update an animation parameter, example:

animator.setFloat("Speed", this.speed);
animator.setBool("Grounded", this.grounded);

You might use to cause the animation states to transition to the proper animation state based on transitions and conditions setup in the Unity Animation controller.

Example script component:

module PROJECT {
    /**
    * Babylon Script Component
    * @class MyTest
    */
    export class MyTest extends BABYLON.ScriptComponent {

        protected awake(): void {
            /* Init component function */
            const animator:BABYLON.AnimationState = this.getComponent("BABYLON.AnimationState");
            if (animator != null) {
                animator.setFloat("Speed", 0);          // Note: Set initial speed parameter to zero
                animator.setBool("Grounded", true);     // Note: Set initial grounded state to true
            }
        }

        protected start(): void {
            /* Start render loop function */
        }

        protected ready(): void {
            /* Execute when ready function */
        }

        protected update(): void {
            /* Update render loop function */
        }

        protected late(): void {
            /* Late update render loop function */
        }

        protected after(): void {
            /* After update render loop function */
        }

        protected fixed(): void {
            /* Fixed update physics step function */
        }

        protected destroy(): void {
            /* Destroy component function */
        }
    }
}

You can also safely removed any unused life-cycle functions. The template is to show you all the functions of the script components life cycle you can use. Like Unity’s Monobehavior class

Hope that helps :slight_smile:

3 Likes

Hi there @symetry just checking in, did Mackey’s answer solve your problem? :smiley:

Hi is there a way to access the component of a pickedResult from raycast? and is there also a way to access a ScriptComponents physics component? to change mass / add force etc.

In my scriptcomponent i try to find the PhysicsImposter attached to the transform, but it returns null.

console.log("1 " + this.getComponent("BABYLON.PhysicsImpostor"));
console.log("2 " + BABYLON.SceneManager.FindScriptComponent(this.transform, "BABYLON.PhysicsImpostor"));

Also documentation is quite lacking so im pretty lost to be honest. Thanks in advance, whoever is reading this. are you still actively developing this @MackeyK24 ?

1 Like