• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[TFS 0.3.6] onSpawn (Changed)

ianelli

Ramza
Joined
Mar 18, 2010
Messages
125
Reaction score
47
Location
Brazil
Hi. I've seen some "onSpawn" functions to TFS 1.0 and other versions, but none seems to work correctly on 0.3.6, so i've created my own and am posting here, may help someone.

Some people made the "onSpawn" from HERE work, but only when the monster RE-spawn. So, you have to kill him so he trigger the creatureevent when he respawn. With that code i'm posting it will work on the first spawn (when loading server) and on re-spawns too. Hope that help!

First of all, let's go to creatureevent.h and look for:

Code:
uint32_t executePrepareDeath(Creature* creature, DeathList deathList);

Below it, paste that:

Code:
uint32_t executeSpawn(Creature* creature);

Then, look for:

Code:
CREATURE_EVENT_PREPAREDEATH,

And below it, paste that:

Code:
CREATURE_EVENT_SPAWN,

Now go to creatureevent.cpp and look for:

Code:
else if(tmpStr == "preparedeath")
        m_type = CREATURE_EVENT_PREPAREDEATH;

And below it, paste:

Code:
else if(tmpStr == "spawn")
        m_type = CREATURE_EVENT_SPAWN;

Find:

Code:
case CREATURE_EVENT_PREPAREDEATH:
            return "onPrepareDeath";

And below it, paste:

Code:
case CREATURE_EVENT_SPAWN:
            return "onSpawn";

Then find:

Code:
case CREATURE_EVENT_LOGIN:
        return "cid";


And change it to:

Code:
case CREATURE_EVENT_LOGIN:
        case CREATURE_EVENT_SPAWN:
            return "cid";


Then, on the end of the file (creatureevent.cpp), paste that:

Code:
uint32_t CreatureEvent::executeSpawn(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;

            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->setEventDesc(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::cout << "[Error - CreatureEvent::executeSpawn] Call stack overflow." << std::endl;
        return 0;
    }
}


Then go to monsters.h and look for:

Code:
private:

ABOVE it, paste that:

Code:
void alertSpawn();

Now go to monsters.cpp and look for:

Code:
void Monster::onCreatureAppear(const Creature* creature)

ABOVE it, paste that:

Code:
void Monster::alertSpawn()
{
 CreatureEventList onSpawnEvents = getCreatureEvents(CREATURE_EVENT_SPAWN);
        for(CreatureEventList::iterator it = onSpawnEvents.begin(); it != onSpawnEvents.end(); ++it)
           (*it)->executeSpawn(this);     
}

Now go to spawn.cpp and look for that function:

Code:
bool Spawn::spawnMonster(uint32_t spawnId, MonsterType* mType, const Position& pos, Direction dir, bool startup /*= false*/)

On the end of it, look for:

Code:
return true;
}

and put it like that:

Code:
    monster->alertSpawn();
    return true;
}

Now just compile and see if everything is working.

Now let's go to data/creaturescripts/creaturescripts.xml, open it and paste that:

Code:
<event type="spawn" name="spawn" event="script" value="spawn.lua"/>

Then create a file inside data/creaturescripts/scripts named spawn.lua and paste that:

Code:
function onSpawn(cid)
   print("Spawned monster - Name: ".. getCreatureName(cid))
end

You can change the code to do whatever you want.

To register that event on monsters, go to data/monsters/scripts, open the monster file you want, and make the end of the file something like that:

Code:
    <script>
    <event name="spawn"/>
    </script>
</monster>

That's it. Let me know if something goes wrong. Hope that helps! See ya!







 
Back
Top