I have a small 3D application that I ported from Flash using OpenFL. It is working well in a single instance running in HTML, but I need to run two or more instances on the same webpage.
This is causing some issues in Lime / OpenFL, as I think that this use case is beyond it’s usual scope (ie games, often full screen).
I hacked the code base a little to remove some static variables, including the GL.hx class, and I’m now at the stage that both 3D outputs are displayed, but they are on the same div - one 3D canvas on top of the other! I’m unsure if it is worth further hacking to try to get this to work, as I’m unsure how many static variables there are that are shared between the two, or more, instances.
Is there a better way to do this in Javascript? ( I seem to recall that this could be done in Flash with flash.system.ApplicationDomain).
I thought that separate copies of OpenFL would each run in a JS module, meaning they are isolated from the global scope (so long as you don’t use -Dmodular). Does this not work?
Above is link to the Hello Triangle demo( built with stock OpenFL 4.5.1 / Lime 3.5.1) with two instances of the hello triangle loaded. Neither is rendered at all. If either of the lime.embed() methods below are remarked out, then the other will render successfully.
I’m not sure I understand what you mean by JS module?
Also, I searched the OpenFl and Lime libraries and found no reference to ‘#if modular’
Static globals such as lime.app.Application.current will be overwritten when the second instance is loaded. I’ve fixed a few of these in my branch of lime/openfl but I’m unsure how many there are of them!
It was caused by lime.embed being overwritten. As a temporary solution (it should be a parameter to the embed method, but I’m open to ideas here) I have just committed changes to Lime and OpenFL, and exposed “lime.(name of application file).embed” in addition to the ordinary embed method.
I created two separate projects, “MultiEmbedA” and “MultiEmbedB”, and built them both. This resulted in “MultiEmbedA.js” and “MultiEmbedB.js”, which expose unique lime.MultiEmbedA.embed and lime.MultiEmbedB.embed entry points. My quick test page source looks like this:
I hope to make multi-module projects (like this) easier to manage in the future. Thanks again for any input on how this should work from an ergonomic/API perspective
I am concerned about name collisions, but I think that using (app file name).embed (or the <app file="" /> name) should be flexible enough to probably use it as the default name for templates. As a result, I’ve changed OpenFL and Lime on the development version to use this instead of lime.embed
For example, the Pirate Pig sample would have PiratePig.embed and not lime.embed in the “index.html” file
The -Dmodular setting is done in the “include.xml” for Lime and OpenFL, and does not impact the source code. In order to use multiple different OpenFL applications on the same page, and use modular code, we will have to look at our use of static variables, and perhaps use macros to cache global values (such as openfl.media.SoundMixer) using some kind of application domain.
In the meantime, two full embeds should work, the file size might be a little larger for multiple embeds on the same page, but it should be functional
Thanks for the rapid response. I’ll have a look at this tomorrow with a fresh mind.
A big headache I was having with statics was the openfl.Lib singleton. How would you envisage application domain working in Javascript - for each rendercycle and each event, the application would need to overwrite Lib, SoundMixer, GL etc - where would be a good place to implement that?
We had a similar issue when deploying OpenFL HTML5 exports into a system which enforced strict AMD. Adhering to the AMD solved the issue for us, but we had to modify the way that export (we did it by hand ) to get a bootstrap process that looks like this.
I am making custom graphs and I was hoping to re-use the one application in multiple places on a webpage. Your solution has 2 different applications? Any ideas on how to allow multiple instances on one web page?
I’m using OpenFL 8.2.2, and when trying to embed 3 instances of the same HTML5 app it doesn’t work correctly. Only the 1st assets load and the cursor change on the 3rd div when touching the 1st one …
And also I had to do Assets.loadBitmapData instead of simple Assets.getBitmapData that works when there is only one instance…
Is it possible to embed many OpenFL instances on the same web page please ? How to do it ? And what about this problem : https://github.com/openfl/lime/issues/967
Multiple embeds don’t currently use closures, so static values are shared between them. We do have multiple embeds on our website for the NPM learn section, but specifically to your question, the Haxelib-based approach right now uses unique string names per project. Embedding the same project twice will cause a collision since they both share the same name. The big thing here (again) is in anything static, but if you are careful, I know I have gotten it working before.
Happy if anyone has suggestions for better using closures to make every embed unique, static and otherwise
Thanks for you answer Joshua,
Yes it would be great if someone fiond a solution for that.
BTW, do you mean If I rename my project (export app js file you mean right) it will work correctly ?