Hi all,
A few weeks ago I added support for nameless inheritance, aka same section inheritance.
I thought I'd say a few words on it today as it's more than just a typing shortcut for lazy programmers. ;-)
First, let's review the two kind of per-key inheritances with which you are probably already familiar:
[Template]
MyKey = MyValue
[Object]
MyKey = @Template ; Implicitly inheriting from Template.MyKey (the key name being the implicit part)
MyOtherKey = @Template.MyKey ; Explicitly inheriting from Template.MyKey
Now the new so called nameless inheritance works this way:
[Object]
MyLastKey = @.MyKey ; Here we inherit from Object.MyKey, the section name being implicit
But there's more to it than meet the eyes. It works in a similar fashion than using '@' alone.
As a reminder, having:
[Template]
MyKey = @ ; Here the value will be the section, Template
[Object@Template] ; Here we inherit from the whole section Template
Querying Object.MyKey will no yield Template but Object as the use of '@' to reference the section name is dynamic and will always reflect the current section being used.
Well it works the same way with the new nameless inheritance:
[Template]
MyKey = MyValue
MyOtherKey = @.MyKey
[Object@Template]
MyKey = MyNewValue
Querying Object.MyOtherKey will not yield MyValue but MyNewValue.
Here's the whole process chain: Object.MyOtherKey -> Template.MyOtherKey -> <nameless>.MyKey -> Object.MyKey -> MyNewValue
Ok, cool, but now what are we going to use this for? Well, it's actually pretty handy for any global selection scheme triggered by a single trigger.
Let's take an example. Let's say in my game all my NPCs have a light and a dark version. Depending if the player is in the light world or in the dark world, they should be spawned with the appropriate attribute.
Let's say, for simplicity sake, that we only want to change their Color. We could, of course, do this in code, checking if we're in a light world then select LightColor and, reciprocally, if we're in a dark world we select DarkColor. We would then need more code to apply this to our created object.
Well, fear not as there's almost no code at all needed if we're using a simple scheme based on nameless inheritance. (Actually one single command would be enough, no need even for any code!)
We simply need to organize our NPCs this way:
; A base class that contains a switch with nameless inheritance
[NPCBase]
Color = @.LightColor ; Using LightColor by default
[NPC1@NPCBase]
LightColor = ... ; Defining light color for NPC1
DarkColor = ... ; Defining dark color for NPC2
For any NPC, we would declare both LightColor and DarkColor but not the Color attribute itself. This one is inherited from NPCBase.
What happens when we create a NPC1, its color query currently would be:
NPC1.Color -> NPCBase.Color -> <nameless>.LightColor -> NPC1.LightColor
No matter how many sub-NPC classes we have, we can switch all their templates at once from light to dark by executing this:
orxConfig_PushSection("NPCBase");
orxConfig_SetString("Color", "@.DarkColor");
orxConfig_PopSection();
Now any NPC created will be using its own DarkColor value. Simple as that.
Of course this can be used for more complex switches and even switch between some data-stored logic with commands/timelines.
As always, don't hesitate if you have any questions and/or comments! =)
Cheers,
Rom