Skip to content

Commit

Permalink
Monster.getId in onSpawn event (otland#4557)
Browse files Browse the repository at this point in the history
  • Loading branch information
MillhioreBT committed Nov 3, 2023
1 parent 16a985c commit 6e3e689
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ class Tile;

enum class EventInfoId
{
CREATURE_ONHEAR
// Creature
CREATURE_ONHEAR,

// Monster
MONSTER_ONSPAWN
};

class Events
Expand Down Expand Up @@ -133,6 +137,8 @@ class Events
switch (eventInfoId) {
case EventInfoId::CREATURE_ONHEAR:
return info.creatureOnHear;
case EventInfoId::MONSTER_ONSPAWN:
return info.monsterOnSpawn;
default:
return -1;
}
Expand Down
18 changes: 18 additions & 0 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2764,6 +2764,7 @@ void LuaScriptInterface::registerFunctions()

registerMethod("Monster", "isMonster", LuaScriptInterface::luaMonsterIsMonster);

registerMethod("Monster", "getId", LuaScriptInterface::luaMonsterGetId);
registerMethod("Monster", "getType", LuaScriptInterface::luaMonsterGetType);

registerMethod("Monster", "rename", LuaScriptInterface::luaMonsterRename);
Expand Down Expand Up @@ -11041,6 +11042,23 @@ int LuaScriptInterface::luaMonsterIsMonster(lua_State* L)
return 1;
}

int LuaScriptInterface::luaMonsterGetId(lua_State* L)
{
// monster:getId()
Monster* monster = getUserdata<Monster>(L, 1);
if (monster) {
// Set monster id if it's not set yet (only for onSpawn event)
if (getScriptEnv()->getScriptId() == g_events->getScriptId(EventInfoId::MONSTER_ONSPAWN)) {
monster->setID();
}

lua_pushinteger(L, monster->getID());
} else {
lua_pushnil(L);
}
return 1;
}

int LuaScriptInterface::luaMonsterGetType(lua_State* L)
{
// monster:getType()
Expand Down
1 change: 1 addition & 0 deletions src/luascript.h
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,7 @@ class LuaScriptInterface

static int luaMonsterIsMonster(lua_State* L);

static int luaMonsterGetId(lua_State* L);
static int luaMonsterGetType(lua_State* L);

static int luaMonsterRename(lua_State* L);
Expand Down

0 comments on commit 6e3e689

Please sign in to comment.