Reloading JavaScript files with QJSEngine
-
wrote on 19 May 2023, 11:18 last edited by
Hi !
I noticed that QJSEngine caches JavaScript files when they get imported, and I would like to override this behavior. It doesn't seem possible, but I'll just ask just in case.
I use JavaScript files in a game engine, and to facilitate game development, I would like to enable hot reload of scripts, rather than having to close and restart the entire application to fix a script. Any idea on how to achieve this without having to clear the cache is welcome as well !
Cheerio !
-
Hi !
I noticed that QJSEngine caches JavaScript files when they get imported, and I would like to override this behavior. It doesn't seem possible, but I'll just ask just in case.
I use JavaScript files in a game engine, and to facilitate game development, I would like to enable hot reload of scripts, rather than having to close and restart the entire application to fix a script. Any idea on how to achieve this without having to clear the cache is welcome as well !
Cheerio !
wrote on 19 May 2023, 12:09 last edited by JonB@plaristote
I assume you mean you use QJSEngine::importModule(const QString &fileName)? ThenIf this is the first time the module is imported in the engine, the file is loaded from the specified location
Subsequent imports of the same module will return the previously imported instance. Modules are singletons and remain around until the engine is destroyed
The specified fileName will internally be normalized using QFileInfo::canonicalFilePath(). That means that multiple imports of the same file on disk using different relative paths will load the file only once.Does not bode well for your desire!? :(
I haven't used QML or QJSEngine, so asking me further questions is liable to be fruitless, but is it possible to load the JS script you want as a string you have read from file instead of passing the filename to
QJSEngine
? -
@plaristote
I assume you mean you use QJSEngine::importModule(const QString &fileName)? ThenIf this is the first time the module is imported in the engine, the file is loaded from the specified location
Subsequent imports of the same module will return the previously imported instance. Modules are singletons and remain around until the engine is destroyed
The specified fileName will internally be normalized using QFileInfo::canonicalFilePath(). That means that multiple imports of the same file on disk using different relative paths will load the file only once.Does not bode well for your desire!? :(
I haven't used QML or QJSEngine, so asking me further questions is liable to be fruitless, but is it possible to load the JS script you want as a string you have read from file instead of passing the filename to
QJSEngine
?wrote on 19 May 2023, 21:31 last edited by@JonB This does not bode well indeed !
Unfortunately, I don't think I can consider loading the scripts as strings: the scripts may themselves use the
import
statement, which I assume uses the same mechanisms asimportModule
.Though that opens the possibility of not using ECS6 imports and instead implementing my own system of import. Sounds like it would be fairly complicated, but that might be worth a try. Thanks for your help !
1/3