• 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!

Feature [CreatureEvent] Regestire all monster and players in creatureevent.xml

Doggynub

LUA / C++
Joined
Sep 28, 2008
Messages
2,541
Reaction score
186
Well as it says you can now register all monsters + player through creature event.xml

How to ?
You need to include "registerTo" in the xml
XML:
<event type="think" name="monsterThink" registerTo ="monster" event="script" value="look.lua"/> <!-- all monster no players-->
 
<event type="think" name="playerThink" registerTo ="player" event="script" value="test.lua"/><!-- players no monster-->
 
<event type="think" name="bothThink" registerTo ="all" event="script" value="move.lua"/><!-- both player & mon-->

You can even make it to none , so you are able to add it manually to monsters or in login
XML:
<event type="think" name="monsterThink" registerTo ="manual" event="script" value="look.lua"/> <!-- you manually register this event-->

Supports : 0.4

Setup:


# goto creatureevent.cpp :


search for this
[cpp]
bool CreatureEvents::playerLogin(Player* player)[/cpp]
and in the block of this function search for

[cpp]
return result;[/cpp]
then paste this before it
[cpp]
if (result)
{
for(CreatureEventList::iterator it = m_creatureEvents.begin(); it != m_creatureEvents.end(); ++it)
{
CreatureEvent* event = *it;
if(event->isLoaded() && ( event->getRegister() == "player" || event->getRegister() == "all") )
player->registerCreatureEvent(event->getName());
}

}[/cpp]
after this
[cpp]
std::string tmpStr = asLowerCaseString(str);[/cpp]
paste this
[cpp]
std::string strr;
if(!readXMLString(p, "registerTo", strr) || strr.empty() ){
if((tmpStr != "login") && (tmpStr != "logout"))
std::clog << "[Warning - CreatureEvent::configureEvent] : You havn't register event '" << m_eventName <<"'"<< std::endl;
}
else
m_eventRegister = strr;[/cpp]
------------------------------------------------------------------------------------------------------------------------------------

# goto creatureevent.h :

after this
[cpp]
CreatureEvent* getEventByName([/cpp]
paste this
[cpp]
std::vector<CreatureEvent*> getAllCreatureEvents(){return m_creatureEvents;}[/cpp]
now in the end of file after this
[cpp]
std::string m_eventName;[/cpp]
paste this
[cpp]
std::string m_eventRegister;[/cpp]
after this
[cpp]
const std::string& getName() const {return m_eventName;}[/cpp]
paste this
[cpp]
const std::string getRegister() const {return m_eventRegister;}[/cpp]

-------------------------------------------------------------------------------------------------------------------------------------

# go to monster.cpp

under this
[cpp]
extern Monsters g_monsters;[/cpp]
paste this
[cpp]
extern CreatureEvents* g_creatureEvents;[/cpp]
find this
[cpp]
// register creature events
for(StringVec::iterator it = mType->scriptList.begin(); it != mType->scriptList.end(); ++it)
{
if(!registerCreatureEvent(*it))
std::cout << "[Warning - Monster::Monster] Unknown event name - " << *it << std::endl;
}[/cpp]
under it place this
[cpp]
// register creature events
std::vector<CreatureEvent*> allEvents = g_creatureEvents->getAllCreatureEvents();
for(std::vector<CreatureEvent*>::const_iterator it = allEvents.begin(); it != allEvents.end(); ++it)
{
CreatureEvent* event = *it;
if(event->isLoaded() && ( event->getRegister() == "monster" || event->getRegister() == "all") )
registerCreatureEvent(event->getName());
}[/cpp]



You are done I guess.
 
Last edited:
Very nice release. I didn't test it but looks good.

BTW I think that there should be function where I can register custom event not for all players/monsters but only for selected.
 
Very nice release. I didn't test it but looks good.

BTW I think that there should be function where I can register custom event not for all players/monsters but only for selected.

selected for what , as you know you can go in the script and make the select you want.

Code:
function onThink(cid)
if getCreatureName(cid)== "rat" then   -- selection

if getCreatureName(cid) == "Jhon"   -- selection
end
 
Nice job..
Didnt have that idea yet. I will use it to make my creaturescripts more clean. Thx..
 
Awesome. Its very usefull for 24h events or players events or something like that xD.
 
1- You don't need to register event in login.lua
2- If you want a creature event for monsters, you dont need to go to each monster fill and add the script name there.[most important]
 
Well
Code:
<event type="think" name="say" registerTo ="monster"  event="script" value="think.lua"/>

think.lua
Lua:
local selectedmonster = {"rat","dog","dragon","lion","demon"}
function onThink(cid)
    if isInArray(selectedmonster ,getCreatureName(cid)) then
         doCreatureSay(cid,"I am selectedmonster",TALKTYPE_MONSTER_SAY)
    end
    return true
end
 
we need smth lik onSpawn(cid) creaturevent ;x so when monsters appear
 
why would you need that for ,couldn't think of something useful, but can easily be done.
 
Last edited:
This one is definetelly great, congratz :D

I agree with the onSpawn :)

EDIT:
You have to spread some rep. before giving..


RE EDIT:

Where you say
"# go to monster.h"

you mean monster.cpp, right?

fix it for the noobs :)


RE RE EDIT:
IMO, you should add a new registerTo to not register any creature automatically, sometimes it's useful when you want to make specific scripts that you dont want to use all the time
 
Last edited:
Then just leave the registerTo with empty string, like that you can ,register it manually in login or in monster file.

You only get a warning about it in console , to let you know nothing else, or you can just make it like that
XML:
<event type="think" name="monsterThink" registerTo ="silent" event="script" value="look.lua"/>
 
and with this i can use the old register system if i dont put anything in "registerTo"?
 
Then just leave the registerTo with empty string, like that you can ,register it manually in login or in monster file.

You only get a warning about it in console , to let you know nothing else, or you can just make it like that
XML:
<event type="think" name="monsterThink" registerTo ="silent" event="script" value="look.lua"/>

:O try to make "none" parameter to avoid the warning, will be more pro :)
 
you can essily use the example "silent" to ignore the error, or just remove the line of the error on compile.
 
This function for 0.3.6 created by me will be usefull to this:
getMonstersOnline()
luascript.cpp
Search:
Lua:
int32_t LuaScriptInterface::luaGetPlayersOnline(lua_State* L)
{
	//getPlayersOnline()
	ScriptEnviroment* env = getEnv();
	AutoList<Player>::iterator it = Player::autoList.begin();

	lua_newtable(L);
	for(int32_t i = 1; it != Player::autoList.end(); ++it, ++i)
	{
		lua_pushnumber(L, i);
		lua_pushnumber(L, env->addThing(it->second));
		pushTable(L);
	}
	return 1;
}

And under you add:
Lua:
int32_t LuaScriptInterface::luaGetMonstersOnline(lua_State* L)
{
	//getMonstersOnline()
	ScriptEnviroment* env = getEnv();
	AutoList<Monster>::iterator it = Monster::autoList.begin();

	lua_newtable(L);
	for(int32_t i = 1; it != Monster::autoList.end(); ++it, ++i)
	{
		lua_pushnumber(L, i);
		lua_pushnumber(L, env->addThing(it->second));
		pushTable(L);
	}
	return 1;
}

Now search:
Lua:
       //getPlayersOnline()
	lua_register(m_luaState, "getPlayersOnline", LuaScriptInterface::luaGetPlayersOnline);

And under you add:
Lua:
        	//getMonstersOnline()
	lua_register(m_luaState, "getMonstersOnline", LuaScriptInterface::luaGetMonstersOnline);


luascript.h

Search:
Lua:
	static int32_t luaGetPlayersOnline(lua_State* L);

Under write:
Lua:
 		static int32_t luaGetMonstersOnline(lua_State* L);


Test script that I was used:
Lua:
function onSay(cid, words, param, channel)
	local m = getMonstersOnline()
	if(not m) then
		return doPlayerSendCancel(cid, "No are monsters")
	end
	for _, mid in ipairs(m) do
		local mpos = getThingPos(mid)
		doSendAnimatedText(mpos, 'WORK', 255)
	end
	return 0
end
 
Back
Top