Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Reloading JavaScript files with QJSEngine
Forum Updated to NodeBB v4.3 + New Features

Reloading JavaScript files with QJSEngine

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 316 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    plaristote
    wrote on 19 May 2023, 11:18 last edited by
    #1

    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 !

    J 1 Reply Last reply 19 May 2023, 12:09
    0
    • P plaristote
      19 May 2023, 11:18

      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 !

      J Offline
      J Offline
      JonB
      wrote on 19 May 2023, 12:09 last edited by JonB
      #2

      @plaristote
      I assume you mean you use QJSEngine::importModule(const QString &fileName)? Then

      If 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?

      P 1 Reply Last reply 19 May 2023, 21:31
      1
      • J JonB
        19 May 2023, 12:09

        @plaristote
        I assume you mean you use QJSEngine::importModule(const QString &fileName)? Then

        If 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?

        P Offline
        P Offline
        plaristote
        wrote on 19 May 2023, 21:31 last edited by
        #3

        @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 as importModule.

        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 Reply Last reply
        0

        1/3

        19 May 2023, 11:18

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved