-
Notifications
You must be signed in to change notification settings - Fork 275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Re-injecting" dependencies into a prefab when it's spawned from a MemoryPool? #284
Comments
I think I solved my own problem. I realize that if we take away monobehaviours. We would never re-inject dependencies into an object, it would be much easier to just destroy and create a new one. Extending that logic to Unity, it doesn't make sense to Re-inject an object. If an object does need New Dependencies, that it should be treated as a new object. |
Hi, Thanks for your I'm confused by your comment of "destroy and create a new one", which doesn't sound quite reasonable for me. flowchart TB;
A --> B;
a & b & c --> A;
Assume A depends on B, and all other objects depend on A. if we want to make A depends on another instance of B, your suggestion would be destroying A and recreate a new instance of A, which would in turn requires destroying and recreate all the 'a b c' instances. That would be unreasonable. For example, if my game has 1000 levels (each level has a json setting file), and on every level start, I want to inject the level setting into my MapController to populate the map and set the global settings. I think rebind and inject is the reasonable solution for such use cases. |
That's a good point, and you're correct, it would be a bad idea to destroy an object that has dependents. I think what I should have said is that it doesn't make sense to re-inject a scope. In traditional IoC Container you can create and destroy scopes easily, so long as it's not the Singleton Scope, but in Unity we don't want to do that because object instantiation is really expensive, as opposed to a traditional application. I'm even a little confused by original post (was over a year ago) but a simpler break down is.
That works all nice and dandy, but we're dealing with Unity, so we want to pool our Game Objects.
Needless to say I don't use Zenject anymore, I don't think it plays nicely with Unity and should probably be reserved for high level logic. |
My goal is to have a somewhat versatile GameObject that contains the following features:
From my research it's possible to pass new dependencies to a Prefab using a Factory, and resolving a monoinstaller from the SubContainer of the prefab. However this doesn't work if the prefab is memory pooled, because the prefab is created at the start of the game, it's container already created and cannot be recreated after the fact.
The documentation says to use the Facade pattern. Where by we manually pass these new dependencies to a facade monobehaviour on the prefab. The only issue with this, is that all other mono behaviours on the prefab will now have to reference the facade for the new data, which kind of defeats the whole purpose of Zenject, seeing as we already "kind of" have a reference to the dependencies via the [Inject] keyword we shouldn't also have to reference the facade.
I've managed to get around the issue somewhat, and thought this solution might help some people:
The other solution is to simply only use reference types for the Bindings and update their properties / fields, which is what I'll probably end up doing.
However I was wondering if there was other ways to approach the issue?
The text was updated successfully, but these errors were encountered: