Doggynub
LUA / C++
- Joined
- Sep 28, 2008
- Messages
- 2,541
- Reaction score
- 186
well based on a request, this event occur when the monster is summoned, created, spawned w/e
using this to Register any monster
Support : 0.4
Setup :
#goto creatureEvent.h :
find [cpp]uint32_t executePrepareDeath(Creature* creature, DeathList deathList);[/cpp]
under it paste [cpp] uint32_t executeOnSpawn(Creature* creature);[/cpp]
find
[cpp]CREATURE_EVENT_DEATH,[/cpp]
replace the line under it with this
[cpp]CREATURE_EVENT_PREPAREDEATH,
CREATURE_EVENT_SPAWN [/cpp]
---------------------------------------------------------------------------------------------------------------------
#goto creatureevent.cpp
find [cpp]else if(tmpStr == "preparedeath")
m_type = CREATURE_EVENT_PREPAREDEATH;[/cpp]
under it paste[cpp]else if(tmpStr == "spawn")
m_type = CREATURE_EVENT_SPAWN; [/cpp]
find [cpp] case CREATURE_EVENT_PREPAREDEATH:
return "onPrepareDeath";[/cpp]
under it paste[cpp] case CREATURE_EVENT_SPAWN:
return "onSpawn";[/cpp]
find [cpp]case CREATURE_EVENT_PREPAREDEATH:
return "cid, deathList";[/cpp]
under itpaste [cpp] case CREATURE_EVENT_SPAWN:
return "cid"; [/cpp]
go to the end of file and paste this
[cpp]uint32_t CreatureEvent::executeOnSpawn(Creature* creature)
{
//onSpawn(cid)
if(m_interface->reserveEnv())
{
ScriptEnviroment* env = m_interface->getEnv();
if(m_scripted == EVENT_SCRIPT_BUFFER)
{
env->setRealPos(creature->getPosition());
std::stringstream scriptstream;
scriptstream << "local cid = " << env->addThing(creature) << std::endl;
if(m_scriptData)
scriptstream << *m_scriptData;
bool result = true;
if(m_interface->loadBuffer(scriptstream.str()))
{
lua_State* L = m_interface->getState();
result = m_interface->getGlobalBool(L, "_result", true);
}
m_interface->releaseEnv();
return result;
}
else
{
#ifdef __DEBUG_LUASCRIPTS__
std::stringstream desc;
desc << creature->getName();
env->setEvent(desc.str());
#endif
env->setScriptId(m_scriptId, m_interface);
env->setRealPos(creature->getPosition());
lua_State* L = m_interface->getState();
m_interface->pushFunction(m_scriptId);
lua_pushnumber(L, env->addThing(creature));
bool result = m_interface->callFunction(1);
m_interface->releaseEnv();
return result;
}
}
else
{
std::clog << "[Error - CreatureEvent::executeCast] Call stack overflow." << std::endl;
return 0;
}
}
[/cpp]
----------------------------------------------------------------------------------------------------------
# goto monster.cpp
find [cpp]
void Monster:nCreatureAppear(const Creature* creature)
{
Creature:nCreatureAppear(creature);
if(creature == this)
{
//We just spawned lets look around to see who is there.
if(isSummon())
isMasterInRange = canSee(master->getPosition());[/cpp]
under it ##press enter key to move down a line or 2## then paste[cpp] CreatureEventList spawn = getCreatureEvents(CREATURE_EVENT_SPAWN);
for(CreatureEventList::iterator it = spawn.begin(); it != spawn.end(); ++it)
(*it)->executeOnSpawn(this); [/cpp]
And you are done.
using this to Register any monster
Code:
<event type="spawn" name="monsterThink" registerTo ="monster" event="script" value="look.lua"/>
Lua:
function onSpawn(cid)
doBroadcastMessage("Monster ".. getCreatureName(cid).." was created.")
return true
end
Support : 0.4
Setup :
#goto creatureEvent.h :
find [cpp]uint32_t executePrepareDeath(Creature* creature, DeathList deathList);[/cpp]
under it paste [cpp] uint32_t executeOnSpawn(Creature* creature);[/cpp]
find
[cpp]CREATURE_EVENT_DEATH,[/cpp]
replace the line under it with this
[cpp]CREATURE_EVENT_PREPAREDEATH,
CREATURE_EVENT_SPAWN [/cpp]
---------------------------------------------------------------------------------------------------------------------
#goto creatureevent.cpp
find [cpp]else if(tmpStr == "preparedeath")
m_type = CREATURE_EVENT_PREPAREDEATH;[/cpp]
under it paste[cpp]else if(tmpStr == "spawn")
m_type = CREATURE_EVENT_SPAWN; [/cpp]
find [cpp] case CREATURE_EVENT_PREPAREDEATH:
return "onPrepareDeath";[/cpp]
under it paste[cpp] case CREATURE_EVENT_SPAWN:
return "onSpawn";[/cpp]
find [cpp]case CREATURE_EVENT_PREPAREDEATH:
return "cid, deathList";[/cpp]
under itpaste [cpp] case CREATURE_EVENT_SPAWN:
return "cid"; [/cpp]
go to the end of file and paste this
[cpp]uint32_t CreatureEvent::executeOnSpawn(Creature* creature)
{
//onSpawn(cid)
if(m_interface->reserveEnv())
{
ScriptEnviroment* env = m_interface->getEnv();
if(m_scripted == EVENT_SCRIPT_BUFFER)
{
env->setRealPos(creature->getPosition());
std::stringstream scriptstream;
scriptstream << "local cid = " << env->addThing(creature) << std::endl;
if(m_scriptData)
scriptstream << *m_scriptData;
bool result = true;
if(m_interface->loadBuffer(scriptstream.str()))
{
lua_State* L = m_interface->getState();
result = m_interface->getGlobalBool(L, "_result", true);
}
m_interface->releaseEnv();
return result;
}
else
{
#ifdef __DEBUG_LUASCRIPTS__
std::stringstream desc;
desc << creature->getName();
env->setEvent(desc.str());
#endif
env->setScriptId(m_scriptId, m_interface);
env->setRealPos(creature->getPosition());
lua_State* L = m_interface->getState();
m_interface->pushFunction(m_scriptId);
lua_pushnumber(L, env->addThing(creature));
bool result = m_interface->callFunction(1);
m_interface->releaseEnv();
return result;
}
}
else
{
std::clog << "[Error - CreatureEvent::executeCast] Call stack overflow." << std::endl;
return 0;
}
}
[/cpp]
----------------------------------------------------------------------------------------------------------
# goto monster.cpp
find [cpp]
void Monster:nCreatureAppear(const Creature* creature)
{
Creature:nCreatureAppear(creature);
if(creature == this)
{
//We just spawned lets look around to see who is there.
if(isSummon())
isMasterInRange = canSee(master->getPosition());[/cpp]
under it ##press enter key to move down a line or 2## then paste[cpp] CreatureEventList spawn = getCreatureEvents(CREATURE_EVENT_SPAWN);
for(CreatureEventList::iterator it = spawn.begin(); it != spawn.end(); ++it)
(*it)->executeOnSpawn(this); [/cpp]
And you are done.
Last edited: