03. InitArgs

  03. Features No Comments

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’t need to use InitArgs directly; all of the various methods Inity provides for initializing instances with arguments 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 documentation explaining its inner workings below.

InitArgs.Set

The InitArgs.Set method can be used to store one to five arguments for a client of a specific type, which the client can subsequently receive during its initialization.

The client class must implement an IArgs<T…> 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.

InitArgs.Set<TClient, Targument>(argument);

The generic TClient type does not have to be the exact type of the client but can also be its base class.

InitArgs.TryGet

The InitArgs.TryGet method can be used by a client object to retrieve arguments stored in InitArgs during its initialization phase.

if(InitArgs.TryGet(Context.Awake, this, out TArgument argument))
{
    Init(argument);
}

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.

InitArgs.Clear

Use InitArgs.Clear to remove arguments cached in InitArgs. This should usually be called by the class responsible for providing the arguments to the client after initialization.

InitArgs.Received

Use this method to determine whether or not Init arguments provided for a client have been received yet or not.

This can be useful for throwing an exception if the client object has failed to receive the arguments during its initialization phase.

Leave a Reply

Your email address will not be published. Required fields are marked *