For those interested; I've experimented a little bit with
aspect oriented programming-like techniques when making a unit script.
For example, in S44 there are some units that are normally stealthed, but become radar visible for a short duration after firing. Excluding this part the script for such unit may be nearly identical to that of other units, and there may also be units with a totally different script that still have exactly this behavior.
In either case, a traditional implementation would definitely result in code duplication, or if-then hell.
So I created a small function 'aop_include' which includes a file, and joins all functions in script table to functions already existing in script table. (That corresponds to following JPM: join point is function execution, pointcut includes only functions in script table.)
If you don't know about AOP, you can view what it does simply as prepending the statements of the script functions in the included file to the functions that already exist at the point the include is executed.
In the end this resulted in a file for the stealth-unless-firing that looks like this (a bit simplified), and can be added with a single line 'aop_include "stealth.lua"' to any other unit script, while keeping the behavior of already defined Create/FireWeapon1 functions.
Code: Select all
local VISIBLE_PERIOD = VISIBLE_PERIOD or 5000
function script.Create()
SetUnitValue(COB.STEALTH, 1)
end
local function Stealth()
SetUnitValue(COB.STEALTH, 0)
Sleep(VISIBLE_PERIOD)
SetUnitValue(COB.STEALTH, 1)
end
function script.FireWeapon1()
StartThread(Stealth)
end
instead of some ugly 'if VISIBLE_PERIOD then ... end' changes at the respective places in the generic tank script I made.
Note that I have no plans (yet) to include this by default in the unit script framework. It's just a nice example of what you can do with Lua unit scripts, and may give others ideas
