Skip to content

Commit

Permalink
Add Lua interface to execute timed GlobalEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
ranisalt committed Jun 28, 2016
1 parent 2e76dd2 commit 76396f2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion data/talkactions/scripts/force_raid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function onSay(player, words, param)
return false
end

local raid = Raid(param)
local raid = GlobalEvent(param)
if raid then
raid:execute()
else
Expand Down
30 changes: 30 additions & 0 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2533,6 +2533,12 @@ void LuaScriptInterface::registerFunctions()
registerMethod("Party", "isSharedExperienceEnabled", LuaScriptInterface::luaPartyIsSharedExperienceEnabled);
registerMethod("Party", "shareExperience", LuaScriptInterface::luaPartyShareExperience);
registerMethod("Party", "setSharedExperience", LuaScriptInterface::luaPartySetSharedExperience);

// GlobalEvent
registerClass("GlobalEvent", "", LuaScriptInterface::luaGlobalEventCreate);
registerMetaMethod("GlobalEvent", "__eq", LuaScriptInterface::luaUserdataCompare);

registerMethod("GlobalEvent", "execute", LuaScriptInterface::luaGlobalEventExecute);
}

#undef registerEnum
Expand Down Expand Up @@ -12046,6 +12052,30 @@ int LuaScriptInterface::luaPartySetSharedExperience(lua_State* L)
return 1;
}

// GlobalEvent
int LuaScriptInterface::luaGlobalEventCreate(lua_State* L)
{
// GlobalEvent(name)
// it doesn't make sense to manipulate onThink/onStartup/onShutdown/onRecord
GlobalEventMap eventMap = g_globalEvents->getEventMap(GLOBALEVENT_TIMER);
auto globalEvent = eventMap.find(getString(L, 2));
if (globalEvent != eventMap.end()) {
pushUserdata<GlobalEvent>(L, globalEvent->second);
setMetatable(L, -1, "GlobalEvent");
} else {
lua_pushnil(L);
}
return 1;
}

int LuaScriptInterface::luaGlobalEventExecute(lua_State* L)
{
// globalEvent:execute()
GlobalEvent* globalEvent = getUserdata<GlobalEvent>(L, 1);
pushBoolean(L, globalEvent != nullptr && globalEvent->executeEvent());
return 1;
}

//
LuaEnvironment::LuaEnvironment() :
LuaScriptInterface("Main Interface"), testInterface(nullptr),
Expand Down
5 changes: 5 additions & 0 deletions src/luascript.h
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,11 @@ class LuaScriptInterface
static int luaPartyShareExperience(lua_State* L);
static int luaPartySetSharedExperience(lua_State* L);

// GlobalEvent
static int luaGlobalEventCreate(lua_State* L);

static int luaGlobalEventExecute(lua_State* L);

//
lua_State* luaState;
std::string lastLuaError;
Expand Down

0 comments on commit 76396f2

Please sign in to comment.