ethanwdp Posted July 29, 2015 Share Posted July 29, 2015 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 endendCompassCore.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() endendEvents.OnPlayerMove.Add(CompassCore.updateCoords); Link to comment Share on other sites More sharing options...
RoboMat Posted July 29, 2015 Share Posted July 29, 2015 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 More sharing options...
ethanwdp Posted July 29, 2015 Author Share Posted July 29, 2015 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 Link to comment Share on other sites More sharing options...
ethanwdp Posted July 29, 2015 Author Share Posted July 29, 2015 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" endendCompassCore.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() endend Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now