Banner Image

Module Functional.ClientWatch

Functional - Manage and track client slots easily to add your own client-based menus and modules to.

The #CLIENTWATCH class adds a simplified way to create scripts and menus for individual clients. Instead of creating large algorithms and juggling multiple event handlers, you can simply provide one or more prefixes to the class and use the callback functions on spawn, despawn, and any aircraft related events to script to your hearts content.


Features:

  • Find clients by prefixes or by providing a Wrapper.CLIENT object
  • Trigger functions when the client spawns and despawns
  • Create multiple client instances without overwriting event handlers between instances
  • More reliable aircraft lost events for when DCS thinks the aircraft id dead but a dead event fails to trigger
  • Easily manage clients spawned in dynamic slots

Author: Statua

Contributions: FlightControl: Wrapper.CLIENT


Global(s)

Global CLIENTWATCH

Manage and track client slots easily to add your own client-based menus and modules to.

#CLIENTWATCH CLIENTWATCH

Manage and track client slots easily to add your own client-based menus and modules to.

Creating a new instance

To start, you must first create a new instance of the client manager and provide it with either a Wrapper.Client#CLIENT object, a string prefix of the unit name, or a table of string prefixes for unit names. These are used to capture the client unit when it spawns and apply your scripted functions to it. Only fixed wing and rotary wing aircraft controlled by players can be used by this class. This will not work if the client aircraft is alive!

Examples

     -- Create an instance with a Wrapper.Client#CLIENT object
     local heliClient = CLIENT:FindByName('Rotary1-1')
     local clientInstance = CLIENTWATCH:New(heliClient)

     -- Create an instance with part of the unit name in the Mission Editor
     local clientInstance = CLIENTWATCH:New("Rotary")

     -- Create an instance using prefixes for a few units as well as a FARP name for any dynamic spawns coming out of it
     local clientInstance = CLIENTWATCH:New({"Rescue","UH-1H","FARP ALPHA"})

Applying functions and methods to client aircraft when they spawn

Once the instance is created, it will watch for birth events. If the unit name of the client aircraft matches the one provided in the instance, the callback method #CLIENTWATCH() can be used to apply functions and methods to the client object.

In the OnAfterSpawn() callback method are four values. From, Event, To, and ClientObject. From,Event,To are standard FSM strings for the state changes. ClientObject is where the magic happens. This is a special object which you can use to access all the data of the client aircraft. The following entries in ClientObject are available for you to use:

  • ClientObject.Unit: The Moose Wrapper.Unit#UNIT of the client aircraft
  • ClientObject.Group: The Moose Wrapper.Group#GRUP of the client aircraft
  • ClientObject.Client: The Moose Wrapper.Client#CLIENT of the client aircraft
  • ClientObject.PlayerName: A #string of the player controlling the aircraft
  • ClientObject.UnitName: A #string of the client aircraft unit.
  • ClientObject.GroupName: A #string of the client aircraft group.

Examples

     -- Create an instance with a client unit prefix and send them a message when they spawn
     local clientInstance = CLIENTWATCH:New("Rotary")
     function clientInstance:OnAfterSpawn(From,Event,To,ClientObject,EventData)
         MESSAGE:New("Welcome to your aircraft!",10):ToUnit(ClientObject.Unit)
     end

Using event callbacks

In a normal setting, you can only use a callback function for a specific option in one location. If you have multiple scripts that rely on the same callback from the same object, this can get quite messy. With the ClientWatch module, these callbacks are isolated t the instances and therefore open the possibility to use many instances with the same callback doing different things. ClientWatch instances subscribe to all events that are applicable to player controlled aircraft and provides callbacks for each, forwarding the EventData in the callback function.

The following event callbacks can be used inside the OnAfterSpawn() callback:

  • :OnAfterDespawn(From,Event,To): Triggers whenever DCS no longer sees the aircraft as 'alive'. No event data is given in this callback as it is derived from other events
  • :OnAfterHit(From,Event,To,EventData): Triggers every time the aircraft takes damage or is struck by a weapon/explosion
  • :OnAfterKill(From,Event,To,EventData): Triggers after the aircraft kills something with its weapons
  • :OnAfterScore(From,Event,To,EventData): Triggers after accumulating score
  • :OnAfterShot(From,Event,To,EventData): Triggers after a single-shot weapon is released
  • :OnAfterShootingStart(From,Event,To,EventData): Triggers when an automatic weapon begins firing
  • :OnAfterShootingEnd(From,Event,To,EventData): Triggers when an automatic weapon stops firing
  • :OnAfterLand(From,Event,To,EventData): Triggers when an aircraft transitions from being airborne to on the ground
  • :OnAfterTakeoff(From,Event,To,EventData): Triggers when an aircraft transitions from being on the ground to airborne
  • :OnAfterRunwayTakeoff(From,Event,To,EventData): Triggers after lifting off from a runway
  • :OnAfterRunwayTouch(From,Event,To,EventData): Triggers when an aircraft's gear makes contact with a runway
  • :OnAfterRefueling(From,Event,To,EventData): Triggers when an aircraft begins taking on fuel
  • :OnAfterRefuelingStop(From,Event,To,EventData): Triggers when an aircraft stops taking on fuel
  • :OnAfterPlayerLeaveUnit(From,Event,To,EventData): Triggers when a player leaves an operational aircraft
  • :OnAfterCrash(From,Event,To,EventData): Triggers when an aircraft is destroyed (may fail to trigger if the aircraft is only partially destroyed)
  • :OnAfterDead(From,Event,To,EventData): Triggers when an aircraft is considered dead (may fail to trigger if the aircraft was partially destroyed first)
  • :OnAfterPilotDead(From,Event,To,EventData): Triggers when the pilot is killed (may fail to trigger if the aircraft was partially destroyed first)
  • :OnAfterUnitLost(From,Event,To,EventData): Triggers when an aircraft is lost for any reason (may fail to trigger if the aircraft was partially destroyed first)
  • :OnAfterEjection(From,Event,To,EventData): Triggers when a pilot ejects from an aircraft
  • :OnAfterHumanFailure(From,Event,To,EventData): Triggers when an aircraft or system is damaged from any source or action by the player
  • :OnAfterHumanAircraftRepairStart(From,Event,To,EventData): Triggers when an aircraft repair is started
  • :OnAfterHumanAircraftRepairFinish(From,Event,To,EventData): Triggers when an aircraft repair is completed
  • :OnAfterEngineStartup(From,Event,To,EventData): Triggers when the engine enters what DCS considers to be a started state. Parameters vary by aircraft
  • :OnAfterEngineShutdown(From,Event,To,EventData): Triggers when the engine enters what DCS considers to be a stopped state. Parameters vary by aircraft
  • :OnAfterWeaponAdd(From,Event,To,EventData): Triggers when an item is added to an aircraft's payload
  • :OnAfterWeaponDrop(From,Event,To,EventData): Triggers when an item is jettisoned or dropped from an aircraft (unconfirmed)
  • :OnAfterWeaponRearm(From,Event,To,EventData): Triggers when an item with internal supply is restored (unconfirmed)

Examples

     -- Show a message to player when they take damage from a weapon
     local clientInstance = CLIENTWATCH:New("Rotary")
         function clientInstance:OnAfterSpawn(From,Event,To,ClientObject,EventData)
         function ClientObject:OnAfterHit(From,Event,To,EventData)
            local typeShooter = EventData.IniTypeName
            local nameWeapon = EventData.weapon_name
            MESSAGE:New("A "..typeShooter.." hit you with a "..nameWeapon,20):ToUnit(ClientObject.Unit)
         end
     end

Global CLIENTWATCHTools

#table CLIENTWATCHTools

@type CLIENTWATCHTools @field #table Unit Wrapper.UNIT of the cient object @field #table Group Wrapper.GROUP of the cient object @field #table Client Wrapper.CLIENT of the cient object @field #string PlayerName Name of the player controlling the client object @field #string UnitName Name of the unit that is the client object @field #string GroupName Name of the group the client object belongs to

Type(s)

Fields and Methods inherited from CLIENTWATCH Description

CLIENTWATCH.ClassName

Name of the class.

CLIENTWATCH.Debug

Write Debug messages to DCS log file and send Debug messages to all players.

CLIENTWATCH.DebugEventData

CLIENTWATCH:FilterByCategory(Category, Category, value)

Filter out all clients that are not of the given category

CLIENTWATCH:FilterByCoalition(Coalition, Coalition, value)

Filter out all clients not belonging to the provided coalition

CLIENTWATCH.FilterCategory

If not nil, will only activate for aircraft of the given category value.

CLIENTWATCH.FilterCoalition

If not nil, will only activate for aircraft of the given coalition value.

CLIENTWATCH:New(Will, Put, Provide, Leave, client)

Creates a new instance of CLIENTWATCH to add scripts to.

CLIENTWATCH:OnAfterSpawn(Controllable, From, Event, To, clientObject, eventdata)

User function for OnAfter "Spawn" event.

CLIENTWATCH.lid

String for DCS log file.

CLIENTWATCH.version

CLIENTWATCH version

Fields and Methods inherited from FSM_CONTROLLABLE Description

CLIENTWATCH.Controllable

CLIENTWATCH:GetControllable()

Gets the CONTROLLABLE object that the FSM_CONTROLLABLE governs.

CLIENTWATCH:New(FSMT, Controllable)

Creates a new FSM_CONTROLLABLE object.

CLIENTWATCH:OnAfterStop(Controllable, From, Event, To)

OnAfter Transition Handler for Event Stop.

CLIENTWATCH:OnBeforeStop(Controllable, From, Event, To)

OnBefore Transition Handler for Event Stop.

CLIENTWATCH:OnEnterStopped(Controllable, From, Event, To)

OnEnter Transition Handler for State Stopped.

CLIENTWATCH:OnLeaveStopped(Controllable, From, Event, To)

OnLeave Transition Handler for State Stopped.

CLIENTWATCH:SetControllable(FSMControllable)

Sets the CONTROLLABLE object that the FSM_CONTROLLABLE governs.

CLIENTWATCH:Stop()

Synchronous Event Trigger for Event Stop.

CLIENTWATCH:__Stop(Delay)

Asynchronous Event Trigger for Event Stop.

CLIENTWATCH:_call_handler(step, trigger, params, EventName)

  • CLIENTWATCH class

Field(s)

#string CLIENTWATCH.ClassName

Name of the class.

#boolean CLIENTWATCH.Debug

Write Debug messages to DCS log file and send Debug messages to all players.

#number CLIENTWATCH.FilterCategory

If not nil, will only activate for aircraft of the given category value.

#number CLIENTWATCH.FilterCoalition

If not nil, will only activate for aircraft of the given coalition value.

#string CLIENTWATCH.lid

String for DCS log file.

#string CLIENTWATCH.version

CLIENTWATCH version

Function(s)

Filter out all clients that are not of the given category

Defined in:

CLIENTWATCH

Parameters:

#number Category

number (0 = airplane, 1 = helicopter)

#string Category

string ('airplane' or 'helicopter')

value

Filter out all clients not belonging to the provided coalition

Defined in:

CLIENTWATCH

Parameters:

#number Coalition

number (1 = red, 2 = blue)

#string Coalition

string ('red' or 'blue')

value

Creates a new instance of CLIENTWATCH to add scripts to.

Can be used multiple times with the same client/prefixes if you need it for multiple scripts.

Defined in:

CLIENTWATCH

Parameters:

#string Will

watch for clients whos UNIT NAME or GROUP NAME matches part of the #string as a prefix.

#table Put

strings in a table to use multiple prefixes for the above method.

a Moose CLIENT object to apply to that specific aircraft slot (static slots only!)

#nil Leave

blank to activate for ALL CLIENTS

client

Return value:

self

User function for OnAfter "Spawn" event.

Defined in:

CLIENTWATCH

Parameters:

Controllable of the group.

#string From

From state.

#string Event

Event.

#string To

To state.

#table clientObject

Custom object that handles events and stores Moose object data. See top documentation for more details.

#table eventdata

Data from EVENTS.Birth.

Field(s)

#string CLIENTWATCH.ClassName

Name of the class.

#boolean CLIENTWATCH.Debug

Write Debug messages to DCS log file and send Debug messages to all players.

#number CLIENTWATCH.FilterCategory

If not nil, will only activate for aircraft of the given category value.

#number CLIENTWATCH.FilterCoalition

If not nil, will only activate for aircraft of the given coalition value.

#string CLIENTWATCH.lid

String for DCS log file.

#string CLIENTWATCH.version

CLIENTWATCH version

Function(s)

Gets the CONTROLLABLE object that the FSM_CONTROLLABLE governs.

Creates a new FSM_CONTROLLABLE object.

Defined in:

Parameters:

#table FSMT

Finite State Machine Table

(optional) The CONTROLLABLE object that the FSM_CONTROLLABLE governs.

Return value:

OnAfter Transition Handler for Event Stop.

Defined in:

Parameters:

The Controllable Object managed by the FSM.

#string From

The From State string.

#string Event

The Event string.

#string To

The To State string.

OnBefore Transition Handler for Event Stop.

Defined in:

Parameters:

The Controllable Object managed by the FSM.

#string From

The From State string.

#string Event

The Event string.

#string To

The To State string.

Return value:

#boolean:

Return false to cancel Transition.

OnEnter Transition Handler for State Stopped.

Defined in:

Parameters:

The Controllable Object managed by the FSM.

#string From

The From State string.

#string Event

The Event string.

#string To

The To State string.

OnLeave Transition Handler for State Stopped.

Defined in:

Parameters:

The Controllable Object managed by the FSM.

#string From

The From State string.

#string Event

The Event string.

#string To

The To State string.

Return value:

#boolean:

Return false to cancel Transition.

Sets the CONTROLLABLE object that the FSM_CONTROLLABLE governs.

Defined in:

Parameter:

Return value:

Synchronous Event Trigger for Event Stop.

Asynchronous Event Trigger for Event Stop.

Defined in:

Parameter:

#number Delay

The delay in seconds.

Defined in:

Parameters:

step

trigger

params

EventName