Last updated: May 30, 2023 21:40:52
Dependency Injection
Timberborn has very robust Dependency Injection for a game. This means you can access most things in the game very easily, as you can tell Timberborn to automatically give you what you need.
Table of contents
Using configurators
The configurators let you bind classes to the dependency injection system.
// ExampleConfigurator.cs
[Configurator(SceneEntrypoint.InGame)] // This attribute registers the configurator and tells where it should be loaded
public class ExampleConfigurator : IConfigurator {
public void Configure(IContainerDefinition containerDefinition) {
containerDefinition.Bind<Foo>().AsSingleton();
containerDefinition.Bind<Bar>().AsSingleton();
}
}
*SceneEntryPoints: InGame
, MainMenu
, MapEditor
, All
Using your registered/bound code
As long as the class you are trying to use and your class are both bound (with Bind<> and the Configurator
is registered within the same entrypoint), you can then use that service!
// Foo.cs
public class Foo {
private readonly Bar _bar;
public Foo(Bar bar) {
_bar = otherClass;
}
public void Click() {
_bar.Add();
}
}
// Bar.cs
public class Bar {
private int _counter;
public int Add() {
return _counter++;
}
}
Full example
// ExampleConfigurator.cs
[Configurator(SceneEntrypoint.InGame)]
public class ExampleConfigurator : IConfigurator {
public void Configure(IContainerDefinition containerDefinition) {
containerDefinition.Bind<Foo>().AsSingleton();
containerDefinition.Bind<Bar>().AsSingleton();
}
}
// Foo.cs
public class Foo {
private readonly Bar _bar;
public Foo(Bar bar) {
_bar = otherClass;
}
public void Click() {
_bar.Add();
}
}
// Bar.cs
public class Bar {
private int _counter;
public int Add() {
return _counter++;
}
}