Animals: Override Default Animal Behaviour Via Script – Arma 3

From Bohemia Interactive Community
Jump to navigation Jump to search

Template:note


Introduction

Current system of animal behaviour is that animals are randomly idling / moving around when spawned. This is caused by changes on engine side to save some performance. However, many people from community want to be able to control animals via script in some reasonable manner. So I started to work on some tweaks of animal animation configs which will offer this possibility to them. This page is overview of this feature and tutorial, how this behaviour can be achieved.

At the moment, the tweaks are made for dog animation config only, because dog has the most variable movement possibilities and is mostly requested by community. Don't worry, other animals will be tweaked as well.

Basics

Animals can be spawned via multiple ways. Here I will explain you which ways will work with my recent tweaks, which partly and which not.

Module Animals from Sites category

Probably the easiest way how to spawn animals. Just insert the module in editor, set the desired parameters and you have animals in your mission. Unfortunately, animals spawned this way can't be controlled via script in any way and will only randomly move around depending on the area you set.

Spawn via createAgent command

Spawning animals via scripting command createAgent is another way how to have animals in your mission. And yes, animals created this way can be controled in some limited way. As the animal AI is controlled by AIAgent, it won't be able to be controlled via commands such as doMove and doStop and will roam randomly around. But you can determine, in which state (like move, stop, etc.) the animal will move.

Example:

_dog = createAgent ["Fin_random_F", getPos player, [], 5, "CAN_COLLIDE"];

Spawn via createUnit command

Another way to spawn animal is using createUnit command. This is probably most interesting for you, modders and mission creators. In this mode the AI is handled differently than agent so the animal won't randomly change directions and will face same direction in which it has been spawned. But the cool point is, you can command the animal via doMove and doStop commands, so you will be able to control it in any way you want (creating waypoints, following the player, etc.)

Example:

_grp = createGroup CIVILIAN;
_dog = _grp createUnit ["Fin_random_F", getPos player, [], 5, "CAN_COLLIDE"];

Additionaly the animal can be groupped with player and thus will be able to obey some basic commands like Move via command menu.

Example:

_dog = group player createUnit ["Fin_random_F", getPos player, [], 5, "CAN_COLLIDE"];

Usage

It is very simple. All you need is playMove or switchMove commands and knowledge of available animation states, which override the default animal behaviour (see list of currently available animals). When you will do that, remember, the animal will remain in that state until you change it via script. And also keep in mind, that the animal agent (spawned via createAgent command) will be affected by FSM if you won't disbale it via BIS_fnc_animalBehaviour_disable variable!

Example mission for VR map

You can try this mission - simply create mission for VR map, copy and paste / create following two files and preview in editor. All dog actions are controllable via action menu. See this video captured from this example mission.

mission.sqm

version=12;
class Mission
{
	addOns[]=
	{
		"a3_characters_f",
		"map_vr",
		"A3_Characters_F_Civil"
	};
	addOnsAuto[]=
	{
		"A3_Characters_F_Civil",
		"a3_characters_f",
		"map_vr"
	};
	randomSeed=9626175;
	class Intel
	{
		timeOfChanges=1800.0002;
		startWeather=0;
		startWind=0.1;
		startWaves=0.1;
		forecastWeather=0;
		forecastWind=0.1;
		forecastWaves=0.1;
		forecastLightnings=0.1;
		year=2035;
		day=28;
		hour=13;
		minute=37;
		startFogDecay=1;
		forecastFogDecay=1;
	};
	class Groups
	{
		items=1;
		class Item0
		{
			side="CIV";
			class Vehicles
			{
				items=1;
				class Item0
				{
					position[]={150,5,150};
					id=0;
					side="CIV";
					vehicle="C_man_1";
					player="PLAYER COMMANDER";
					leader=1;
					skill=0.60000002;
				};
			};
		};
	};
	class Markers
	{
		items=4;
		class Item0
		{
			position[]={100,5,200};
			name="BURK_mrk1";
			text="Marker 1";
			type="hd_destroy";
			colorName="ColorBlack";
		};
		class Item1
		{
			position[]={200,5,200};
			name="BURK_mrk2";
			text="Marker 2";
			type="hd_destroy";
			colorName="ColorBlack";
		};
		class Item2
		{
			position[]={200,5,100};
			name="BURK_mrk3";
			text="Marker 3";
			type="hd_destroy";
			colorName="ColorBlack";
		};
		class Item3
		{
			position[]={100,5,100};
			name="BURK_mrk4";
			text="Marker 4";
			type="hd_destroy";
			colorName="ColorBlack";
		};
	};
};
class Intro
{
	addOns[]=
	{
		"map_vr"
	};
	addOnsAuto[]=
	{
		"map_vr"
	};
	randomSeed=15868174;
	class Intel
	{
		timeOfChanges=1800.0002;
		startWeather=0;
		startWind=0.1;
		startWaves=0.1;
		forecastWeather=0;
		forecastWind=0.1;
		forecastWaves=0.1;
		forecastLightnings=0.1;
		year=2035;
		day=28;
		hour=13;
		minute=37;
		startFogDecay=0.0049999999;
		forecastFogDecay=0.0049999999;
	};
};
class OutroWin
{
	addOns[]=
	{
		"map_vr"
	};
	addOnsAuto[]=
	{
		"map_vr"
	};
	randomSeed=5156929;
	class Intel
	{
		timeOfChanges=1800.0002;
		startWeather=0;
		startWind=0.1;
		startWaves=0.1;
		forecastWeather=0;
		forecastWind=0.1;
		forecastWaves=0.1;
		forecastLightnings=0.1;
		year=2035;
		day=28;
		hour=13;
		minute=37;
		startFogDecay=0.0049999999;
		forecastFogDecay=0.0049999999;
	};
};
class OutroLoose
{
	addOns[]=
	{
		"map_vr"
	};
	addOnsAuto[]=
	{
		"map_vr"
	};
	randomSeed=12604209;
	class Intel
	{
		timeOfChanges=1800.0002;
		startWeather=0;
		startWind=0.1;
		startWaves=0.1;
		forecastWeather=0;
		forecastWind=0.1;
		forecastWaves=0.1;
		forecastLightnings=0.1;
		year=2035;
		day=28;
		hour=13;
		minute=37;
		startFogDecay=0.0049999999;
		forecastFogDecay=0.0049999999;
	};
};

init.sqf

waitUntil {!isNil "BIS_fnc_init"};
waitUntil {time > 0.1};

// Draw icons in 3D on marker positions 
addMissionEventHandler ["Draw3D", {
	drawIcon3D ["", [1, 1, 1, 0.75], [100, 200, 5], 0, 0, 0, "Marker 1", 1, 0.05, "PuristaMedium"];
}];

addMissionEventHandler ["Draw3D", {
	drawIcon3D ["", [1, 1, 1, 0.75], [200, 200, 5], 0, 0, 0, "Marker 2", 1, 0.05, "PuristaMedium"];
}];

addMissionEventHandler ["Draw3D", {
	drawIcon3D ["", [1, 1, 1, 0.75], [200, 100, 5], 0, 0, 0, "Marker 3", 1, 0.05, "PuristaMedium"];
}];

addMissionEventHandler ["Draw3D", {
	drawIcon3D ["", [1, 1, 1, 0.75], [100, 100, 5], 0, 0, 0, "Marker 4", 1, 0.05, "PuristaMedium"];
}];

// 3rd person view
player switchCamera "External";

// Dog group and follow setup
createCenter CIVILIAN;
BURK_grp = createGroup CIVILIAN;
BURK_dogFollowing = false;

// Spawn dog
BURK_dog = BURK_grp createUnit ["Fin_random_F", getPos player, [], 5, "CAN_COLLIDE"];

// Following player fnc
BURK_dogFollowPlayer = {
	BURK_dogFollowing = true;
	
	0 = [] spawn {
		while {BURK_dogFollowing} do 
		{
			if (alive BURK_dog) then 
			{
				BURK_dog doMove getPos player;
				sleep 1; 
			};
		};
	};
};

// Stop following fnc
BURK_dogStopFollowing = {
	BURK_dogFollowing = false;

	BURK_dog playMove "Dog_Idle_Stop";
	doStop BURK_dog;
};

// Follow defined path fnc
BURK_dogFollowPath = {
	if (BURK_dogFollowing) then {
		[] call BURK_dogStopFollowing;
	};
	
	BURK_dog doMove getMarkerPos "BURK_mrk1";	
	waitUntil {(BURK_dog distance getMarkerPos "BURK_mrk1") < 1};
	
	BURK_dog doMove getMarkerPos "BURK_mrk2";	
	waitUntil {(BURK_dog distance getMarkerPos "BURK_mrk2") < 1};
	
	BURK_dog doMove getMarkerPos "BURK_mrk3";	
	waitUntil {(BURK_dog distance getMarkerPos "BURK_mrk3") < 1};
	
	BURK_dog doMove getMarkerPos "BURK_mrk4";	
	waitUntil {(BURK_dog distance getMarkerPos "BURK_mrk4") < 1};
	
	BURK_dog playMove "Dog_Idle_Stop";
	doStop BURK_dog;	
};

// Actions for following
player addAction ["Dog: Start following player", {[] call BURK_dogFollowPlayer;}];
player addAction ["Dog: Stop following", {[] call BURK_dogStopFollowing;}];
player addAction ["Dog: Follow path (Markers 1 - 2 - 3 - 4)", {[] call BURK_dogFollowPath;}];

// Actions for behaviour override
player addAction ["Dog: Default behaviour", {BURK_dog playMove "Dog_Idle_Stop";}];
player addAction ["Dog: Stop", {BURK_dog playMove "Dog_Stop";}];
player addAction ["Dog: Sit", {BURK_dog playMove "Dog_Sit";}];
player addAction ["Dog: Walk", {BURK_dog playMove "Dog_Walk";}];
player addAction ["Dog: Run", {BURK_dog playMove "Dog_Run";}];
player addAction ["Dog: Sprint", {BURK_dog playMove "Dog_Sprint";}];

Currently available animals

Dog

Available states

Stop:

_dog playMove "Dog_Stop";

Sit:

_dog playMove "Dog_Sit";

Walk:

_dog playMove "Dog_Walk";

Run:

_dog playMove "Dog_Run";

Sprint:

_dog playMove "Dog_Sprint";

Back to default behaviour:

_dog playMove "Dog_Idle_Stop";