The 3rd Age

Magic Staff (Isengard-Saruman)

Magic Staff (Isengard-Saruman)

Modified weapon.ini to improve the look and feel of Saruman's standard attack.

Button for The 3rd AgeButton for The Dwarf HoldsButton for The Elven AllianceButton for Helm's Deep Last HopeButton for GothmogtheOrcButton for BFME+Button for The Four AgesButton for HDR HeadquartersButton for Middle Earth CenterButton for Project Perfect Mod

Become an affiliate!


Quick Lists

Top Rated Popular New Updated Last Comments Users

Register and log in to move these advertisements down

Stat Variation

Tutorial for Battle for Middle-earth BFME, Battle for Middle-earth II BFME 2, Battle for Middle-earth II: Rise of the Witch-king ROTWK

Avatar of zimoo


Category: Code
Level: Intermediate
Created: Tuesday May 29, 2007 - 5:21
Updated: Tuesday May 29, 2007 - 7:34
Views: 4424
Summary: A short guide on how to give units variating stats


Staff says


Members say




13 votes

Page 1 2
Ever since I entered the modding community, I have constantly seen mods go for graphical variation. Making units in a horde look different, giving them different weapons etc.

One day I decided to go one step further, and attempt to implement a system whereby units in a horde would all differ from each other in statistics (as well as appearance). At the time I had no working computer, so the idea remained in my head for a while as I thought of ways to implement it.

My first try at implementing involved using the random peasant code. I created almost 400 different orc warrior objects, giving them all different armour/weapon sets. It worked of course, but was way too time consuming for something that I wanted to implement for every single unit.

The second method I came up with is the one I'm still using today. While not perfect, I am very proud of it. It uses LUA (which most people use to variate between equipment), but it's really very simple to understand. It's not difficult at all, it's just that I've used the old LUA methods to create a new system.

In this example I will use the Doriath Warrior object, as seen (hidden) in my own mod, WOTS.

First things first, we need to set up the LUA. In your unit INI (not horde!) search for AIUpdate, and in that behaviour block you will see the following:

AILuaEventsList = DoriathWarriorFunctions

Basically change the 'DoriathWarriorFunctions' to something suitable for your unit. (NOTE: It is important that you don't give this same set of functions to the horde object otherwise all your work will be for naught).

Next open up scriptevents.xml (in datax.big I believe, where x is a random number depending on the game). You need to add your new function to the list. It should look something like this:

<EventList Name="DoriathWarriorFunctions" Inherit="InfantryFunctions">
<EventHandler EventName="OnCreated" ScriptFunctionName="OnDoriathWarriorCreated" DebugSingleStep="false"/>

As you can probably see, this is saying that when the unit is created it runs the script 'OnDoriathWarriorCreated'. Now I'll show you this script:

function OnDoriathWarriorCreated(self)

weapon = GetRandomNumber()
armour = GetRandomNumber()
health = GetRandomNumber()

if weapon <= 0.15 then
ObjectGrantUpgrade( self, "Upgrade_WeaponVariation1" )
elseif weapon <= 0.3 then
ObjectGrantUpgrade( self, "Upgrade_WeaponVariation2" )
elseif weapon <= 0.45 then
ObjectGrantUpgrade( self, "Upgrade_WeaponVariation3" )
elseif weapon <= 0.6 then
ObjectGrantUpgrade( self, "Upgrade_WeaponVariation4" )
elseif weapon <= 0.75 then
ObjectGrantUpgrade( self, "Upgrade_WeaponVariation5" )

if armour <= 0.15 then
ObjectGrantUpgrade( self, "Upgrade_ArmourVariation1" )
elseif armour <= 0.3 then
ObjectGrantUpgrade( self, "Upgrade_ArmourVariation2" )
elseif armour <= 0.45 then
ObjectGrantUpgrade( self, "Upgrade_ArmourVariation3" )
elseif armour <= 0.6 then
ObjectGrantUpgrade( self, "Upgrade_ArmourVariation4" )
elseif armour <= 0.75 then
ObjectGrantUpgrade( self, "Upgrade_ArmourVariation5" )

if health <= 0.15 then
ObjectGrantUpgrade( self, "Upgrade_HealthVariation1" )
elseif health <= 0.3 then
ObjectGrantUpgrade( self, "Upgrade_HealthVariation2" )
elseif health <= 0.45 then
ObjectGrantUpgrade( self, "Upgrade_HealthVariation3" )
elseif health <= 0.6 then
ObjectGrantUpgrade( self, "Upgrade_HealthVariation4" )
elseif health <= 0.75 then
ObjectGrantUpgrade( self, "Upgrade_HealthVariation5" )

If you're not familiar with LUA I'm sure your reaction to that is something along the lines of 'O_o'. Basically what it does is get random numbers for 3 variables (health, armour and weapon). Feel free to add as many (or as few) variables as you like here. It then has a simple IF function, saying that (depending on the random number) a unit should be given an upgrade (or not, which means they'd take the default value).

This system is set up to give 6 possible values for health, armour and the unit's weapon.


zimooIdea and implementation
PhilTips on how to do it, Teaching me LUA


Display order: Newest first

zimoo - Tuesday May 29, 2007 - 9:07

Well what you could do is do the uniquer variations (accuracy, radius damage, poison etc) through use of various weaponstates (I don't know if WEAPONSET_HERO_MODE would work with units, but it'd be worth a try) then do all the 'standard' damage variations through attributemodifiers.

Bart (Administrator) - Tuesday May 29, 2007 - 7:43

Very original. I do wonder, how to do this in BFME 1, where there aren't hundreds of CaH weapon states?

I guess you're limited to just damage variations then :-/

Sulherokhh (Team Chamber Member) - Tuesday May 29, 2007 - 6:35

Great idea! Sweet and neat! That lends itself to those units that have already lua-coded weapon/armor differentiation. I guess this will be useful many times! :)

Go to top


"One site to rule them all, one site to find them,
one site to host them all, and on the network bind them."