Jump to content

Recommended Posts

Posted

Hi,

I've been thinking about creating a dynamic flight plan creator. I'm using the latest openbeta, tried it on a syria map.
So far I've got this code:
 

-- Define the altitude at which the waypoints will be added (in meters)
local ALTITUDE = 1500

-- Define the maximum number of waypoints the player can add
local MAX_WAYPOINTS = 15

-- Create an empty table to store the player's waypoints
local playerWaypoints = {}

-- Define a function to create a waypoint based on a map marker
function createWaypointFromMarker(marker, player)
  -- Get the coordinates of the map marker
  local point = marker:getPoint()

  -- Create a new waypoint at the same coordinates
  local waypoint = {
    ["type"] = "Turning Point",
    ["action"] = "Turning Point",
    ["altitude"] = ALTITUDE,
    ["alt_type"] = "BARO",
    ["speed"] = 150,
    ["x"] = point.x,
    ["y"] = point.z,
    ["name"] = "Waypoint " .. #Group.getByName(player:getClientGroup():getName()):getWaypoints() + 1,
    ["task"] = {
      ["id"] = "ComboTask",
      ["params"] = {
        ["tasks"] = {
          {
            ["enabled"] = true,
            ["id"] = "EngageTargets",
            ["number"] = 1,
            ["params"] = {
              ["expend"] = "Jettison",
              ["weaponType"] = 2032
            }
          }
        }
      }
    }
  }
  -- Add the new waypoint to the group
  Group.getByName(player:getClientGroup():getName()):getController():pushTask(waypoint)
end

function onPlayerAddMarker(eventName, args)
  local clientName = args.initiator:getClientName()
  if clientName ~= nil then
    -- Get the player's coalition
   local coalition = args.initiator:getCoalition()
-- Get the map marker based on the name of the marker group and the text of the marker
    local markerGroup = coalition.getGroups(coalition, Group.Category.MARKER)
    for _, group in pairs(markerGroup) do
      if group:getName() == args.groupName then
        for _, marker in pairs(group:getMarkers()) do
          if marker:getName() == args.text then
            -- Add a new waypoint based on the map marker
            local client = args.initiator:getClientGroup():getUnits()[1]
            if not playerWaypoints[client] then
              playerWaypoints[client] = {}
            end
            if #playerWaypoints[client] < MAX_WAYPOINTS then
              createWaypointFromMarker(marker, client)
              table.insert(playerWaypoints[client], marker)
              trigger.action.outTextForGroup(client:getGroup():getID(), "Added waypoint " .. #playerWaypoints[client], 5)
            else
              trigger.action.outTextForGroup(client:getGroup():getID(), "You have reached the maximum number of waypoints (" .. MAX_WAYPOINTS .. ")", 5)
            end
            break
          end
        end
        break
      end
    end
  end
end

-- Register the event handler for the "mark_added" event
-- This event is triggered when a player adds a new marker on the F10 map

world.addEventHandler("mark_added", onPlayerAddMarker)

I load up the mission, and jump into a client A-10C and when I try to add a marker on the F10 Map I get the error:
 

(See attached)
Anyone got any ideas how this could be resolved.
If this is something that DCS is currently not supporting please also let me know.
Thanks in advance!

error.jpg

Posted

Your event handler is incorrect, and some other errors. Here is a suggestion on how to handle events:

local function onPlayerAddMarker(event)
  local unit = event.initiator
  if unit and event.id == world.event.S_EVENT_MARK_ADDED then
    local clientName = unit:getPlayerName()
    if clientName then
      -- get some values
      local coalition = unit:getCoalition()
      local group = unit:getGroup()
      local markId = event.idx
      local markText = event.text
      local markPosition = event.pos
      -- do your stuff here
    end
  end
end

local eventHandler = { f = onPlayerAddMarker }
function eventHandler:onEvent(e)
  self.f(e)
end
world.addEventHandler(eventHandler)

 

Posted (edited)
2 hours ago, Elphaba said:

I believe you should be using the world enum not "mark_added"

e.g.

 

"mark_added" is the name, so it can be anything as far as I know. (but I get what you mean)

2 hours ago, Chump said:

Your event handler is incorrect, and some other errors. Here is a suggestion on how to handle events:

local function onPlayerAddMarker(event)
  local unit = event.initiator
  if unit and event.id == world.event.S_EVENT_MARK_ADDED then
    local clientName = unit:getPlayerName()
    if clientName then
      -- get some values
      local coalition = unit:getCoalition()
      local group = unit:getGroup()
      local markId = event.idx
      local markText = event.text
      local markPosition = event.pos
      -- do your stuff here
    end
  end
end

local eventHandler = { f = onPlayerAddMarker }
function eventHandler:onEvent(e)
  self.f(e)
end
world.addEventHandler(eventHandler)

 

I'll give this one a try, thank you!

Edited by Skynet99
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...