D
D
DOTSNET
Search…
Dependency Injection
DOTSNET comes with dependency injection for all systems in the server/client worlds.
Dependency injection is a comfort feature that you don’t need to use, but you probably will because it makes your life a lot easier.
Often times, a system might need to use another system:
1
using DOTSNET;
2
3
[ServerWorld]
4
public class TestSystem : ComponentSystem
5
{
6
void DoSomething()
7
{
8
// send a message
9
GetExistingSystem<NetworkServerSystem>().Send(...);
10
}
11
12
void DoSomethingElse()
13
{
14
// send a message
15
GetExistingSystem<NetworkServerSystem>().Send(...);
16
}
17
}
Copied!
For performance and ease of use, it makes sense to cache the system once in OnCreate:
1
using DOTSNET;
2
3
[ServerWorld]
4
public class TestSystem : ComponentSystem
5
{
6
protected NetworkServerSystem server;
7
8
protected override void OnCreate()
9
{
10
server = GetExistingSystem<NetworkServerSystem>().;
11
}
12
13
void DoSomething()
14
{
15
// send a message
16
server.Send(...);
17
}
18
19
void DoSomethingElse()
20
{
21
// send a message
22
server.Send(...);
23
}
24
}
Copied!
Caching all required systems in OnStartRunning can get cumbersome. This is where Dependency Injection comes in. Simply use the [AutoAssign] attribute:
1
using DOTSNET;
2
3
[ServerWorld]
4
public class TestSystem : ComponentSystem
5
{
6
[AutoAssign] protected NetworkServerSystem server;
7
8
void DoSomething()
9
{
10
// send a message
11
server.Send(...);
12
}
13
14
void DoSomethingElse()
15
{
16
// send a message
17
server.Send(...);
18
}
19
}
Copied!
DOTSNET simply initializes all [AutoAssign] systems by calling World.GetExistingSystem at startup.
    A system in the DefaultWorld can use [AutoAssign] for any other system in the Default world.
    A system in the ClientWorld can use [AutoAssign] for any other system in the Client world.
    A system in the ServerWorld can use [AutoAssign] for any other system in the Server world.
Last modified 5mo ago
Copy link