{"id":751,"date":"2025-06-06T17:13:43","date_gmt":"2025-06-06T17:13:43","guid":{"rendered":"https:\/\/docs.sisus.co\/init-args\/?p=751"},"modified":"2025-07-14T19:27:48","modified_gmt":"2025-07-14T19:27:48","slug":"service-api","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/reference\/service-api\/","title":{"rendered":"Service API"},"content":{"rendered":"<p>The Service class contains methods that can be used to manually register service objects and retrieve them for clients.<\/p>\n<p>This is lower level API. You typically don&#8217;t need to use it directly, but should instead use the <a href=\"https:\/\/docs.sisus.co\/init-args\/services\/service-attribute\/\">[Service] attribute<\/a>, <a href=\"https:\/\/docs.sisus.co\/init-args\/services\/service-tag\/\">Service Tag<\/a> and <a href=\"https:\/\/docs.sisus.co\/init-args\/services\/services-component\/\">Services component<\/a> to register services, and create a class that derives from <a href=\"https:\/\/docs.sisus.co\/init-args\/clients\/monobehaviour-t\/\">MonoBehaviour&lt;T&#8230;&gt;<\/a> to retrieve them automatically.<\/p>\n<h2 class=\"western\">Service.Set<\/h2>\n<p>The <a href=\"https:\/\/docs.sisus.co\/init-args-reference\/class_sisus_1_1_init_1_1_service_afa86ad1d7582e8285718d1cfcc2cf061.html\">Service.Set<\/a> method can be used to register an object as a global service.<\/p>\n<p>The generic type argument of the method determines the <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/service-defining-type\/\">defining type<\/a> for the service, which clients can use to retrieve the instance.<\/p>\n<pre class=\"western\">Service.Set&lt;IPlayer&gt;(player);<\/pre>\n<h2 class=\"western\">Service.Unset<\/h2>\n<p>The Service.Unset method can be used to register a global service.<\/p>\n<p>The generic type argument of the method specifies the <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/service-defining-type\/\">defining type of the service<\/a> to unset.<\/p>\n<pre class=\"western\">Service.Unset&lt;IPlayer&gt;();<\/pre>\n<h2 class=\"western\">Service.AddFor<\/h2>\n<p>The Service.Addfor method can be used to register an object as a local service.<\/p>\n<p>The generic type argument of the method determines the <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/service-defining-type\/\">defining type<\/a> for the service, which clients can use to retrieve the instance.<\/p>\n<p>The clients argument specifies which clients have access to the service, based on their location in the scene hierarchies.<\/p>\n<p>The last argument determines the origin point of the service, which is used when determining whether or not a particular client has access to the service in question.<\/p>\n<pre class=\"western\">Service.AddFor&lt;IPlayer&gt;(Clients.InChildren, player, player.transform);<\/pre>\n<h2 class=\"western\">Service.RemoveFrom<\/h2>\n<p>The Service.RemoveFrom method can be used to unregister a local service.<\/p>\n<p>The generic type argument of the method specifies the <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/service-defining-type\/\">defining type of the service<\/a> to unset.<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\">Service.RemoveFrom&lt;IPlayer&gt;(Clients.InChildren, player, player.transform);\r\n<\/span><\/pre>\n<h2 class=\"western\">Service.TryGet<\/h2>\n<p>The Service.TryGet method can be used by a client object to try and retrieve a global service previously registered using Service.Set or Service.AddFor with a clients value of Everywhere.<\/p>\n<pre class=\"western\">if(Service.TryGet(out Player player))\r\n{\r\n    Init(player);\r\n}<\/pre>\n<h2 class=\"western\">Service.TryGetFor<\/h2>\n<p>The Service.TryGetFor method can be used by a client object to try and retrieve a local or global service previously registered using Service.Set or Service.AddFor.<\/p>\n<pre class=\"western\">if(Service.TryGetFor(this, out Player player))\r\n{\r\n    Init(player);\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The Service class contains methods that can be used to manually register service objects and retrieve them for clients. This is lower level API. You typically don&#8217;t need to use it directly, but should instead use the [Service] attribute, Service Tag and Services component to register services, and create a class that derives from MonoBehaviour&lt;T&#8230;&gt; ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/service-api\/\" title=\"read more\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/751"}],"collection":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/comments?post=751"}],"version-history":[{"count":4,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/751\/revisions"}],"predecessor-version":[{"id":782,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/751\/revisions\/782"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}