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

Doggynub

LUA / C++
Joined
Sep 28, 2008
Messages
2,541
Best answers
0
Reaction score
181
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:

VirrageS

←•†ĿuĀ && ©¤¤•→
Joined
May 30, 2010
Messages
984
Best answers
0
Reaction score
59
Location
Poland
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.
 
OP
Doggynub

Doggynub

LUA / C++
Joined
Sep 28, 2008
Messages
2,541
Best answers
0
Reaction score
181
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
 

Summ

(\/)(;,,;)(\/) Y not?
Staff member
Global Moderator
Joined
Oct 15, 2008
Messages
4,168
Best answers
9
Reaction score
1,011
Location
Germany :O
Nice job..
Didnt have that idea yet. I will use it to make my creaturescripts more clean. Thx..
 

Fresh

Quack!
Joined
Oct 21, 2009
Messages
1,576
Best answers
10
Reaction score
167
Location
Poland
Awesome. Its very usefull for 24h events or players events or something like that xD.
 
OP
Doggynub

Doggynub

LUA / C++
Joined
Sep 28, 2008
Messages
2,541
Best answers
0
Reaction score
181
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]
 

zakius

Enter the Ninja!
Joined
Apr 30, 2009
Messages
2,635
Best answers
0
Reaction score
61
Location
with Taiga
what about register to array of monster names? would be nice
 
OP
Doggynub

Doggynub

LUA / C++
Joined
Sep 28, 2008
Messages
2,541
Best answers
0
Reaction score
181
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
 

cbrm

Top Class brunet scripter
Staff member
Global Moderator
Joined
Jan 6, 2009
Messages
6,585
Best answers
3
Reaction score
851
Location
Caribbean Sea
we need smth lik onSpawn(cid) creaturevent ;x so when monsters appear
 
OP
Doggynub

Doggynub

LUA / C++
Joined
Sep 28, 2008
Messages
2,541
Best answers
0
Reaction score
181
why would you need that for ,couldn't think of something useful, but can easily be done.
 
Last edited:

Scarlet Ayleid

Intermediate OT User
Joined
Jul 7, 2007
Messages
4,055
Best answers
0
Reaction score
233
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:
OP
Doggynub

Doggynub

LUA / C++
Joined
Sep 28, 2008
Messages
2,541
Best answers
0
Reaction score
181
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"/>
 

Exedion

Excellent OT User
Joined
Jun 11, 2007
Messages
628
Best answers
0
Reaction score
28
and with this i can use the old register system if i dont put anything in "registerTo"?
 

Exedion

Excellent OT User
Joined
Jun 11, 2007
Messages
628
Best answers
0
Reaction score
28
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 :)
 
OP
Doggynub

Doggynub

LUA / C++
Joined
Sep 28, 2008
Messages
2,541
Best answers
0
Reaction score
181
you can essily use the example "silent" to ignore the error, or just remove the line of the error on compile.
 

KylerXX

Intermediate OT User
Joined
Jun 24, 2010
Messages
439
Best answers
0
Reaction score
28
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
 
Top