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

function placeNpc

Capaverde

New Member
Joined
Sep 30, 2007
Messages
107
Reaction score
4
luascript.cpp - where else? x)
Code:
    //placeNpc(name, pos)
	lua_register(m_luaState, "placeNpc", LuaScriptInterface::luaPlaceNpc);
Code:
int32_t LuaScriptInterface::luaPlaceNpc(lua_State *L)
{
    //placeNpc(name, pos)
   	Position pos;
	uint32_t stackpos;
	popPosition(L, pos, stackpos);
	const char *name = popString(L);
	
	ScriptEnviroment* env = getScriptEnv();
	
	Npc* npc = new Npc(name);
	if(!npc->isLoaded())
	{
		delete npc;
		std::string error_str = (std::string)"Npc name(" + name + (std::string)") not found";
		reportErrorFunc(error_str);
		lua_pushnumber(L, LUA_ERROR);
		return 1;
	}
	
	// Place the npc
	if(!g_game.placeCreature(npc, (Position&)pos))
	{
		delete npc;
		std::string error_str = (std::string)"Can not summon npc: " + name;
		reportErrorFunc(error_str);
		lua_pushnumber(L, LUA_ERROR);
		return 1;
    }
    uint32_t cid = env->addThing((Thing*)npc);
	
	lua_pushnumber(L, cid);
	return 1;
}
luascript.h
Code:
	static int32_t luaPlaceNpc(lua_State* L);
done!
I made it based on the placeNpc command, it does the same thing (summons npc) and its syntax is:
Code:
placeNpc(name,position)
name is the name of the npc's xml file without .xml at the end(like in the command)
position is the position where the npc shall be summoned(like in doPlaceCreature)

you may think of it as a doPlaceCreature that instead of monsters places npcs
it can be useful to placeNpcs ingame (after the server is loaded) and without the need of a gamemaster (as happens with the command)
you can put it on actions/talkactions/movements/npc's scripts/creaturescripts(added recently ^^)/spells/weapons/ and anything else your crazy mind conceives (like an addEvent by global.lua that is independent of any other scripts found in the folders above and checks from hour to hour if a npc was killed, loading the first npc not from map but from global.lua so you can know his uniqueid and check his aliveness, and if so replaces it)
 
Last edited:
Nice! This could probably help out lots of people. You just need to create a talkaction for it and you can have hotkeys for the NPCs. :) Good for troubleshooting. Nice work. Keep it coming!
 
Useful! I just realised that the Spawn Creature function does not work with NPCs, and I needed it :)
 
Back
Top