engine icon indicating copy to clipboard operation
engine copied to clipboard

Enabled parameter not included in awaiting component when create script

Open YuniChou opened this issue 10 months ago • 5 comments

Hi all, I'm having trouble synchronizing the enabled attribute of a script. I will import the script files after calling app.scene.loadScene and it will miss the initializeComponentData function (script/system.js). The script should bind automatically on entity when registerScript runs, as it checks is there any components awaiting for script. (script-registry.js:134).

However, I found that the enabled value from _scriptsData is missing in the create function’s parameters. Would it be reasonable to add it back like the example below?

Image

Waiting for your feedback and suggestions. Thank you!

YuniChou avatar Feb 19 '25 09:02 YuniChou

Hey @YuniChou thanks for flagging this. Do you have a repro of this error?

marklundin avatar Feb 19 '25 19:02 marklundin

Hi @marklundin , here is my repo that can reproduce. Thanks for taking a look at this!

I'm making a project which whould like to load diffrient editor scenes using engine-only way. There are two way can repro (main.js). The script hidden need to be disabled on Root entity.

A: This one is a litte bit tricky. In order to prevent error expection in __game-scripts.js will stuck the thread when app.start, I force game-scripts load after app.loadScene. It can 100% repro the flow.

B: I don't call app.scenes.loadScene after app.preload to ensure __game-scripts.js load first. There is a chance that the flow may occur, because the script is not ready when initializeComponentData is called.

Following the other proper route indeed avoids the component awaiting situation, but I'm troubled because the enabled parameter isn't working. 🙇‍♂️

YuniChou avatar Feb 21 '25 07:02 YuniChou

Thanks for the repro @YuniChou 🙏

I'm not 100% clear on the issue. I can see you have an empty dummy__game-scripts.js but not clear why you're overriding the fields in the config.json.

I have stripped out that part so it just loads the config, and you can see that it correctly does not initialize hidden script which is disabled. You can check this preview here. Let me know if I'm missing something obvious here.

marklundin avatar Feb 21 '25 10:02 marklundin

Sorry for making you misunderstand! Let me use another example to clarify. preview here

Without overriding anything, I set hidden.js preload to false and will manually load it later. The flow works on https://playcanv.as/b/1d3a4567;

The Box entity's hidden script enabled is set to true. The Root entity's hidden script enabled is set to false. After manually loading hidden.js, both Box and Root's hidden scripts will be triggered.

const asset = app.assets.find('hidden.js');
app.assets.load(asset);

Or is this something I shouldn't do? 🙏

config.json

{
    "215181886": {
      "name": "hidden.js",
      "preload": false,
    }
}

2175667.json

{
    "e951e842-82e0-11ee-9eae-baf4d5e490e2": {
      "name": "Root",
      "components": {
        "script": {
          "enabled": true,
          "order": ["hidden", "hello"],
          "scripts": {
            "hidden": { "enabled": false, "attributes": {} },
            "hello": { "enabled": true, "attributes": {} }
          }
        }
      },
    },
    "e952058e-82e0-11ee-9eae-baf4d5e490e2": {
      "name": "Box",
        "script": {
          "enabled": true,
          "order": ["hidden"],
          "scripts": { "hidden": { "enabled": true, "attributes": {} } }
        }
      },
}

YuniChou avatar Feb 21 '25 11:02 YuniChou

Without overriding anything, I set hidden.js preload to false and will manually load it later. The flow works on https://playcanv.as/b/1d3a4567;

The Box entity's hidden script enabled is set to true. The Root entity's hidden script enabled is set to false. After manually loading hidden.js, both Box and Root's hidden scripts will be triggered.

In sync or async loading scenarios, initialize method of scripts, should not trigger if any of:

  1. Entity or any of its ancestors are disabled.
  2. Script is disabled within Script Component.

So if this behaves differently, then it is bug indeed.

Maksims avatar Feb 21 '25 12:02 Maksims