modify this so that it also shows meters you're away from it, live [code]
- User
modify this so that it also shows meters you're away from it, live
code - Assistant
modify this so that it also shows meters you're away from it, live
-- Declare global tables to store centers and waypoint statuses
centers = {}
local waypoints = {}
local proximityDistance = 100 -- Distance at which waypoints turn red
if SERVER then
util.AddNetworkString("ShowWaypoint")
-- Function to send waypoint data to the client
local function SendWaypointToClient(ply, pos, index)
net.Start("ShowWaypoint")
net.WriteVector(pos)
net.WriteUInt(index, 16)
net.Send(ply)
end
-- Send trigger positions to clients when a player spawns
concommand.Add("waypoints", function(ply)
print("PlayerInitialSpawn hook called")
-- Find all trigger entities that could potentially be set for autosave
local triggers = {}
for _, ent in pairs(ents.GetAll()) do
if string.find( ent:GetClass(), 'button' ) or ent:GetClass() == 'func_button' then
table.insert(triggers, ent)
end
end
-- Sort triggers by entindex
table.sort(triggers, function(a, b) return a:EntIndex() < b:EntIndex() end)
-- Store centers and send positions to clients
centers = {} -- Clear the centers table
for index, ent in ipairs(triggers) do
local min, max = ent:GetCollisionBounds()
local center = ent:GetPos() + (min + max) / 2
centers[index] = center
SendWaypointToClient(ply, center, index)
print("Trigger entity found:", ent)
print("TriggerPos" .. index .. " sent to client:", center)
end
-- After all triggers, find the nearest trigger_changelevel
local triggerChangelevels = ents.FindByClass("trigger_changelevel")
if #triggerChangelevels > 0 then
local nearest = triggerChangelevels[1]
local minDist = ply:GetPos():Distance(nearest:GetPos())
for i = 2, #triggerChangelevels do
local dist = ply:GetPos():Distance(triggerChangelevels[i]:GetPos())
if dist < minDist then
nearest = triggerChangelevels[i]
minDist = dist
end
end
local index = #centers + 1
local changelevelCenter = nearest:GetPos() + (nearest:OBBMins() + nearest:OBBMaxs()) / 2
centers[index] = changelevelCenter
SendWaypointToClient(ply, changelevelCenter, index)
print("trigger_changelevel entity found:", nearest)
print("TriggerPos" .. index .. " sent to client:", changelevelCenter)
end
print("Total number of centers found:", #centers)
end)
else -- CLIENT
-- Retrieve the trigger positions from the server
net.Receive("ShowWaypoint", function()
local pos = net.ReadVector()
local index = net.ReadUInt(16)
waypoints[index] = { position = pos, isReached = false }
end)
-- Display all waypoints and change color based on proximity
hook.Add("HUDPaint", "DrawWaypoints", function()
if #waypoints == 0 then return end
local plyPos = LocalPlayer():GetPos()
for i, waypoint in ipairs(waypoints) do
if waypoint and waypoint.position then
local pos = waypoint.position:ToScreen()
local distance = plyPos:Distance(waypoint.position)
-- Determine color based on distance
local color
if distance <= proximityDistance then
table.RemoveByValue( waypoints, waypoint )
else
color = Color(155, 255, 0, 255) -- Default color
end
-- Draw the waypoint
surface.SetDrawColor(color)
-- Draw a circle for each waypoint
for j = 0, 4 do
surface.DrawCircle(pos.x, pos.y, 4.9 + j)
end
-- Optionally, draw text label
draw.SimpleTextOutlined("Waypoint " .. i, "Default", pos.x, pos.y - 20, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, color_black)
end
end
end)
end
To modify your code so that it displays the distance in meters to each waypoint live, you'll need to adjust the HUDPaint hook in the client-side code. Here's how you can do it:
Calculate the distance in meters: Since distances in Garry's Mod are in units, you'll need to convert them to meters. An approximate conversion is 52.5 units per meter.
Display the distance on the HUD: Use draw.SimpleTextOutlined to display the distance at each waypoint's screen position.
Handle waypoint removal safely: When modifying a table while iterating over it, it's safer to iterate backwards to prevent index issues.
Here's the modified client-side code:
hook.Add("HUDPaint", "DrawWaypoints", function()
if #waypoints == 0 then return end
local plyPos = LocalPlayer():GetPos()
local unitsPerMeter = 52.5 -- Approximate conversion factor
-- Iterate backwards to safely remove waypoints during iteration
for i = #waypoints, 1, -1 do
local waypoint = waypoints[i]
if waypoint and waypoint.position then
local pos = waypoint.position:ToScreen()
local distance = plyPos:Distance(waypoint.position)
local distanceInMeters = distance / unitsPerMeter
-- Determine color based on distance
local color = Color(155, 255, 0, 255) -- Default color
if distance <= proximityDistance then
table.remove(waypoints, i)
end
-- Draw the waypoint
surface.SetDrawColor(color)
-- Draw a circle for each waypoint
for j = 0, 4 do
surface.DrawCircle(pos.x, pos.y, 4.9 + j)
end
-- Draw the waypoint label
draw.SimpleTextOutlined("Waypoint " .. i, "Default", pos.x, pos.y - 20, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, color_black)
-- Draw the distance in meters
draw.SimpleTextOutlined(string.format("%.1f meters", distanceInMeters), "Default", pos.x, pos.y - 35, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, color_black)
end
end
end)
Explanation of Changes:
Distance Conversion: Added unitsPerMeter = 52.5 to convert units to meters. Calculated distanceInMeters = distance / unitsPerMeter.
Display Distance: Added a draw.SimpleTextOutlined call to display the distance above each waypoint label.
Safe Waypoint Removal: Changed the loop to iterate from #waypoints down to 1 using for i = #waypoints, 1, -1 do. This ensures that removing waypoints inside the loop doesn't cause indexing issues.
Set Default Color: Moved color = Color(155, 255, 0, 255) before the proximity check to ensure it's always defined.
Now, when you run your script, it will display the distance in meters to each waypoint live on the HUD.
Not yet included in a published release.