[0.8.9][NK] Mixins

johnycilohoklajohnycilohokla REGISTERED Posts: 283 Developer
edited December 2015 in Modding Tutorials
Mixins can be treated as templates or addons that can be applied to a class or instance.
When a mixin is applied, all of the functions from the mixin are applied (injected) to the target class or instance of a class.

Example (Creating Mixin):
MyMixin = EternusEngine.Mixin.Subclass("MyMixin")

Static Mixin

Static Mixins are called on the class, only functions are copied to the target class.
Static Mixins are not constructed (Mixin:Constructor will not be called).

Example:
MyClass = EternusEngine.Class.Subclass("MyClass")
MyClass.StaticMixin(MyMixin)

Constructor Mixin

Constructor Mixin are called on an instance of a class, all functions are copied to the target instance and then the Constructor function of the Mixin is called.

Example:
MyClass = EternusEngine.Class.Subclass("MyClass")
function MyClass:Constructor( args )
self:Mixin(MyMixin, args)
end

Overriding functions (__mixinoverrides)

The __mixinoverrides option allows mixins to override function instead of chaining.

This is useful when creating a Subclass of a Mixin when you want to override one of the super functions.
MyMixin = EternusEngine.Mixin.Subclass("MyMixin")

-------------------------------------------------------------------------------
function MyMixin:MyFunction( args )
return false
end
AnotherMixin = MyMixin.Subclass("AnotherMixin")

AnotherMixin.__mixinoverrides =
{
'MyFunction'
}

-------------------------------------------------------------------------------
function AnotherMixin.MyFunction( args )
return true
end

Defining default functions (__nochain)

To define a default function you can use the __nochain option, this will make sure the mixin doesn't override or chain a function when it's already defined, it will only be added to the class when the function doesn't exist.

This option is only useful when using classInstance:Mixin(MyMixin, args) and when the super class can implement the function you don't want to chain.

Example:
MyMixin = EternusEngine.Mixin.Subclass("MyMixin")

MyMixin.__nochain =
{
'MyFunction'
}

-------------------------------------------------------------------------------
function MyMixin:MyFunction( args )
return false
end
My Blog
jc.png
  yt.png  t.png  

Comments

  • RawrRawr REGISTERED, Tester Posts: 510 Seed
    Ok now I'm seeing where these things start to fit in.
    Johny, many thanks for this tutorial.
    Programmer, designer, artist.
Sign In or Register to comment.