Hi June. To implement callable functions in a Needle Engine build integrated into a Flutter web build, you’ll need to set up a communication layer between Flutter and your Needle Engine JavaScript runtime. Here’s a step-by-step approach you can follow:
Step-by-Step Pseudocode
Create an Interface in JavaScript:
Define JavaScript functions in your Needle Engine build that can be called from Flutter.
Use window object to expose these functions.
Register Event Listeners in Flutter:
Use dart:html to communicate with the JavaScript functions registered in the Needle Engine.
Function Calling Setup:
Establish a mechanism to call the JavaScript functions from your Flutter code using designated methods.
Return Data to Flutter from JavaScript:
Ensure that the JavaScript functions return data back to Flutter as needed.
Example Code
JavaScript (Needle Engine Build)
// Expose a function on the window object in your Needle Engine build
window.callFromFlutter = function(param) {
console.log("Function called from Flutter with param: " + param);
// Example function implementation
return "Response from Needle Engine: " + param;
};
Flutter Code
import 'dart:html' as html;
class MyFlutterClass {
void callNeedleFunction(String param) {
// Calling the JavaScript function
html.window.callMethod('callFromFlutter', [param])
.then((response) {
print("Response from Needle Engine: $response");
});
}
void registerView() {
ui_web.platformViewRegistry.registerViewFactory(
viewType,
(int viewId) {
needleElement = html.Element.tag('needle-engine');
return needleElement;
},
);
}
}
Explanation
JavaScript: The callFromFlutter function is defined globally. This will allow Flutter to call this method directly through the window object.
Flutter: Using html.window.callMethod, you can invoke the exposed JavaScript functions. Make sure to match the parameter types as needed.
Asynchronous Handling: If your JavaScript function has a return value (like in this case), Flutter can handle it asynchronously.
By following this structure, you should be able to establish a two-way communication channel between Flutter and your Needle Engine build. If you encounter any specific errors or issues, please let me know!
1 replies left - Get a PRO license for more AI replies. thread_JRYsi6yoEKCOp3NmE1hXsjZM Reply to this message to continue the conversation
(1) all methods and classes are registered in global scope Needle (e.g. window.Needle) which is primarely useful for CDN cases without a bundler/npm package workflow.
(2) You can access the context from the needle engine web component. In your case needleElement.context. The context gives you access to the scene and all objects and components (e.g. needleElement.context.scene.findObjectOfType(Camera) or needleElement.context.scene.children etc).
Classes and types can be imported as usual for modules via import { Camera } from "@needle-tools/engine" - this should also work with flutter (just as with any thirdparty module/package or if you’d be working with vanilla three.js)
There’s no separation between needle-engine and “the web”. Everything that you know about developing a javascript and web project applies to working with a needle engine web project. This means you can import and export classes and functions the same way and depending on your usecase e.g. create event listener functions that you later invoke from a component.
If you need more guidance happy to help. Please try provide a concrete example or a github repository that helps us to understand the problem. Thank you