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

Simple example of summon in area

bomba

Member
Joined
Feb 26, 2008
Messages
635
Reaction score
7
Location
Brazil
Need a simple example to create monsters in certain area, like this:
Code:
local config = {
monster = "Rat",
ammout = 5,
topL = {x=100, y=100, z=7},
botR = {x=200, y=200, z=7}
}
 
Last edited:
Code:
for i = 1, config.amount do
    Game.createMonster(config.monster, Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
end

Ofc if you are using TFS 1.2 as you didn't specify.
 
There is no Position() in 0.4, you have to do {x=math.random(topL.x, botR.x), y=math.random(topL.y, botR.y), z=topL.z}
 
math.random for z as well, in case they want to do multiple floors. :p
Code:
local config = {
     monster = "Rat",
     amount = 5,
     topL = {x = 100, y = 100, z = 7},
     botR = {x = 200, y = 200, z = 7}
}

for var = 1, config.amount do
     local pos = {x = math.random(config.topL.x, config.botR.x), y = math.random(config.topL.y, config.botR.y), z = math.random(config.topL.z, config.botR.z)}
     doCreateMonster(config.monster:lower(), pos)
end
 
There is no Position() in 0.4, you have to do {x=math.random(topL.x, botR.x), y=math.random(topL.y, botR.y), z=topL.z}
Isn't Position just a matter of,
Code:
function Position(a, b, c, d)
    return {x = (a > -1 and a < 65536) and a or 0, y = (b > -1 and b < 65536) and b or 0, z = (c > -1 and c < 16) and c or 7, stackpos = (d > -1 and d < 256) and d or 255}
end
Or is there more to it than that?
 
Last edited:
You have to set the metatable as well
What else is there to position aside from using it as a function, i never actually seen it used for anything else aside from using it as a shortcut to setting the x,y,z and stackpos.

It is not inherited and each metatable has its own getPosition, but since it is 0.4 it can be used as global function.
Code:
    // Position
    registerClass("Position", "", LuaScriptInterface::luaPositionCreate);
    registerMetaMethod("Position", "__add", LuaScriptInterface::luaPositionAdd);
    registerMetaMethod("Position", "__sub", LuaScriptInterface::luaPositionSub);
    registerMetaMethod("Position", "__eq", LuaScriptInterface::luaPositionCompare);

    registerMethod("Position", "getDistance", LuaScriptInterface::luaPositionGetDistance);
    registerMethod("Position", "isSightClear", LuaScriptInterface::luaPositionIsSightClear);

    registerMethod("Position", "sendMagicEffect", LuaScriptInterface::luaPositionSendMagicEffect);
    registerMethod("Position", "sendDistanceEffect", LuaScriptInterface::luaPositionSendDistanceEffect);
 
Last edited:
What else is there to position aside from using it as a function, i never actually seen it used for anything else aside from using it as a shortcut to setting the x,y,z and stackpos.

Is getPosition a metamethod of Position?
https://github.com/otland/forgottenserver/wiki/Metatable:position
Those methods. Plus the metamethods of addition, subtraction and eq (==)

How to set Position in lua:
https://github.com/otland/forgottenserver/blob/master/src/luascript.cpp#L870-L880

You just have to set the metatable for the table like this:

Code:
function Position(x, y, z, stackpos)
    local t = {x=x, y=y, z=z, stackpos=stackpos or 0}
    return setmetatable(t, debug.getmetatable(Position))
end
^ Actually this may not work have to test it but however it would work if oyu have some dummy position like
local pos = Position(1, 1, 1)
debug.getmetatable(pos)
 
Last edited:
Back
Top