The Regnus Scripting Tutorial
Part 12: Aliases
Aliases are perhaps the most complex, but also one of the most powerful features of the Regnus language.
Put simply, an alias is a type of double-expression reference which allows you to return not the result of expression itself, but a pre-defined alternative to that result, allowing you to provide alternative selections of text based on previously generated content.
That may sound a little complicated, but hopefully, you'll quickly understand once you've played with a few examples!
There are two elements to an alias: firstly, there is the alias reference type, which is invoked using the at character ("@"), and secondly, there is the "ALIAS" qualifier, which is used to define the replacement text. So, to illustrate, imagine the following situation:
START <Example>My pet ##Animal; is eating a ##Food;!
GROUP Animal
ENTRY cat
ENTRY bird
GROUP Food
ENTRY mouse
ENTRY worm
Here, either type of animal can be eating either type of prey, which might not be desirable. It may be that you wish to randomise the pet involved, but choose the result of the second reference based on which animal is selected for the first.
This is exactly what aliases are for, and here's how you would use them to solve this problem:
START <Alias Example>My pet #>(0):Animal; is eating a #@[0]:(1);!
GROUP Animal
ENTRY cat
ENTRY bird
ALIAS cat:mouse
ALIAS bird:worm
Here, the first reference stores the result of the expression "Animal" to slot number 0. The second reference uses the alias RTC to return an alias for the selected animal. So, how does this work?
The alias reference expects two expressions. The first is the text for which you wish to return an alias, in this case the contents of slot number 0 (to which we saved the result of our first reference).
The second expression is the number of the alias of that text you wish to return, which we will cover shortly...
Each "ALIAS" qualifier defines firstly some text to replace, and secondly the alias text with which to replace it. In this case, the two aliases define that "cat" should be replaced with "mouse" and "bird" should be replaced with "worm".
This means that if the content of slot 0 is "cat", the second reference in the example will return "mouse", and if the content is "bird", it will return "worm".
So, what about that second expression in the alias reference? Well, this is in case you wish to use several aliases for the same text. In this case, you can simply add more "ALIAS" qualifiers, and specify the number (the first to appear in the script being "1", the second being "2" and so on) of the alias you wish to return.
So, to give a practical example:
START <Alias Example>My pet #>(0):Animal; ate a #@[0]:(1); and then #@[0]:(2);!
GROUP Animal
ENTRY cat
ENTRY bird
ALIAS cat:mouse
ALIAS bird:worm
ALIAS cat:miaowed
ALIAS bird:chirped
Here, the third reference asks for the second alias of the animal selected, which contains the noise associated with that animal. Hopefully, that's fairly straight forward!
Anyway, there is another important aspect to aliases: the alias text is itself parsed, and so can include further references! So, to enlarge on our example script:
START <Alias Example>My pet #>(0):Animal; ate a #@[0]:(1); and then #@[0]:(2);!
GROUP Animal
ENTRY cat
ENTRY bird
ALIAS cat:##CatFood;
ALIAS bird:##BirdFood;
ALIAS cat:##CatAction;
ALIAS bird:##BirdAction;
GROUP CatAction
ENTRY miaowed
ENTRY purred
GROUP CatFood
ENTRY mouse
ENTRY gerbil
GROUP BirdAction
ENTRY chirped
ENTRY flew away
GROUP BirdFood
ENTRY worm
ENTRY pile of seed
Also, remember that you can use the "REFER" qualifer to name numeric values, for instance:
REFER 1:Action
REFER 2:Food
START <Alias Example>My pet #>(0):Animal; ate a #@[0]:(Food); and then #@[0]:(Action);!
GROUP Animal
ENTRY cat
ENTRY bird
ALIAS cat:##CatAction;
ALIAS bird:##BirdAction;
ALIAS cat:##CatFood;
ALIAS bird:##BirdFood;
GROUP CatAction
ENTRY miaowed
ENTRY purred
GROUP CatFood
ENTRY mouse
ENTRY gerbil
GROUP BirdAction
ENTRY chirped
ENTRY flew away
GROUP BirdFood
ENTRY worm
ENTRY pile of seed
Hopefully, once you've played with that script a little, you'll be fully comfortable with using aliases, and ready to move on to the next chapter!