Jump to content
jo spanner

Starting a vehicle performing a useraction

Recommended Posts

Hi,

I have an addon vehicle in my mission, and I want it to start the mission by performing one of its actions. Specifically, it turns on a set of lights.

I have found the action I need in the vehicle's useractions section, under the cfgviewer.

How do I go about scripting it to perform that action? The only thing I've found is the function "action", which doesn't really seem to be doing what I want.

Even when doing it like so, trying to make myself as the player perform it, it still doesn't work, giving me a "Foreign Error" and "unknown enum value":

player action ["some_useraction"]

or

player action ["some_useraction", vehicle player]

What should I be doing?

Share this post


Link to post
Share on other sites

So, in my case, I needed to find the useractions bit in the cfgVehicles, and then copy and edit the "statement" section into valid code.

Effectively, don't think about trying to "perform" the action, just do the action's code.

  • Like 1

Share this post


Link to post
Share on other sites
On 4/20/2020 at 2:13 AM, jo spanner said:

So, in my case, I needed to find the useractions bit in the cfgVehicles, and then copy and edit the "statement" section into valid code.

Effectively, don't think about trying to "perform" the action, just do the action's code.

In my opinion, this is "the best" way to do it, but it requires one to look through the CfgVehicles. One major issue with this is that some vehicles (or other objects) may not have been set up by their creator. I guess there's not much you can do in this case though.

 

I created a script to open and close building doors and kind follows this idea. You can find it here and maybe with some tweaking you could possibly use it for your case, or even better make it even more generic to work for other actions too.

 

Keep in mind though that this is not a great script, performance-wise as it looks a lot into the config files (which is a rather expensive operation). Nevertheless, a (probably) simpler solution would be to use indeed this copy-paste way :).

 

EDIT: If anyone wants to have a look at the script but doesn't want to click some random link (this can be a quite clever move, depending on the forums, although I haven't encountered anything that justifies it here!) please let me know and I can post the script here. Didn't do it to save some space.

 

EDIT: I attached the code here 'cause it is quite easy to hide it and let only those interested to access it. Enjoy :).
 

 
 
 
 
Spoiler

/* ----------------------------------------------------------------------------------------------------
 * Function to open and close a door.
 * ----------------------------------------------------------------------------------------------------
 * Author: Achilles
 * ----------------------------------------------------------------------------------------------------
 * Input
 * ----------
 * building [Object]: The building
 * door [Number] (Optional): The number representing the door (defaults to 1)
 * action [Number] (Optional): Number representing the desired action. 1 to open and
 *                             0 to close (defaults to 0)
 * ----------------------------------------------------------------------------------------------------
 * Output
 * ----------
 * Nothing
 * ----------------------------------------------------------------------------------------------------
 * How-To:
 * ----------
 * - Call the function.
 * ----------------------------------------------------------------------------------------------------
 * Example
 * ----------
 * - Place the function in two addAction commands of a tablet to be used to open/close a door.
 *
 *    _tablet addAction["Open Door", {_building = _this select 3; [_building, 2, 1] call A4A_fnc_openCloseDoor}, _building];
 *    _tablet addAction["Close Door", {_building = _this select 3; [_building, 2, 0] call A4A_fnc_openCloseDoor}, _building];
 *
 * - _tablet: The tablet used to open and close the building's door
 * - _building: The building/hangar with the door
 * ----------------------------------------------------------------------------------------------------
 */

// Get input variables
params[["_building", objNull, [objNull]], // The building
       ["_door", 1, [1]], // The number representing the door
       ["_action", 0, [0]]]; // The desired action (0 -> close, 1 -> open)

// Perform parameter checks
// Check building
if(isNull _building) exitWith {
  systemChat "openCloseDoor: _building is objNull. Exiting..."; // Exit and inform of the error
};

// Check door
private _numOfDoors = getNumber (configFile >> "CfgVehicles" >> (typeOf _building) >> "numberOfDoors"); // Get the number of doors the building has
if(_numOfDoors isEqualTo -1 || {_numOfDoors isEqualTo 0}) exitWith {
  systemChat "openCloseDoor: Building has no doors. Exiting...";
};

// Make sure door is an integer
_door = floor _door;

// Check door number is a valid one
if(_door < 1) then {
  _door = 1; // Set to default
  systemChat "openCloseDoor: _door is less than one. Setting to one..."; // Inform of the changes
} else {
  // Check availability of door
  if(_door > _numOfDoors) then {
    _door = 1; // Set default
    systemChat format["openCloseDoor: _door is greater than the available doors (%1). Setting to one...", _numOfDoors]; // Inform of the changes
  };
};

// Check action
_action = floor _action; // Make sure it's an integer

if(_action != 0 && {_action != 1}) then {
  _action = "Close"; // Set to default
  systemChat "openCloseDoor: Wrong _action number. Setting to zero (close door)..."; // Inform of the changes
} else {
  // Make action a string to use later on
  if(_action isEqualTo 0) then {
    _action = "Close";
  } else {
    _action = "Open";
  };
};

// Find the appropriate code in the configFile
private _configPath = configFile >> "CfgVehicles" >> (typeOf _building) >> "UserActions"; // Hold the path up to this point in a variable
private _allDoors = "true" configClasses (_configPath); // Get the UserAction classes for this type of building

// Look for a string containing 1) the word "door", 2) the requested number, 3) the requested action
_door = _allDoors findIf {
  (((configName _x) find "Door") != -1) && {((configName _x) find (str _door)) != -1} && {((configName _x) find _action) != -1}
};

// Execute the action on the door
_door = getText (_configPath >> configName (_allDoors select _door) >> "statement"); //Get the statement to be executed
_door = _door splitString "this"; // Get rid of the "this" variable (which is undefined in the current scope)

// Check if "this" was between other characters
if((count _door) > 1) then {
  _door = format["%1%2%3", (_door select 0), (str _building), (_door select 1)]; // Place the name of the building where "this" was before
} else {
  _door = format["%1%2", (str _building), (_door select 0)]; // Place the name of the building where "this" was before
};

// Execute the statement to perform the action
call (compile _door);

 

 

  • Like 2

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

×