[+] Added OnInit lua callback for CUnitType to initialize unit variables after creation

This commit is contained in:
cybermind 2013-05-04 19:45:27 +06:00
parent e82dd1e975
commit 2901ead35c
4 changed files with 11 additions and 1 deletions

View file

@ -467,6 +467,7 @@ public:
LuaCallback *OnHit; /// lua function called when unit is hit
LuaCallback *OnEachCycle; /// lua function called every cycle
LuaCallback *OnEachSecond; /// lua function called every second
LuaCallback *OnInit; /// lua function called on unit init
mutable std::string DamageType; /// DamageType (used for extra death animations and impacts)

View file

@ -559,6 +559,8 @@ static int CclDefineUnitType(lua_State *l)
type->OnEachCycle = new LuaCallback(l, -1);
} else if (!strcmp(value, "OnEachSecond")) {
type->OnEachSecond = new LuaCallback(l, -1);
} else if (!strcmp(value, "OnInit")) {
type->OnInit = new LuaCallback(l, -1);
} else if (!strcmp(value, "Type")) {
value = LuaToString(l, -1);
if (!strcmp(value, "land")) {

View file

@ -738,6 +738,12 @@ CUnit *MakeUnit(const CUnitType &type, CPlayer *player)
unit->AssignToPlayer(*player);
}
if (unit->Type->OnInit) {
unit->Type->OnInit->pushPreamble();
unit->Type->OnInit->pushInteger(UnitNumber(*unit));
unit->Type->OnInit->run();
}
// fancy buildings: mirror buildings (but shadows not correct)
if (type.Building && FancyBuildings
&& unit->Type->NoRandomPlacing == false && (MyRand() & 1) != 0) {

View file

@ -611,7 +611,7 @@ CUnitType::CUnitType() :
Slot(0), Width(0), Height(0), OffsetX(0), OffsetY(0), DrawLevel(0),
ShadowWidth(0), ShadowHeight(0), ShadowOffsetX(0), ShadowOffsetY(0),
Animations(NULL), StillFrame(0),
DeathExplosion(NULL), OnHit(NULL), OnEachCycle(NULL), OnEachSecond(NULL),
DeathExplosion(NULL), OnHit(NULL), OnEachCycle(NULL), OnEachSecond(NULL), OnInit(NULL),
CorpseType(NULL), Construction(NULL), RepairHP(0), TileWidth(0), TileHeight(0),
BoxWidth(0), BoxHeight(0), BoxOffsetX(0), BoxOffsetY(0), NumDirections(0),
MinAttackRange(0), ReactRangeComputer(0), ReactRangePerson(0), Priority(0),
@ -649,6 +649,7 @@ CUnitType::~CUnitType()
delete OnHit;
delete OnEachCycle;
delete OnEachSecond;
delete OnInit;
BoolFlag.clear();