VContainer how to's #498
Unanswered
SimonNordon4
asked this question in
Q&A
Replies: 1 comment
-
I did something silly and made my own ContainerBuilderExtension: RegisterSystem is similar to RegisterEntryPoint, except register system is tied to the Scope's GameObject. That means if the gameobject with the scope on it is disabled, so are all of it's systems. I also implemented IEnablable and IDisablable. This is so I can pool objects and actually disable Tick() without disposing. I could have also totally done this manually, but I like make it syntactically similar. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey guys, I thought it would be a good idea to open up a discussion for tips and tricks. The current documentation is extremely rich in its depth, but it would great to have a few more "Hello World" examples to help people get started.
I'll use language familiar to a Unity Developer and follow it with the DI explanation.
Spawn a GameObject prefab
Register a factory that returns an instance instantiated by the current scopes container
Create a prefab spawner.
Spawn a GameObject prefab with parameters.
As an example, let's imagine you wanted to spawn a bullet, and set it's velocity when you created it.
There's a few issues with this code. We're assuming the prefab gameobject has a bullet component, which it may not. The other issue this is registering as 'Func<Vector3,GameObject> which means we can't create any new factories using this formatting. Instead we will instantiate the bullet directly.
Spawn a MonoBehaviour prefab with parameters.
we spawn the bullet like this
Spawn a MonoBehaviour prefab using a Factory Class
If our prefab logic start's getting too large, it's not a good idea to define inside the Scope, but instead create a custom class to handle the logic for us.
Register our new BulletFactory class
and finally use it in our spawner.
Note that in all examples, the prefabs dependencies will be injected when instantiated.
If this was helpful to anyone I can show how to do Gameobject Pools as well.
Spawn a Prefab with it's own LifetimeScope
If you try to spawn a prefab that has it's own scope, you might notice you're getting null reference errors. We need to take a slightly different approach if our object has it's own LifetimeScope.
In this example, we'll look at spawning an enemy with it's own LifetimeScope.
In this example we had to use "CreateChildFromPrefab()" instead of the instantiate method.
Spawn a Prefab with it's own LifetimeScope and pass a parameter into the scope.
Maybe we want to the spawned prefab scope to automatically register an instance. For now the best solution seems to set a public field on your prefab scope before building it.
The prefab scope would look something like this:
I'm aware there's a way to register new services in children that are dynamically spawned, however I found in the context of prefabs this to be a little more straight forward.
It also allows use to set an EnemyTarget manually in the editor for testing purposes.
Createing a Gameobject Pool
// TODO:
This one I haven't figured out yet
Beta Was this translation helpful? Give feedback.
All reactions