Jump to content

Recommended Posts

Posted (edited)

Hey,

how to get the unit controlled by player (jet, heli, tank).

I want to measure distance from player to different objects (bulls, enemy, etc).

 

And there can be many "Client" units in mission, not just only 1 specified one.

 

I didn't found any giveMyUnitNameOrId() function.

Should I itarate all coalition units?

 

Or... how to get name or id of the player that used F10 menu command?

 

Thanks

Edited by ghashpl
Posted

I did something like this:

 

isPlayer = Unit.getByName('unit/pilot name here')
	
if isPlayer then
  trigger.action.outText("this unit is controled by player", 10)
else
  trigger.action.outText("this unit is not controlled by player", 10)
end

Posted

@ghashpl

 

That snippet will only target a single unit... the moment you switch clients, it'll stop working.

 

 

I think this MOOSE script will serve you better (required fields marked in red, don't remove any quotation marks):

local function Get_Player_Unit(Player_Name)  
 
 local Client_SET = SET_CLIENT:New():FilterActive(Active):FilterOnce()
 
 Client_SET:ForEachClient(
 function(Client)
    
    if Client:GetPlayerName() then
       if Client:GetPlayerName() == Player_Name then
          
          trigger.action.outText(Client:GetName().." is controlled by player "..Player_Name, 10)
          
          local MOOSE_Object = UNIT:FindByName( Client:GetName() )
          local DCS_Object = Unit.getByName( Client:GetName() )
          
          return MOOSE_Object , DCS_Object
       end
    end 
 end 
 )
end
 
 [color="blue"]-- The following variable will contain the unit that the specified player is controlling (if any). MOOSE_Unit is the MOOSE unit object, DCS_Unit is the standard DCS unit object[/color] 
 
 local MOOSE_Unit , DCS_Unit = Get_Player_Unit("[color="Red"]Player name[/color]") 
 
 
 [color="Blue"]-- Then we use another function to get the distance between the player and a specified object on demand[/color]
 
local function Get_Distance(Player , Object_Name)
     
     if Player and UNIT:FindByName(Object_Name) then
        
        local Object_2_Measure = UNIT:FindByName(Object_Name)
        local Object_Coord = Object_2_Measure:GetCoordinate()
        local Player_Coord = Player:GetCoordinate()
        local Distance = Player_Coord:Get2DDistance(Object_Coord)

        trigger.action.outText(Object_Name.." distance to player (meters) = "..Distance, 10)
        
        return Distance
        
     end
end  

 Get_Distance(MOOSE_Unit , "[color="red"]Name of the unit whose distance you want to know[/color]") [color="Blue"]-- This will return distance in meters between the player's unit and the specified object[/color]

 

 

Remember, you'll need to load Moose.lua first, otherwise this script won't work

  • Recently Browsing   0 members

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