Jump to content
Sign in to follow this  
Luft08

Taking control of AI units without interrupting what they are doing.

Recommended Posts

I am making a multiplayer mission in which a player takes control of a convoy in order to give it waypoints. The convoy starts parked along a road. The problem I'm having is that when the player takes control some of the vehicles start moving. I have already set their formation to single file. I'm not sure why this is happening or how to stop it. My code should allow a player to take control of the convoy without interrupting the convoy's actions. I add a custom menu to whichever player has a particular object (Radio) and remove it when they relinquish it. Here is some of the code:

// Important!! Run these on both client and server.
// LFT_ConvoyControlFunctions.sqf

/*
 * Function: LFT_AddConvoyMenu
 * Parameters:
 *   _player - Object: The player to add the convoy control menu actions to.
 * Returns:
 *   None
 * Description:
 *   Adds custom menu actions to the player for transferring and removing convoy control.
 */
LFT_AddConvoyMenu = {
    params ["_player"];
    
    // Check if convoy actions already exist
    private _existingActions = _player getVariable ["convoyActions", []];
    if (count _existingActions > 0) exitWith {};

    // Add convoy control actions
    private _transferActionId = _player addAction ["Transfer convoy control", {
        params ["_player"];
        // Transfer control of the convoy to the player
        [_player] call LFT_TransferAIGroupToPlayer;      
    }];
    
    private _removeActionId = _player addAction ["Remove convoy control", {
        params ["_player"];
        // Remove control of the convoy from the player
        [_player] call LFT_TransferControlBackToAI;
    }];
    
    // Store the action IDs
    _player setVariable ["convoyActions", [_transferActionId, _removeActionId], true];
};

/*
 * Function: LFT_RemoveConvoyMenu
 * Parameters:
 *   _player - Object: The player to remove the convoy control menu actions from.
 * Returns:
 *   None
 * Description:
 *   Removes all convoy-related custom menu actions from the player.
 */
LFT_RemoveConvoyMenu = {
    params ["_player"];
    private _actionIds = _player getVariable ["convoyActions", []];
    {
        _player removeAction _x;
    } forEach _actionIds;
    _player setVariable ["convoyActions", nil, true];
    [_player] call LFT_TransferControlBackToAI;
};

/*
 * Function: LFT_TransferAIGroupToPlayer
 * Parameters:
 *   _player - Object: The player to transfer control to.
 * Returns:
 *   None
 * Description:
 *   Transfers control of the AI group to the player and makes the player the leader.
 */
LFT_TransferAIGroupToPlayer = {
    params ["_player"];
    originalPlayerGroup = group _player;
    if (originalPlayerGroup == group convoyCommander) exitWith {};

    // Transfer all units from the original group to the player's group
    (units originalCommanderGroup) joinSilent originalPlayerGroup;

    // Select the player as the leader of the new group
    originalPlayerGroup selectLeader _player;
    [originalPlayerGroup, _player] remoteExec ["selectLeader", groupOwner originalPlayerGroup];
};

/*
 * Function: LFT_TransferControlBackToAI
 * Parameters: 
 *   _player - Object: The player to transfer control from.
 * Returns:
 *   None
 * Description:
 *   Transfers control of the AI group back to the original AI leader and makes the original AI unit the leader.
 */
LFT_TransferControlBackToAI = {
    params ["_player"];
  
    // Check if originalCommanderGroup is null and create a new group if necessary
    if (isNull originalCommanderGroup) then {
        originalCommanderGroup = createGroup west;
    };
    
    {
        [_x] joinSilent originalCommanderGroup;
    } forEach units group _player;
    
    originalCommanderGroup selectLeader convoyCommander;
    [originalCommanderGroup, convoyCommander] remoteExec ["selectLeader", groupOwner originalCommanderGroup];
    
    [_player] joinSilent originalPlayerGroup;
    [originalPlayerGroup, _player] remoteExec ["selectLeader", groupOwner originalPlayerGroup];
};

 

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×