Page 1 of 1

Templates

Posted: Sat Feb 17, 2018 2:31
by Lobby
A common issue in writing plugins is code duplication as you use the same code over and over again to define similar things. A solution to this problem are templates.

Let's say your code (simplified) looks something like this :json

Code: Select all

[
  {
    "id":"$myid00",
    "type":"something",
    "author":"anyone",
    "A":1,
    "B":2,
    "C":[1,2,3,4]
  },
  {
    "id":"$myid01",
    "type":"something",
    "author":"anyone",
    "A":1,
    "B":2,
    "C":[1,2,3,4],
    "D":"Test"  // Additional attribute
  },
  {
    "id":"$myid02",
    "type":"something",
    "author":"anyone",
    "A":8,  // Different value for A
    "B":2,
    "C":[1,2,3,4]
  }
]
As you might notice there's some duplication here. Templates can help to extract common definitions into a single object. Here that would look like :json

Code: Select all

[
  {
    "id":"$mytemplate00",
    "type":"template",
    "author":"anyone",
    "A":1,
    "B":2,
    "C":[1,2,3,4]
   },//<<--was missing
  {
    "id":"$myid00",
    "type":"something",
    "template":"$mytemplate00"  // Reference to template, no author, A, B, C needed here anymore
  },
  {
    "id":"$myid01",
    "type":"something",
    "template":"$mytemplate00"  // Reference to template
    "D":"Test"  // Additional attribute can still be applied
  },
  {
    "id":"$myid02",
    "type":"something",
    "template":"$mytemplate00"  // Reference to template
    "A":8  // Override value of A
  }
]
So templates are of type "template"

Code: Select all

"type":"template"
and can predefine anything that can be defined in plugin objects (except id and type for obvious reasons). To use a template just add

Code: Select all

"template":"$templateid"
to your using objects. As usual, order matters. So in order to use a template you have to ensure that it is already defined. You can overwrite definitions provided by a template by just redefining it. Templates can reference to other templates. Any object can only reference to one template at max.

For convenience it can be useful to inherit from multiple templates, you can do so by providing an array of template ids:

Code: Select all

"template":["$templateid0", "$templateid1", ...]

Re: Templates

Posted: Sat Feb 17, 2018 3:49
by cesareborgia94
Hello [mention]Lobby[/mention],

I have difficulty understanding in how templates can be used in plugins. The only thing I understand is that templates can simplify code duplication. The only thing I do not understand is when to use templates in writing codes, or how to apply templates in plugin making?

Can you show an example in graphics, so that we could understand the uses of templates in plugin creation?

Re: Templates

Posted: Sat Feb 17, 2018 20:21
by Lobby
I added it to make bus code easier to read. Here's how the code for a single bus would look like without templates :json

Code: Select all

{
  "id":"$bus00",
  "type":"car",
  "frames":[{"x":0,"y":0,"w":10,"h":8,"count":4,"offset x":1024,"offset y":3328}],
  "overlay frames":[{"x":0,"y":0,"w":10,"h":8,"count":4,"offset x":1112,"offset y":3328}],
  "v2":[4,1,3,-1],
  "auto colorize":true,            // These attributes will be needed by any bus
  "flag normal":false,             //
  "flag bus":true,                 //
  "meta":{"tags":{"idle bus":{}}}  //
}
This code wouldn't be an issue if you have just a few busses, but here 9 different bus types are needed, so templates were introduced. The template :json

Code: Select all

{
  "id":"$template_bus00",
  "type":"template",
  "auto colorize":true,
  "flag normal":false,
  "flag bus":true,
  "meta":{"tags":{"idle bus":{}}}
},
The code that's now needed for each bus :json

Code: Select all

{
  "id":"$bus00",
  "type":"car",
  "template":"$template_bus00",
  "frames":[{"x":0,"y":0,"w":10,"h":8,"count":4,"offset x":1024,"offset y":3328}],
  "overlay frames":[{"x":0,"y":0,"w":10,"h":8,"count":4,"offset x":1112,"offset y":3328}],
  "v2":[4,1,3,-1]
},
I don't have an example for graphics, but let's assume you need the same frames for multiple buildings, you could then just define this in a template.

Re: Templates

Posted: Sat Feb 17, 2018 20:30
by CommanderABab
May the template include a reference to a predefined animation?

Re: Templates

Posted: Sat Feb 17, 2018 21:06
by Lobby
Sure, templates don't really know their content, it will just be copy'n'pasted into plugins that reference to the template.