Comments (5)
That kind of scenario is something that DI can help a lot!
Considering that "context" in Adic could be translated to a container (an instance of InjectionContainer, created inside the SetupContainers() method of a GameRoot) you could have a specific container for each of your ships. However, I don't think this would be the best approach.
You could place specific configurations of your ships in the same container using different conditional identifiers (see Conditions) and also bind any common configurations or objects that are shared among all ships in that same container. So, in your modules (MonoBehaviours or not), you inject the configurations using these identifiers.
I don't know exactly what happen when the ship is docked (whether e.g. the "mothership" will then be controlled by the player). But, if that's the case, or even to ensure some communication between the ship and the (for the lack of a better word) "mothership", you could dispatch a command (see Using commands) to pass the control to the "mothership" by configuring the necessary modules (and also to dock the ship).
I hope that can help!
from adic.
Thanks for the info! I missed the parentInstance field of the context in the conditions section when reading earlier. Using that instance I could do something like:
//Players created before container setup
foreach (var player in players)
{
int playerID = player.ID;
PlayerInput input = player.Input;
container.Bind<PlayerInput>().ToGameObject(input).When(c =>
((MonoBehaviour)c.parentInstance)
.transform
.root
.GetComponent<Ship>()
.Player
.ID == playerID
);
}
Which would inject the input object for all modules of a ship.
I also guess it would work for players joining mid-game if I just create all players (in the above foreach) beforehand - since it is not possible to update the container after the initial SetupContainers()?
Is it possible to redo injection at a later stage (long after object creation)? Say I create a ship module at start and later on it changes owner, could I redo the injection to get a new player input instance? Perhaps by doing this.Inject()?
from adic.
That should do the trick if new instances of the Player
component have the correct IDs (and the player
list contains the unique inputs for each player), and should also work when a new player joins mid-game.
It's a complex binding, but looks like an acceptable approach that Adic can handle just fine.
However, I don't feel this approach is the best for your problem. Why not use a factory to decide which input object to use based on the same conditions (a factory receives the same InjectionContext
object as the condition)? The main use of a factory is to customize the way an object is created.
Analyzing your code, looks like the Input is a MonoBehaviour. Have you thought about making it a regular class? This way it doesn't need to be attached to any game object and new instances could be created directly by the container itself.
Obviously I don't know how your code is organized, so I can't really say that it could be a proper solution.
And, although not recommended, you could add bindings to a container after SetupContainers()
by either holding a reference to it or by injecting the container through an IInjectionContainer
constructor parameter or class field - but that not necessarily will help you on solving the input problem described above.
from adic.
To make the use of factories more pleasurable, I opened the issue #4 to allow them to be created by the container, so they can have any dependencies resolved (currently factories don't support injection, so you could not e.g. receive the container as a dependency to resolve other types when manually creating an object).
from adic.
Thanks, very good suggestions! #4 seems like a smart way of handling this, I'll try the factory approach.
from adic.
Related Issues (20)
- Improve chaining on containers
- Duplicated singleton instances HOT 1
- Add Init event to containers
- Add attribute to automatically get components
- Inject on private members
- Bindings Setup through Inspector
- Replace Resorces folder examples with Asset Bundles HOT 1
- Add a ToPrefab implementation using GameObject
- The type `Adic.Cache.ParameterInfo' does not contain a constructor that takes `2' arguments
- Binding to a UnityUI object caused RectTransform to bind too HOT 1
- Getting lots of obselete warnings when using ToGameObject HOT 1
- Update examples for scene wide injection HOT 1
- Binding to an instance still attempts to resolve instance dependencies HOT 3
- Coroutines for regular classes HOT 1
- Ensure hierarchy order when using children injection
- Problem with injecting from multiple containers HOT 1
- Why does this.Inject() work in Start() method, but not in Update() method? HOT 1
- DynamicMethod and ILGenerator aren't availiable on Unity 2020+ HOT 1
- [Bug] Container auto inject when call BindToGameObject
- Missed the assembly reference by default.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from adic.