koragg Posted October 13, 2016 Share Posted October 13, 2016 Hey everyone. I wanna extract some of the functionality from the race_progressbar resource but I'm not sure what to do here. I need the distance from player to the next checkpoint to be drawn somewhere on the screen with dxdraw so that it updates live when the player moves (I can draw it but not sure how to actually use the code I have to get it first). Other things like total map distance and distance from spawn to first checkpoint are a bonus If anyone can help out, please reply back. Here's what I found that's connected to the things I need : function calculateCheckpointDistance() local total = 0 local cps = {} local cpsSum = {} local pos = g_Spawnpoints[1].position local prevX, prevY, prevZ = pos[1],pos[2],pos[3] for k,v in ipairs(g_Checkpoints) do local pos = v.position if prevX ~= nil then local distance = getDistanceBetweenPoints3D(pos[1],pos[2],pos[3],prevX,prevY,prevZ) total = total + distance cps[k] = distance cpsSum[k] = total end prevX = pos[1] prevY = pos[2] prevZ = pos[3] end g_CheckpointDistance = cps g_CheckpointDistanceSum = cpsSum g_TotalDistance = total end function distanceFromPlayerToCheckpoint(player, i) -- TODO: check if the index exists local checkpoint = g_Checkpoints[i] if checkpoint == nil then return false end local x, y, z = getElementPosition(player) return getDistanceBetweenPoints3D(x, y, z, unpack(checkpoint.position)) end function calculateDistance(headingForCp,distanceToCp) local sum = g_CheckpointDistanceSum[headingForCp] if sum == nil then return false end local checkpointDistance = g_CheckpointDistance[headingForCp] if distanceToCp > checkpointDistance then distanceToCp = checkpointDistance end return sum - distanceToCp end function calculateDistance2(headingForCp,distanceToCp) local sum = 0 for k,v in ipairs(g_CheckpointDistance) do sum = sum + v if headingForCp == k then if distanceToCp > v then distanceToCp = v end sum = sum - distanceToCp return sum end end return 0 end This was in the client.lua file of the resource (couldn't find mta community link so sry for github one instead). Link to comment
Captain Cody Posted October 13, 2016 Share Posted October 13, 2016 distanceFromPlayerToCheckpoint(player, i) That's what you need to use.. Link to comment
koragg Posted October 13, 2016 Author Share Posted October 13, 2016 (edited) 21 minutes ago, CodyL said: distanceFromPlayerToCheckpoint(player, i) That's what you need to use.. Yes but, how to make it output as dxdrawing? I tried adding dxdraw under the 'return' part and it started giving errors that g_Checkpoints[i] is undefined and stuff like that. Used this but still didn't work I think: g_Checkpoints = getAll("checkpoint") Edited October 13, 2016 by koragg Link to comment
Captain Cody Posted October 13, 2016 Share Posted October 13, 2016 add a onClientRender event - then use this dxDrawText(distanceFromPlayerToCheckpoint(player, i) -- Other stuff for dxDrawText Link to comment
koragg Posted October 14, 2016 Author Share Posted October 14, 2016 (edited) It draws "false" instead of distance with the following code. No errors in debug at all. local sx_, sy_ = guiGetScreenSize ( ) local sx, sy = sx_/1440, sy_/900 function distanceFromPlayerToCheckpoint(player, i) g_Checkpoints = getAll("checkpoint") local checkpoint = g_Checkpoints[i] if checkpoint == nil then return false end local x, y, z = getElementPosition(player) return getDistanceBetweenPoints3D(x, y, z, unpack(checkpoint.position)) end function getAll(name) local result = {} for i,element in ipairs(getElementsByType(name)) do result[i] = {} result[i].id = getElementID(element) or i local position = { tonumber(getElementData(element,"posX")), tonumber(getElementData(element,"posY")), tonumber(getElementData(element,"posZ")) } result[i].position = position; end return result end function draw() dxDrawText(tostring(distanceFromPlayerToCheckpoint(player, i)), sx_ * 1.5200, sy_ * 0.9843, sx_ * 0.2172, sy_ * 1.0167, tocolor(131, 253, 1, 255), 0.55*sy, "bankgothic", "center", "top", false, false, false, false, false) end addEventHandler("onClientRender", root, draw) I did 'tostring' on line 30 as that's what it said in debug as the only error. Edited October 14, 2016 by koragg Link to comment
dugasz1 Posted October 14, 2016 Share Posted October 14, 2016 player and i variable isn't defined in line 30. Use localPlayer instead player draw it in a for. Link to comment
koragg Posted October 14, 2016 Author Share Posted October 14, 2016 I replaced *player* to *localPlayer but still same thing, draws "false" with no errors in debug. Link to comment
quindo Posted October 15, 2016 Share Posted October 15, 2016 Where are you defining "i" variable? I don't see it anywhere in the code. Link to comment
koragg Posted October 15, 2016 Author Share Posted October 15, 2016 Not sure how to define it Link to comment
Gravestone Posted October 15, 2016 Share Posted October 15, 2016 21 hours ago, koragg said: dxDrawText(tostring(distanceFromPlayerToCheckpoint(player, i)), sx_ * 1.5200, sy_ * 0.9843, sx_ * 0.2172, sy_ * 1.0167, tocolor(131, 253, 1, 255), 0.55*sy, "bankgothic", "center", "top", false, false, false, false, false) Neither 'player' nor 'i' is defined in the 'draw' function. Link to comment
Gravestone Posted October 15, 2016 Share Posted October 15, 2016 Sorry for being late to edit the post. Anyways, if you're using the default race gamemode then this code will do the job: local sx_, sy_ = guiGetScreenSize ( ) local sx, sy = sx_/1440, sy_/900 function distanceFromPlayerToCheckpoint(player, i) g_Checkpoints = getAll("checkpoint") local checkpoint = g_Checkpoints[getElementData(player, "race.checkpoint")] if checkpoint == nil then return false end local x, y, z = getElementPosition(player) return getDistanceBetweenPoints3D(x, y, z, unpack(checkpoint.position)) end function getAll(name) local result = {} for i,element in ipairs(getElementsByType(name)) do result[i] = {} result[i].id = getElementID(element) or i local position = { tonumber(getElementData(element,"posX")), tonumber(getElementData(element,"posY")), tonumber(getElementData(element,"posZ")) } result[i].position = position; end return result end function draw() dxDrawText(tostring(distanceFromPlayerToCheckpoint(localPlayer)), sx_ * 1.5200, sy_ * 0.9843, sx_ * 0.2172, sy_ * 1.0167, tocolor(0, 0, 0, 255), 0.55*sy, "bankgothic", "center", "top", false, false, false, false, false) end addEventHandler("onClientRender", root, draw) 1 Link to comment
koragg Posted October 15, 2016 Author Share Posted October 15, 2016 (edited) Yes, it does work. Just one thing, is it in meters? Because when I'm right next to a cp it says "20". Down right side, red number under the HUD : Edited October 15, 2016 by koragg Link to comment
Gravestone Posted October 15, 2016 Share Posted October 15, 2016 I guess yeah, it's in meters. It shows 20 because you're really 20 GTA meters away from the checkpoint. The position of the checkpoint is right in the center of it. Due to it's scale being enlarged, I thought that there was some problem too. Link to comment
koragg Posted October 15, 2016 Author Share Posted October 15, 2016 Xiti said this on skype "You need then to subtraction the size of checkpoint to have at accurate" but I couldn't find anything relevant around Google, any thoughts? Link to comment
Gravestone Posted October 15, 2016 Share Posted October 15, 2016 4 minutes ago, koragg said: Xiti said this on skype "You need then to subtraction the size of checkpoint to have at accurate" but I couldn't find anything relevant around Google, any thoughts? Well I don't think so you can achieve the checkpoint(marker) element anyway and then getMarkerSize. However, you can just subtract a specific number from the distance. By viewing the above photo, you can try -15. Link to comment
koragg Posted October 15, 2016 Author Share Posted October 15, 2016 2 minutes ago, Gravestone said: Well I don't think so you can achieve the checkpoint(marker) element anyway and then getMarkerSize. However, you can just subtract a specific number from the distance. By viewing the above photo, you can try -15. About the -15, isn't that different if cp size is bigger/smaller than the one on the pic? Link to comment
Gravestone Posted October 15, 2016 Share Posted October 15, 2016 1 minute ago, koragg said: About the -15, isn't that different if cp size is bigger/smaller than the one on the pic? Yes it would be. I thought you have similar checkpoint size for every map. You can try something else then. Link to comment
koragg Posted October 15, 2016 Author Share Posted October 15, 2016 g_Checkpoints = getAll("checkpoint") local checkpoint = g_Checkpoints[getElementData(player, "race.checkpoint")] local size = getMarkerSize ( checkpoint ) Can this work? Link to comment
koragg Posted October 15, 2016 Author Share Posted October 15, 2016 6 minutes ago, Gravestone said: I guess so, try it. Nope, gave these errors. Link to comment
Gravestone Posted October 15, 2016 Share Posted October 15, 2016 Well then the table is not returning the marker element. Try getElementByID maybe. Link to comment
koragg Posted October 17, 2016 Author Share Posted October 17, 2016 (edited) Tried this and while there are no errors, there is no distance now as well. local sx_, sy_ = guiGetScreenSize ( ) local sx, sy = sx_/1440, sy_/900 function draw() for k,v in ipairs(getElementsByType("player")) do local state = getElementData(v, "player state") if state == "Spectating" or state == "Away" or state == "Dead" or state == "Finished" or state == "Not Ready" then return end if distanceFromPlayerToCheckpoint(localPlayer) then distance = round(distanceFromPlayerToCheckpoint(localPlayer)) local cp = getElementByID("checkpoint") if cp then local size = getMarkerSize (cp) if size then local fix = distance - size dxDrawText(fix.." m to next checkpoint", sx_ * 0.8734, sy_ * 0.2009, sx_ * 0.9510, sy_ * 0.2259, tocolor(254, 254, 34, 255), 1*sy, "default-bold", "center", "top", false, false, false, false, false) end end end end end addEventHandler("onClientRender", root, draw) Edited October 17, 2016 by koragg Link to comment
Gravestone Posted October 17, 2016 Share Posted October 17, 2016 (edited) 2 hours ago, koragg said: Tried this and while there are no errors, there is no distance now as well. That is because: 2 hours ago, koragg said: local cp = getElementByID("checkpoint") if cp then Line 11 checks if the getElementByID had returned the ID of the given element. In this case, you're using the first argument as a string, which has to be an element instead. Quote id: The ID of the element as it appears in the XML file or as set by setElementID. Gimme some time, I'll try to figure it out. Edit: Done, here's how it will work: local sx_, sy_ = guiGetScreenSize ( ) local sx, sy = sx_/1440, sy_/900 function distanceFromPlayerToCheckpoint(player, i) g_Checkpoints = getAll("checkpoint") local checkpoint = g_Checkpoints[getElementData(player, "race.checkpoint")] if checkpoint == nil then return false end local x, y, z = getElementPosition(player) for i, v in ipairs(checkpoint) do outputChatBox(unpack(v)) end return getDistanceBetweenPoints3D(x, y, z, unpack(checkpoint.position)), unpack(checkpoint.size) end function getAll(name) local result = {} for i,element in ipairs(getElementsByType(name)) do result[i] = {} result[i].id = getElementID(element) or i local position = { tonumber(getElementData(element,"posX")), tonumber(getElementData(element,"posY")), tonumber(getElementData(element,"posZ")) } local size = { tonumber(getElementData(element, "size")) } result[i].position = position; result[i].size = size; end return result end function draw() local distance, size = distanceFromPlayerToCheckpoint(localPlayer) dxDrawText(distance-size*5, sx_ * 1.5200, sy_ * 0.9843, sx_ * 0.2172, sy_ * 1.0167, tocolor(0, 0, 0, 255), 0.55*sy, "bankgothic", "center", "top", false, false, false, false, false) end addEventHandler("onClientRender", root, draw) Edited October 17, 2016 by Gravestone Link to comment
koragg Posted October 17, 2016 Author Share Posted October 17, 2016 attempt to perform aritmetic on local size a nil value Link to comment
Dealman Posted October 17, 2016 Share Posted October 17, 2016 That means you're trying to do some math on something that returns nil. For example nil+1 obviously wouldn't make sense. The error will tell you at what line this error occurred - use that to find what is returning nil and work from there. Link to comment
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