Getting a service
In Knight, services can be accessed in a few different ways depending on how your configuration is set up. A key setting that impacts how you fetch services is theCYCLIC_INDEXING_ENABLED flag, which controls whether cyclic dependencies between services are allowed. By default, this setting is enabled.
Knight require
Rich custom class import module.
GetService() has been deprecated and will be removed in a future release.
Accessing Services
Services in Knight can be accessed using either theKnight.GetService method or the custom require(). These methods allow you to retrieve and interact with other services in your game.
However, if you have CYCLIC_INDEXING_ENABLED set to true (the default setting), you can also access other services directly by indexing the self.Services table within a service. This allows services to reference one another cyclically.
Accessing Services with Cyclic Indexing
WhenCYCLIC_INDEXING_ENABLED is true, you can access another service directly from within your current service without worrying about manual importing or memory overhead from excessive service loading. This is useful for cases where services need to call each other’s functions during their execution.
Here’s an example of how you can use cyclic indexing to call a function from another service:
self.Services.OtherServicerefers to another service calledOtherServiceRunning on the same context level (Server or Client), to reference a shared Service/Object useself.Shared.CallFunction()is a method defined within theOtherServiceservice.
How Cyclic Indexing Works
WithCYCLIC_INDEXING_ENABLED set to true, Knight’s internal service loader allows services to reference one another via the Services table. This is beneficial when services are interdependent, such as when one service needs to trigger an event or call a function in another service.
Considerations When Using Cyclic Indexing
While cyclic indexing can be convenient, it’s important to note that in complex projects, heavy reliance on cyclic dependencies can increase the difficulty of managing your services. This is especially true if the dependency chain grows large. To avoid memory issues and potential bugs, Knight introducedGetService as an alternative.
Disabling Cyclic Indexing
If you prefer to avoid cyclic dependencies altogether, you can disable cyclic indexing by settingCYCLIC_INDEXING_ENABLED to false in your KNIGHT_CONFIG.lua file. When cyclic indexing is disabled, services must be explicitly retrieved using either Knight.Import or Knight.GetService, and attempts to access services via cyclic indexing will fail.
Here’s how you disable cyclic indexing:
Best Practices
- Use Cyclic Indexing Sparingly: While convenient, cyclic indexing can lead to tightly coupled services. For better maintainability, consider using
GetServiceorImportto explicitly manage dependencies. - Optimize for Memory Usage: In large games with many services, consider disabling cyclic indexing and using
GetServicefor a more memory-efficient approach. - Keep Dependencies Clear: Regardless of whether cyclic indexing is enabled or disabled, ensure that your services have well-defined responsibilities to avoid confusion and unintended dependencies.
