diff --git a/src/include/unittype.h b/src/include/unittype.h index 178551ce7..a2eea11d5 100644 --- a/src/include/unittype.h +++ b/src/include/unittype.h @@ -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) diff --git a/src/unit/script_unittype.cpp b/src/unit/script_unittype.cpp index fa839ece0..6dbc4b465 100644 --- a/src/unit/script_unittype.cpp +++ b/src/unit/script_unittype.cpp @@ -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")) { diff --git a/src/unit/unit.cpp b/src/unit/unit.cpp index 56d5f42f4..e3ccdb745 100644 --- a/src/unit/unit.cpp +++ b/src/unit/unit.cpp @@ -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) { diff --git a/src/unit/unittype.cpp b/src/unit/unittype.cpp index e15fa6653..94d800a14 100644 --- a/src/unit/unittype.cpp +++ b/src/unit/unittype.cpp @@ -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();