Jump to content

detectClient code -- help needed


Wrecking Crew

Recommended Posts

Hi, thanks for taking a look, trying to detect when clients take off. The Flag 50003 is supposed to go True on an S_EVENT_TAKEOFF. I cobbled this code from examples so there may well be something wrong, but it is not throwing errors, testing it in Single Player DCS v1.5.4. I did try different means for setting Flag 50003 to True and 1, and using just 'F-15C 01' for the player name but no joy. Appreciate any help.

 

do
local function detectClient(event)
 if(event.id == world.event.S_EVENT_TAKEOFF and event.initiator) then
  local airborneUnit = event.initiator
  if(Unit.getPlayerName(airborneUnit) == {'F-15C 01', 'F-15C 02', 'F-15C 03', 'F-15C 04','M-2000C 21', 'M-2000C 22', 'M-2000C 23', 'M-2000C 24'}) then
   trigger.setUserFlag(50003, 1)
  end
 end
end
mist.addEventHandler(detectClient)
end

 

The Player names are in the 'Pilot' field for the Client a/c.


Edited by Wrecking Crew

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Link to comment
Share on other sites

  if(Unit.getPlayerName(airborneUnit) == {'F-15C 01', 'F-15C 02', 'F-15C 03', 'F-15C 04','M-2000C 21', 'M-2000C 22', 'M-2000C 23', 'M-2000C 24'})

 

Unit.getPlayerName returns only a string, not a table. This condition won't ever be true. :)

 

Something like this maybe:

do
local function detectClient(event)
 local function contains(tbl, val)
   for _,v in ipairs(tbl) do
  if v == val then return true end
   end
   return false
 end
 if(event.id == world.event.S_EVENT_TAKEOFF and event.initiator) then
  local airborneUnit = event.initiator
  if(contains({'F-15C 01', 'F-15C 02', 'F-15C 03', 'F-15C 04','M-2000C 21', 'M-2000C 22', 'M-2000C 23', 'M-2000C 24'}, Unit.getPlayerName(airborneUnit))) then
   trigger.setUserFlag(50003, 1)
  end
 end
end
mist.addEventHandler(detectClient)
end


Edited by Rakuzard
- Deutsche Tutorials und DCS Gameplay: youtube.com/Rakuzard | raku.yt/discord -
Link to comment
Share on other sites

Thanks, had high hopes this would do it. Once this works I will be able to do lots more.

 

This did not work for player F-15C 01 in Single Player test:

do
local function detectClient(event)
 local function contains(tbl, val)
   for _,v in ipairs(tbl) do
    if v == val then return true end
   end
   return false
 end
 if(event.id == world.event.S_EVENT_TAKEOFF and event.initiator) then
  local airborneUnit = event.initiator
  if(contains({'F-15C 01', 'F-15C 02', 'F-15C 03', 'F-15C 04','M-2000C 21', 'M-2000C 22', 'M-2000C 23', 'M-2000C 24'}, Unit.getPlayerName(airborneUnit))) then
   trigger.setUserFlag(50003, 1)
  end
 end
end
mist.addEventHandler(detectClient)
end

 

 

The mission is attached. The event is the 4th one down, 'mist.detectClient'

 

The help is appreciated. :beer:

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Link to comment
Share on other sites

Do the number of spaces/tab vs spaces make a difference between working code and not? There are two spaces in the function 'contains' instead of one.

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Link to comment
Share on other sites

Making progress, this code detects the take off of the F-15C and sets the flag and outputs the text but when I quit the mission the following error appears. Why this error?

 

do
local function detectClient(event)
 local airborneUnitName = event.initiator:getName()
 if(event.id == world.event.S_EVENT_TAKEOFF and event.initiator) then
  trigger.action.setUserFlag(50003, true)
  trigger.action.outText(airborneUnitName .. ' just tookoff!', 5)
 end
end
mist.addEventHandler(detectClient)
end

 

attachment.php?attachmentid=147280&stc=1&d=1472406073

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Link to comment
Share on other sites

do
local function detectClient(event)
 local airborneUnitName = event.initiator:getName()
 if(event.id == world.event.S_EVENT_TAKEOFF and event.initiator) then
  trigger.action.setUserFlag(50003, true)
  trigger.action.outText(airborneUnitName .. ' just tookoff!', 5)
 end
end
mist.addEventHandler(detectClient)
end

 

When you quit the mission an event is triggered(S_EVENT_MISSION_END) as well

Event = {
 id = 12,
 time = Time,
}

this event, however, does no have an "initiator".

 

In your code you did

local airborneUnitName = event.initiator:getName()

before the if clause. Try to simply place this line into the "if"

do
local function detectClient(event)
 if(event.id == world.event.S_EVENT_TAKEOFF and event.initiator) then
  local airborneUnitName = event.initiator:getName()
  trigger.action.setUserFlag(50003, true)
  trigger.action.outText(airborneUnitName .. ' just tookoff!', 5)
 end
end
mist.addEventHandler(detectClient)
end

so that when you quit a mission, an event is triggered and event handler does your function. It goes to the if, only to find that the event is not a takeoff event; then it stops there and leaves no scripting error


Edited by Yukari
typo

Nooooooooo

Link to comment
Share on other sites

Makes perfect sense -- that solved the error, thank you! Now to extend the logic to detect the takeoff of specific a/c who are Clients...

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Link to comment
Share on other sites

Got it! Thank you guys. Here is the tested code and simple mission. The outText line is only for testing.

 

do
local function detectClientTakeoff(event)
 local function contains(tbl, val)
  for _,v in ipairs(tbl) do
   if v == val then return true end
  end
  return false
 end
 if(event.id == world.event.S_EVENT_TAKEOFF and event.initiator) then
  local airborneUnitName = event.initiator:getName()
  trigger.action.outText(airborneUnitName .. ' just tookoff!', 5)
  if(contains({'F-15C 01', 'F-15C 02', 'F-15C 03', 'F-15C 04', 'M-2000C 21', 'M-2000C 22', 'M-2000C 23', 'M-2000C 24'}, airborneUnitName)) then
   trigger.action.setUserFlag(50003, true)
  end
 end
end
mist.addEventHandler(detectClientTakeoff)
end

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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