How to access private engine code from scripts without building your own version of the engine

How would I access private engine components from scripts. I know I can rebuild Playcanvas, but it’s a huge hassle.

Specifically I want to access ammo. The raycastFirst function is flawed since it not returning callback causes incorrect results in a loop. The raycast results write over each other.

You can monkey patch the engine to do something like this.

What’s the issue with raycastFirst. Using it in a loop should be fine as long as you are not saving the reference to the result object.

Can you post some code on how you are using it? Perhaps there’s an easy fix without needing to monkey patch.

“Using it in a loop should be fine as long as you are not saving the reference to the result object.”

That is what I must have done somehow. I tried creating a project to show off this “bug” and can’t recreate it. Thanks for that.

Is there documentation on how to access private engine code from scripts anyway without monkey patching it?

I could speed up my code by not getting unnecessary variable in my “raycastFirst” “result” object for instance.

If you want to override the functionality of the engine without using a custom version of the engine, the only way is to monkey patch it.

Alternatively, you can write a new function (or extension) for ray casting that does what you want to do.

What exactly do you mean by “monkey pathcing” do I have to use a local build or can I deploy it to some other host and have the editor get my build from there?

In Javascript, everything is public and accessible. Functions are also first class objects so they can assigned to variables.

That means that it is possible to reassign an existing function binding to something else instead.

eg https://playcanvas.com/editor/code/598984?tabs=16748303

pc.RigidBodyComponentSystem.prototype.raycastFirst = function(start, end) {
    console.log("Hello World");
    return null;
};

Project here: https://playcanvas.com/project/598984/overview/monkey-patching-example

I’ve overridden it here to print to the console instead of doing a physics raycast.

The advantage here is that you don’t need a custom engine build and your changes are self contained in the project.

2 Likes