Jump to content

getDirectionTo woes


ethanwdp

Recommended Posts

I've been working on a compass mod. I've reworked it so that the compass only shows the direction you're facing, which is the problem. It doesn't return the direction.

 

I'm at my wits end as of why it does this. When I simply execute the code inside of the function using LetMeSpeaks command line which uses loadstring ingame, it works as intended. Using loadstring in the function doesn't help.

 

Here's my code:

 

CompassCore.returnDirection = function()
    local wx, wy = getPlayer():getX(), getPlayer():getY()
    wx = math.floor(wx);
    wy = math.floor(wy);
    local cell = getWorld():getCell();
    local sq = cell:getGridSquare(getPlayer():getX(), getPlayer():getY(), getPlayer():getZ());
    local sqObjs = sq:getObjects();
    local sqSize = sqObjs:size();
    for i = sqSize-1, 0, -1 do
        local obj = sqObjs:get(i);
        local direction = getDirectionTo(getPlayer(), obj)
        getPlayer():Say(getDirectionTo(getPlayer(), obj))
        if direction == "N" then
            return "North"
        elseif direction == "NW" then
            return "North East"
        elseif direction == "NE" then
            return "North West"
        elseif direction == "S" then
            return "South"
        elseif direction == "SW" then
            return "South West"
        elseif direction == "SE" then
            return "South East"
        elseif direction == "W" then
            return "West"
        elseif direction == "E" then
            return "East"
        else
            return "ERROR" -- it always returns error, for some reason. presumably because it's calling nil, which I don't know why
        end
    end
end

CompassCore.updateCoords = function()
    if ISCompassWindow:getIsVisible() then
        if CompassCore.DoNotListCoords ~= true then
            ISCompassWindow.compassLines[2] = "-------------Your Coords-------------".." <LINE> ".."X: "..CompassCore.doRound(getPlayer():getX()).." Y: "..CompassCore.doRound(getPlayer():getY()).." <LINE> "
        end
        if CompassCore.MarkedX ~= nil and CompassCore.MarkedY ~= nil then    
            ISCompassWindow.compassLines[1] = "-----"..CompassCore.DisplayName.." Coords-----".." <LINE> ".."X: "..CompassCore.MarkedX.." Y: "..CompassCore.MarkedY.." <LINE> "
            if CompassCore.DoNotListCoords ~= true then
                ISCompassWindow.compassLines[3] = "-----Distance to Destination-----".." <LINE> ".."X: "..CompassCore.checkCoords(tonumber(CompassCore.MarkedX), getPlayer():getX()).." Y: "..CompassCore.checkCoords(tonumber(CompassCore.MarkedY), getPlayer():getY()).." <LINE> "
            end
            --ISCompassWindow.compassLines[4] = "-----Direction to Destination-----".." <LINE> "..CompassCore.returnDirection()
        end
        if CompassCore.CurrentDirection == true then
            ISCompassWindow.compassLines[1] = "-----Current Direction----- <LINE> "..CompassCore.returnDirection().." <LINE> "
        end
        CompassCore.updateCompass()
    end
end

Events.OnPlayerMove.Add(CompassCore.updateCoords);

Link to comment
Share on other sites

Have you printed out direction yet?

 

Btw. you can get rid of the if-else construct by doing this:

-- Put this somewhere outside of the functionlocal DIRECTIONS = {  N  = 'North',  NE = 'North East',  NW = 'North West',  S  = 'South',  SW = 'South West',  SE = 'South East',  W  = 'West',  E  = 'East',} -- In the function replace the if-elses with:return DIRECTIONS[direction] or error('Your error text here ...')
Link to comment
Share on other sites

 

Have you printed out direction yet?

 

Btw. you can get rid of the if-else construct by doing this:

-- Put this somewhere outside of the functionlocal DIRECTIONS = {  N  = 'North',  NE = 'North East',  NW = 'North West',  S  = 'South',  SW = 'South West',  SE = 'South East',  W  = 'West',  E  = 'East',} -- In the function replace the if-elses with:return DIRECTIONS[direction] or error('Your error text here ...')

Tried printing it, only works if I directly load it with loadstring using my in-game commandline.

 

I know the if-elses aren't very efficient at the moment, I usually switch to tables afterwards. I just use them for the testing phase :P

Link to comment
Share on other sites

EDIT: Nevermind, figured it out.

 

getDirectionTo() only returns North West or North for some reason, so I used IsoDirections.fromAngle(getPlayer():getAngle()). Works like a charm now:

 

Code:

 

CompassCore.returnDirection = function()
    local wx, wy = getPlayer():getX(), getPlayer():getY()
    wx = math.floor(wx);
    wy = math.floor(wy);
    local cell = getWorld():getCell();
    local sq = cell:getGridSquare(wx, wy, getPlayer():getZ());
    local sqObjs = sq:getObjects();
    local sqSize = sqObjs:size();
    for i = sqSize-1, 0, -1 do
        local obj = sqObjs:get(i);
        local direction = tostring(IsoDirections.fromAngle(getPlayer():getAngle()))
        local completedString;
        local directions = {
          N  = "North",
          NE = "North East",
          NW = "North West",
          S  = "South",
          SW = "South West",
          SE = "South East",
          W  = "West",
          E  = "East"
        }
        getPlayer():Say(direction)
        return directions[direction] or "ERROR"
    end
end

CompassCore.updateCoords = function()
    if ISCompassWindow:getIsVisible() then
        if CompassCore.DoNotListCoords ~= true then
            ISCompassWindow.compassLines[2] = "-------------Your Coords-------------".." <LINE> ".."X: "..CompassCore.doRound(getPlayer():getX()).." Y: "..CompassCore.doRound(getPlayer():getY()).." <LINE> "
        end
        if CompassCore.MarkedX ~= nil and CompassCore.MarkedY ~= nil then    
            ISCompassWindow.compassLines[1] = "-----"..CompassCore.DisplayName.." Coords-----".." <LINE> ".."X: "..CompassCore.MarkedX.." Y: "..CompassCore.MarkedY.." <LINE> "
            if CompassCore.DoNotListCoords ~= true then
                ISCompassWindow.compassLines[3] = "-----Distance to Destination-----".." <LINE> ".."X: "..CompassCore.checkCoords(tonumber(CompassCore.MarkedX), getPlayer():getX()).." Y: "..CompassCore.checkCoords(tonumber(CompassCore.MarkedY), getPlayer():getY()).." <LINE> "
            end
            --ISCompassWindow.compassLines[4] = "-----Direction to Destination-----".." <LINE> "..CompassCore.returnDirection()
        end
        if CompassCore.CurrentDirection == true then
            ISCompassWindow.compassLines[1] = "-----Current Direction----- <LINE> "..CompassCore.returnDirection().." <LINE> "
        end
        CompassCore.updateCompass()
    end
end

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...