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

Lua Function Npc that tell long stories.

tarjei

Necronian Engineer
Joined
May 25, 2008
Messages
505
Reaction score
126
Location
Poland
Hello. Some time ago I was trying to solve problem of npcs that says message one by one with delay.
I don't know if anyone solved this, any way I thought I can share my code with you guys:)
It's an easy c++ function added to luascript.cpp and handled properly in lua: )
So lets go.

open your luascript.cpp and add method:
Code:
int32_t LuaScriptInterface::luaDoNpcSay(lua_State* L)
{
	//doNpcSay(nid,txt,speaktype,cid)
	

	uint32_t  uid =popNumber(L);
	  
	SpeakClasses type = SPEAK_SAY;
	type = (SpeakClasses)popNumber(L);
	std::string text = popString(L);
	ScriptEnviroment* env = getEnv();
	

	if(Creature* creature = env->getCreatureByUID(popNumber(L)))
	{
		Npc* npc = creature->getNpc();
		Creature* creature = env->getCreatureByUID(uid);
		if(npc){

                   npc->doSay(text,type,creature->getPlayer());

		  lua_pushboolean(L, true);
        }
	}
	else
	{
		errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
		lua_pushboolean(L, false);
	}
	return 1;
}
Under :
Code:
	//doCreatureSay(uid, text[, type = SPEAK_SAY[, ghost = false[, cid = 0[, pos]]]])
	lua_register(m_luaState, "doCreatureSay", LuaScriptInterface::luaDoCreatureSay);
add:
Code:
	//doNpcSay(nid,txt,speaktype[,cid])
	lua_register(m_luaState, "doNpcSay", LuaScriptInterface::luaDoNpcSay);

and in luascript.h under:
Code:
		static int32_t luaDoCreatureSay(lua_State* L);
add:
Code:
		static int32_t luaDoNpcSay(lua_State* L);

Ok now you have a function that works almost like selfSay but it takes nid (npc id ) as a parameter.
You can use this lua function to make npc say some stuff with delay.

Code:
	 local texts = {
			{"this is showed always at 0", 1500},
			{"im showed after 1500", (4000-1500)},
			{"I will be show after 4 seconds xD", 5000},
			{"test4", 5000},
	 }
	 function delays(nid,cid,array,iter,npcHandler)
	 	if(not npcHandler:isFocused(cid)) then
			return false
		end
		  if array[iter] ~= nil then
			  doNpcSay(nid,array[iter][1],TALKTYPE_PRIVATE_NP,cid)
			  addEvent(delays,array[iter][2],nid,cid,array,iter+1,npcHandler)
		  end
	 end
        --caling the function
	delays(getNpcId(),cid,texts,1,npcHandler)

I would explain what each parameter mean there, but I think you can figure it out yourself : )
Have fun with it, make it perfect etc : )
Regards.
 
Last edited:
Msg delay should depends from last npc msg delay, if he's msg will end after 0.5 sec next msg should appear and so on.
So you should create something like lua function "getLastNpcMsgDelay"
This is of course if you wanna make it better like in real tibia.

Next problem is, what will be if someone leave npc? also you should add "stopevent"
 
@2up
I think you dont undestand this : P
You give an array with messages as parameter to this function
It checks if you are focused (if not it stopes executing). Otherwise it tells you first message and set addEvent (delay = number next messege) that was said.
2.step) checks if you are focused at npcs. (if not it stops) if you are it says you second messega and set addEvent and so on. I quess you havent checked it out ^^

Besides I just share this code, someone who see this usefull can use it and impove by himself :)

@up You are welcome :)
 
@2up
I think you dont undestand this : P
You give an array with messages as parameter to this function
It checks if you are focused (if not it stopes executing). Otherwise it tells you first message and set addEvent (delay = number next messege) that was said.
2.step) checks if you are focused at npcs. (if not it stops) if you are it says you second messega and set addEvent and so on. I quess you havent checked it out ^^

Besides I just share this code, someone who see this usefull can use it and impove by himself :)

@up You are welcome :)

Ok now i understand first part. What about something like lua function "getLastNpcMsgTime" after which the message will disappear, i mean when someone is conversation in default like in 7.6 tibia; next yellow msg appears only when the previous msg ended. We need time of that previous msg. You are able to create that function(getLastNpcMsgTime) in c++?
 
I think that time how long the message is visible depends only on client. But you can easily study the average time and push this time into array, so I think you dont need this function you are talking about xD
 
Indeed but that wasnt my goal to wirte that : P I just wanted to improve selfSay function to work with addEvent ( with selfSay each message is said by diffrent npc ) : P
 
Back
Top