{"id":58,"date":"2021-10-24T07:33:24","date_gmt":"2021-10-24T07:33:24","guid":{"rendered":"https:\/\/docs.sisus.co\/inity\/?p=58"},"modified":"2025-07-14T19:38:27","modified_gmt":"2025-07-14T19:38:27","slug":"initargs","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/reference\/initargs\/","title":{"rendered":"InitArgs API"},"content":{"rendered":"<p>The InitArgs class is the bridge through which initialization arguments are passed from the classes that create instances to the objects that are being created (called clients).<\/p>\n<p>Note that in most cases you don\u2019t need to use InitArgs directly; all of the various methods provided for <a href=\"https:\/\/docs.sisus.co\/init-args\/getting-started\/initializing-components-in-code\/\">initializing instances with arguments<\/a> already handle this for you behind the scenes. But for those less common scenarios where you want to write your own code that makes use of the InitArgs you can find details about its inner workings below.<\/p>\n<h2 class=\"western\">InitArgs.Set<\/h2>\n<p>The InitArgs.Set method can be used to store up to twelve initialization arguments for a client of a specific type, which the client can subsequently receive during its initialization.<\/p>\n<p>The client class must implement an <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/iinitializable\/\">IInitializable&lt;T&#8230;&gt;<\/a> or <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/iargs\/\">IArgs&lt;T&#8230;&gt;<\/a> interface with generic argument types matching the types of the parameters being passed in order for it to be targetable by the InitArgs.Set method.<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\">InitArgs<span style=\"color: #808030;\">.<\/span>Set<span style=\"color: #808030;\">&lt;<\/span>TClient<span style=\"color: #808030;\">,<\/span> TArgument<span style=\"color: #808030;\">&gt;(<\/span>argument<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span><\/span><\/pre>\n<h2 class=\"western\">InitArgs.TryGet<\/h2>\n<p>The InitArgs.TryGet method can be used by a client object to retrieve arguments stored in InitArgs during its initialization phase.<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\"><span style=\"color: #800000;\"><b>if<\/b><\/span><span style=\"color: #808030;\">(<\/span>InitArgs<span style=\"color: #808030;\">.<\/span>TryGet<span style=\"color: #808030;\">(<\/span>Context<span style=\"color: #808030;\">.<\/span>Awake<span style=\"color: #808030;\">,<\/span> <span style=\"color: #800000;\"><b>this<\/b><\/span><span style=\"color: #808030;\">,<\/span> <span style=\"color: #800000;\"><b>out<\/b><\/span> TArgument argument<span style=\"color: #808030;\">))<\/span><\/span>\r\n<span style=\"color: #800080;\">{<\/span>\r\n<span style=\"color: #000000;\">    Init<span style=\"color: #808030;\">(<\/span>argument<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span><\/span>\r\n<span style=\"color: #800080;\">}<\/span><\/pre>\n<p>Use the Context argument to specify the method context from which InitArgs is being called, for example Context.Awake when calling during the Awake event or Context.Reset when calling during the Reset event.<\/p>\n<h2 class=\"western\">InitArgs.Clear<\/h2>\n<p>Use InitArgs.Clear to release the arguments stored for a client from memory. This can be done by the class that provided the arguments for the client, after the client has been initialized.<\/p>\n<p>If the stored arguments were never acquired by any client, then InitArgs.Clear will return <strong>true<\/strong>. In this case you can throw an <span style=\"color: #000000;\">InitArgumentsNotReceivedException <\/span>or log an error if you want.<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\"><span style=\"color: #800000;\"><b>if<\/b><\/span><span style=\"color: #808030;\">(<\/span>InitArgs<span style=\"color: #808030;\">.<\/span>Clear<span style=\"color: #808030;\">&lt;<\/span>TClient<span style=\"color: #808030;\">,<\/span> TArgument<span style=\"color: #808030;\">&gt;<\/span><span style=\"color: #808030;\">(<\/span><span style=\"color: #808030;\">))<\/span><\/span>\r\n<span style=\"color: #800080;\">{<\/span>\r\n<span style=\"color: #000000;\">    <span style=\"color: #800000;\"><b>throw new <\/b><\/span>InitArgumentsNotReceivedException(<span style=\"color: #800000;\"><b>typeof<\/b><\/span>(TClient))<span style=\"color: #800080;\">;<\/span><\/span>\r\n<span style=\"color: #800080;\">}<\/span><\/pre>\n<h2 class=\"western\">InitArgs.Received<\/h2>\n<p>If you need to determine separately whether or not initialization arguments stored using <em>InitArgs.Set<\/em> have been acquired by any client yet, you can check this using <em>InitArgs.Received<\/em>.<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\"><span style=\"color: #800000;\"><b>while<\/b><\/span><span style=\"color: #808030;\">(<\/span>!InitArgs<span style=\"color: #808030;\">.<\/span>Received<span style=\"color: #808030;\">&lt;<\/span>TClient<span style=\"color: #808030;\">,<\/span> TArgument<span style=\"color: #808030;\">&gt;<\/span><span style=\"color: #808030;\">(<\/span><span style=\"color: #808030;\">))<\/span><\/span>\r\n<span style=\"color: #800080;\">{<\/span>\r\n<span style=\"color: #000000;\">    <span style=\"color: #800000;\"><b>yield return<\/b><\/span> null<span style=\"color: #800080;\">;<\/span><\/span>\r\n<span style=\"color: #800080;\">}<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The InitArgs class is the bridge through which initialization arguments are passed from the classes that create instances to the objects that are being created (called clients). Note that in most cases you don\u2019t need to use InitArgs directly; all of the various methods provided for initializing instances with arguments already handle this for you ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/initargs\/\" 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\/58"}],"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=58"}],"version-history":[{"count":11,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/58\/revisions"}],"predecessor-version":[{"id":835,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/58\/revisions\/835"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}