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

NPC Looking direction

Slime

Active Member
Joined
Jan 25, 2014
Messages
115
Reaction score
32
Is there any way to change the direction NPC is looking at right after it respawns? Most npcs on my server don't even move and when they respawn they are always turned north.
 
Try this one:
Code:
local npcs = {
  ["Hoaxette"] = SOUTH
}


function onStartup()
  for name, direction in pairs(npcs) do
    local npc = Npc(name)
    if npc ~= nil then
      npc:setDirection(direction)
    end
  end
end
 
Code:
function onStepIn(cid, item, pos)
    local crePos = {x = 1000, y = 997, z = 7} --- Position of the creature whose direction you wanna change
    local direction = 2 --- direction that you want it to face

        doCreatureSetLookDirection(getTopCreature(crePos).uid, direction)
    return TRUE
end

More npcs:
Code:
local config = {
    npcs = {
        [2] = {x = 1000, y = 997, z = 7},
    },
}
}

function onStepIn(cid, item, pos)
    for dir, pos in pairs(config.npcs) do
        doCreatureSetLookDirection(getTopCreature(pos).uid, dir)
    end
    return true
end
 
Last edited:
Himii's scripts turn the creatures at a specific location to a specified direction.

Kamelwixx identifies the creature by its name instead. Easier to read, but it assumes creatures with unique names.

Himii's:

Triggering with onStepIn() isn't the only option, but it would work.
You could use the same logic in the code that created the NPC's, so it would only run once.

This call: getTopCreature(pos) returns the id of the (top) creature at that position.
This: getTopCreature(pos).uid resolves to the creature's unique id
This: doCreatureSetLookDirection( creatureUid, direction) turns that creature to the correct direction.

Directions (from global.lua) are:
NORTH = 0
EAST = 1
SOUTH = 2
WEST = 3
SOUTHWEST = 4
SOUTHEAST = 5
NORTHWEST = 6
NORTHEAST = 7

The second script uses the direction (as an integer) to index a table, which means it can handle multiple creatures. To make this work properly you'd use an array of positions (one for each creature that should be turned to that direction) with an imbedded loop to step through them. You could also use a normal integer-indexed array with one entry per creature, including both position and desired direction.
 
OK, SOLVED!!!
For everyone interested:
In the data\globalevents\scripts\npcpositioning.lua

local npcs = {
[2] = {x = 2911, y = 2624, z = 7}, --the number in [] is the direction to which npc should be set and {} is obviously the position of that npc
[3] = {x = 2914, y = 2633, z = 7},
}


function onStartup(cid, item, pos)
for dir, pos in pairs(npcs) do
doCreatureSetLookDirection(getTopCreature(pos).uid, dir)
end
return true
end

Now in data\globalevents\globalevents.xml
<globalevent name="npc positioning" event="script" type="start" value="npcpositioning.lua"/>
That's all :D.
 
Last edited:
Yes, but it's all in one table so it is not so much work to do.
@edit
Well, it turned out that the script doesn't work at all. You can set up only one npc per direction. Can anyone fix this?
 
Last edited:
Yes, but it's all in one table so it is not so much work to do.
@edit
Well, it turned out that the script doesn't work at all. You can set up only one npc per direction. Can anyone fix this?

That's what I said in my post for Ray Rewind :)

Each element has to be an array of positions, like this:
Code:
local npcs = {
[2] = { {x = 2911, y = 2624, z = 7}, {x = 2924, y = 2611, z = 7} },
[3] = { {x = 2914, y = 2633, z = 7}, {x = 2933, y = 2614, z = 7} }
}

function onStartup(cid, item, pos)
    for dir, posArray in pairs(npcs) do
        for indx, pos in ipairs(posArray) do
            doCreatureSetLookDirection(getTopCreature(pos).uid, dir)
        end
    end
    return true
end

It's still messy though. When you're programming control logic, there are only 3 interesting integers: 0, 1, many. In this case, with multiple creatures per direction, I'd go with this myself:
Code:
local npcPositions = {
    { position={x = 2911, y = 2624, z = 7}, direction=NORTH },
    { position={x = 2924, y = 2611, z = 7}, direction=SOUTH },
    { position={x = 2914, y = 2633, z = 7}, direction=EAST },
    { position={x = 2955, y = 2644, z = 7}, direction=NORTH },
    { position={x = 2933, y = 2614, z = 7}, direction=WEST }
}

function onStartup(cid, item, pos)
    for indx, npcLoc in ipairs(npcPositions) do
        npcUid = getTopCreature(npcLoc.position).uid
        doCreatureSetLookDirection(npcUid, npcLoc.direction)
    end
    return true
end
The data structure is easy to figure out now, and it's a lot easier to add extra elements and comments, and to group similar NPCs together. The control logic is also simpler and easier to read and change.

Note that now we have one instance of our "programming number" many - it's the number of NPC's, which is open-ended. And we have exactly one loop, for that element. This isn't always correct, but you always try it this way first.

PS - NORTH, SOUTH, etc aren't strings. They are Lua variables set in config.lua, as I mentioned above.
 
Last edited:
Still doesn't work the proper way. After using your script:
[15/04/2014 09:23:32] data/globalevents/scripts/npcpositioning.lua:eek:nStartup
[15/04/2014 09:23:32] Description:
[15/04/2014 09:23:32] (luaDoCreatureSetLookDir) Creature not found
I removed the additional {} near npc positions so it looks like this and doesn't show any console error.
local npcs = {
[3] = {x = 2911, y = 2624, z = 7}, {x = 2913, y = 2628, z = 7}, {x = 2914, y = 2633, z = 7}
}
But now the first npc is turned right, second one is turned south and third is turned left. Wat do?
Ok, @Pteryx 's one works 100% fine. Thanks!
 
Last edited:
There were no redundant {} - your second version is producing weird results because of your change.

Change original version again and run it again.
Comment out:
doCreatureSetLookDirection(getTopCreature(pos).uid, dir)
and add:
print( "{" .. pos.x .. ", " .. pos.y .. ", " .. pos.z .. "}, " .. dir )
print( "Creature uid: " .. getTopCreature(pos).uid )

immediately below the commented line, so it looks like this:

-- doCreatureSetLookDirection(getTopCreature(pos).uid, dir)
print( dir .. ", {" .. pos.x .. ", " .. pos.y .. ", " .. pos.z .. "}" )
print( "Creature uid: " .. getTopCreature(pos).uid )
 
It's ok dude, your script works perfectly. This doesn't:
local npcs = {
[2] = { {x = 2911, y = 2624, z = 7}, {x = 2924, y = 2611, z = 7} },
[3] = { {x = 2914, y = 2633, z = 7}, {x = 2933, y = 2614, z = 7} }
}

function onStartup(cid, item, pos)
for dir, posArray in pairs(npcs) do
for indx, pos in ipairs(posArray) do
doCreatureSetLookDirection(getTopCreature(pos).uid, dir)
end
end
return true
end
 
The exact script I posted here had made-up coordinates - for each direction, I made the second position by just switching the last two digits of the x and y values from the first one.

If you ran it without checking the coordinates that error makes sense - it was telling you there's no creature at the specified position, which isn't so strange since the ones I added to my example are basically random locations :)

OTOH if there's a logic problem in the script(s) please let me know. I test them outside OT (Eclipse with the Lua plugin) so I can't be 100% sure they will work in OT.
 
Last edited:
OK, SOLVED!!!
For everyone interested:
In the data\globalevents\scripts\npcpositioning.lua

local npcs = {
[2] = {x = 2911, y = 2624, z = 7}, --the number in [] is the direction to which npc should be set and {} is obviously the position of that npc
[3] = {x = 2914, y = 2633, z = 7},
}


function onStartup(cid, item, pos)
for dir, pos in pairs(npcs) do
doCreatureSetLookDirection(getTopCreature(pos).uid, dir)
end
return true
end

Now in data\globalevents\globalevents.xml
<globalevent name="npc positioning" event="script" type="start" value="npcpositioning.lua"/>
That's all :D.
[Error - GlobalEvent::configureEvent] No valid type "start" for globalevent with name npc positioning
[Warning - BaseEvents::loadFromXml] Failed to configure event
I use TFS 1.4
I change it to 'startup' and dont have error but NPC still look SOUTH.
 
Back
Top