Skip to content

Add command creation callback#10

Open
Bodix wants to merge 2 commits into
pixeption:masterfrom
Bodix:master
Open

Add command creation callback#10
Bodix wants to merge 2 commits into
pixeption:masterfrom
Bodix:master

Conversation

@Bodix
Copy link
Copy Markdown

@Bodix Bodix commented Jul 4, 2025

There is a problem with the initialization order when using dependency injection (in my case, Zenject). The problem is that the OnCommandsCreated callback is called too late for Zenject. Some internal logic is executed before it (namely, the command variable change callbacks registered to the OnValueChanged attribute), which may require dependencies that Zenject should have provided. This causes a NullReferenceException.

Initialization order of command:

  1. Initializing of default values of variables.
  2. Loading variables values from PlayerPrefs if they exist.
  3. Changing variables values followed by a callback. This may cause a NullReferenceException because
    the command dependencies have not yet been injected.
  4. Injecting dependencies by Zenject (too late).

So I created the OnCommandCreated callback, which is invoked per command before any internal callback.

Also, I recommend renaming the previous OnCommandsCreated to OnCommandsInitialized to distinguish between creation and initialization. Same with the HasCommandsCreated property (to HasCommandsInitialized). I didn't do it because it is breaking changes, and I don't know your package update pipeline.

@Bodix Bodix marked this pull request as ready for review July 4, 2025 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant