Animals: Override Default Animal Behaviour Via Script – Arma 3

From Bohemia Interactive Community
Jump to navigation Jump to search
m (removed note about version)
(Wiki formatting)
Line 1: Line 1:
[[Category:Arma 3: Editing]]
{{SideTOC}}
[[Category:Arma 3: Tutorials]]
----
 
== Introduction ==
== 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.
Animals in {{arma3}} roam freely according to their [[FSM]]; they autonomously walk, stop, eat, flee etc.
In order to control them fully you will need to override their default behaviour and manually guide them, all with a bit of scripting.


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 ==
== Animal creation ==


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.
=== createAgent command ===


=== Module Animals from Sites category ===
Spawning animals ''via'' [[createAgent]] is the best way how to have control over them in your mission.<br>
An '''agent''' is a "light AI" that doesn't have all the abilities/intelligence of a "normal" AI unit created ''via'' [[createUnit]];
therefore, they '''cannot''' be controlled via commands such as [[doMove]] and [[doStop]]. However, low-level commands such as [[moveTo]] or [[setDestination]] will work.


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.
For a basic example see [[#Example script|Example script]].


=== Spawn via [[createAgent|createAgent]] command ===
<div style="float: right; margin-left: 1.5em;">[[File:arma3 animals module in menu.png|150px]]</div><!-- cannot have AND size AND caption -->
=== Animals module ===


Spawning animals via scripting command [[createAgent|createAgent]] is another way how to have animals in your mission. And yes, animals created this way can be controled! As the animal AI is controlled by AIAgent, it won't be able to be controlled via commands such as [[doMove|doMove]] and [[doStop|doStop]] and will roam randomly around. '''But agents can be controlled via [[moveTo|moveTo]] command'''. And you can also determine, in which state (like run, sprint, etc.) the animal will move.
The Animals module can be found in [[Eden Editor]] in the Modules page (F5), under the Sites category.<br>


'''Example:''' <syntaxhighlight lang="cpp">_dog = createAgent ["Fin_random_F", getPos player, [], 5, "CAN_COLLIDE"];</syntaxhighlight>
Insert the module in editor, set the desired parameters and you have animals in your mission.<br>
Spawned animals will be for mission decoration purpose instead of heavily-scripted behaviour.


=== Spawn via [[createUnit|createUnit]] command ===


{{note|'''WARNING: If you will group animal created this way with another unit such as soldier, the animal will also be able to communicate via radio messages (eg. "Cease fire" etc.). Do not use unless you know, what you are doing!'''}}
== Disable animal's scripted behaviour ==


Another way to spawn animal is using [[createUnit|createUnit]] command. 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. '''You can command the animal via [[doMove|doMove]] and [[doStop|doStop]] commands''', so you will be able to control it in its movement, but still, be aware of the warning above. Best way to spawn script controllable animals is [[createAgent|createAgent]] command.
You can disable [[BIS_fnc_animalBehaviour|animal's scripted behaviour]] using the following:
_animal [[setVariable]] ["BIS_fnc_animalBehaviour_disable", [[true]]];


'''Example''':
<syntaxhighlight lang="cpp">_grp = createGroup CIVILIAN;
_dog = _grp createUnit ["Fin_random_F", getPos player, [], 5, "CAN_COLLIDE"];</syntaxhighlight>


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


'''Example:''' <syntaxhighlight lang="cpp">_dog = group player createUnit ["Fin_random_F", getPos player, [], 5, "CAN_COLLIDE"];</syntaxhighlight>
Animations can be played using [[playMove]], [[playMoveNow]] or [[switchMove]]. Their names are quite self-explanatory.
{{ Important | Animals will remain in the same animation cycle until manually changed! }}


== Usage ==
=== Dog ===


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 [[#Currently available animals|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|BIS_fnc_animalBehaviour_disable]] variable!
_dog [[playMove]] "Dog_Stop";
_dog [[playMove]] "Dog_Sit";
_dog [[playMove]] "Dog_Walk";
_dog [[playMove]] "Dog_Run";
_dog [[playMove]] "Dog_Sprint";
_dog [[playMove]] "Dog_Idle_Stop"; {{codecomment|// wandering, default behaviour}}


=== Example mission for dog  ===
=== Sheep ===


You can try following mission, just '''[https://mega.nz/#!4otnwA7T!R4HL77l_9Qwc_-3EgXZ95LqtV9g-W3ALXwru3JjQ6NQ simply download ZIP archive with mission here]'''. All dog actions are controllable via action menu. See [https://www.youtube.com/watch?v=0X8oumNj9kc this video] captured from this example mission.
_sheep [[playMove]] "Sheep_Stop";
_sheep [[playMove]] "Sheep_Walk";
_sheep [[playMove]] "Sheep_Run";
_sheep [[playMove]] "Sheep_Idle_Stop"; {{codecomment|// wandering, default behaviour}}


=== Example script for dog following player ===
=== Goat ===


<syntaxhighlight lang="cpp">// Spawn dog
_goat [[playMove]] "Goat_Stop";
_dog = createAgent ["Fin_random_F", getPos player, [], 5, "CAN_COLLIDE"];
_goat [[playMove]] "Goat_Walk";
_goat [[playMove]] "Goat_Run";
_goat [[playMove]] "Goat_Idle_Stop"; {{codecomment|// wandering, default behaviour}}


// Disable animal behaviour
=== Rabbit ===
_dog setVariable ["BIS_fnc_animalBehaviour_disable", true];


// Following loop
_rabbit [[playMove]] "Rabbit_Stop";
0 = [_dog] spawn {
_rabbit [[playMove]] "Rabbit_Hop";
params ["_dog"];
_rabbit [[playMove]] "Rabbit_Idle_Stop"; {{codecomment|// wandering, default behaviour}}


// Force dog to sprint
=== Cockerel ===
_dog playMove "Dog_Sprint";
while {alive _dog} do
{
_dog moveTo getPos player;


sleep 0.5;
_cock [[playMove]] "Cock_Stop";
};
_cock [[playMove]] "Cock_Walk";
};</syntaxhighlight>
_cock [[playMove]] "Cock_Run";
_cock [[playMove]] "Cock_Idle_Stop"; {{codecomment|// wandering, default behaviour}}


== Currently available animals ==
=== Hen ===


=== Dog ===
_hen [[playMove]] "Hen_Stop";
_hen [[playMove]] "Hen_Walk";
_hen [[playMove]] "Hen_Idle_Stop"; {{codecomment|// wandering, default behaviour}}


'''Stop:''' <syntaxhighlight lang="cpp">_dog playMove "Dog_Stop";</syntaxhighlight>
=== Snake ===
'''Sit:''' <syntaxhighlight lang="cpp">_dog playMove "Dog_Sit";</syntaxhighlight>
'''Walk:''' <syntaxhighlight lang="cpp">_dog playMove "Dog_Walk";</syntaxhighlight>
'''Run:''' <syntaxhighlight lang="cpp">_dog playMove "Dog_Run";</syntaxhighlight>
'''Sprint:''' <syntaxhighlight lang="cpp">_dog playMove "Dog_Sprint";</syntaxhighlight>
'''Back to default behaviour:''' <syntaxhighlight lang="cpp">_dog playMove "Dog_Idle_Stop";</syntaxhighlight>


=== Sheep ===
_snake [[playMove]] "Snakes_Stop";
_snake [[playMove]] "Snakes_Move";
_snake [[playMove]] "Snakes_Idle_Stop"; {{codecomment|// wandering, default behaviour}}


'''Stop:''' <syntaxhighlight lang="cpp">_sheep playMove "Sheep_Stop";</syntaxhighlight>
'''Walk:''' <syntaxhighlight lang="cpp">_sheep playMove "Sheep_Walk";</syntaxhighlight>
'''Run:''' <syntaxhighlight lang="cpp">_sheep playMove "Sheep_Run";</syntaxhighlight>
'''Back to default behaviour:''' <syntaxhighlight lang="cpp">_sheep playMove "Sheep_Idle_Stop";</syntaxhighlight>


=== Goat ===
== Example script ==
 
'''Stop:''' <syntaxhighlight lang="cpp">_goat playMove "Goat_Stop";</syntaxhighlight>
'''Walk:''' <syntaxhighlight lang="cpp">_goat playMove "Goat_Walk";</syntaxhighlight>
'''Run:''' <syntaxhighlight lang="cpp">_goat playMove "Goat_Run";</syntaxhighlight>
'''Back to default behaviour:''' <syntaxhighlight lang="cpp">_goat playMove "Goat_Idle_Stop";</syntaxhighlight>
 
=== Rabbit ===


'''Stop:''' <syntaxhighlight lang="cpp">_rabbit playMove "Rabbit_Stop";</syntaxhighlight>
Simple example for a dog to follow the player:
'''Hop:''' <syntaxhighlight lang="cpp">_rabbit playMove "Rabbit_Hop";</syntaxhighlight>
'''Back to default behaviour:''' <syntaxhighlight lang="cpp">_rabbit playMove "Rabbit_Idle_Stop";</syntaxhighlight>


=== Cockerel ===
{{codecomment|// Spawn dog}}
_dog = [[createAgent]] ["Fin_random_F", [[getPosATL]] [[player]], [], 5, "NONE"];
{{codecomment|// Disable animal behaviour}}
_dog [[setVariable]] ["BIS_fnc_animalBehaviour_disable", [[true]]];
{{codecomment|// Following loop}}
[_dog] [[spawn]] {
[[params]] ["_dog"];
{{codecomment|// Force dog to sprint}}
_dog [[playMove]] "Dog_Sprint";
[[while]] { [[sleep]] 1; [[alive]] _dog } [[do]]
{
_dog [[moveTo]] [[getPosATL]] [[player]];
};
};


'''Stop:''' <syntaxhighlight lang="cpp">_cock playMove "Cock_Stop";</syntaxhighlight>
'''Walk:''' <syntaxhighlight lang="cpp">_cock playMove "Cock_Walk";</syntaxhighlight>
'''Run:''' <syntaxhighlight lang="cpp">_cock playMove "Cock_Run";</syntaxhighlight>
'''Back to default behaviour:''' <syntaxhighlight lang="cpp">_cock playMove "Cock_Idle_Stop";</syntaxhighlight>


=== Hen ===
== Dog example mission ==


'''Stop:''' <syntaxhighlight lang="cpp">_hen playMove "Hen_Stop";</syntaxhighlight>
* Example mission: [https://mega.nz/#!4otnwA7T!R4HL77l_9Qwc_-3EgXZ95LqtV9g-W3ALXwru3JjQ6NQ download link]
'''Walk:''' <syntaxhighlight lang="cpp">_hen playMove "Hen_Walk";</syntaxhighlight>
* YouTube video: [https://www.youtube.com/watch?v=0X8oumNj9kc video link]
'''Back to default behaviour:''' <syntaxhighlight lang="cpp">_hen playMove "Hen_Idle_Stop";</syntaxhighlight>


=== Snake ===


'''Stop:''' <syntaxhighlight lang="cpp">_snake playMove "Snakes_Stop";</syntaxhighlight>
[[Category:Arma 3: Editing]]
'''Move:''' <syntaxhighlight lang="cpp">_snake playMove "Snakes_Move";</syntaxhighlight>
[[Category:Arma 3: Tutorials]]
'''Back to default behaviour:''' <syntaxhighlight lang="cpp">_snake playMove "Snakes_Idle_Stop";</syntaxhighlight>

Revision as of 21:02, 17 July 2019

Template:SideTOC

Introduction

Animals in Arma 3 roam freely according to their FSM; they autonomously walk, stop, eat, flee etc. In order to control them fully you will need to override their default behaviour and manually guide them, all with a bit of scripting.


Animal creation

createAgent command

Spawning animals via createAgent is the best way how to have control over them in your mission.
An agent is a "light AI" that doesn't have all the abilities/intelligence of a "normal" AI unit created via createUnit; therefore, they cannot be controlled via commands such as doMove and doStop. However, low-level commands such as moveTo or setDestination will work.

For a basic example see Example script.

arma3 animals module in menu.png

Animals module

The Animals module can be found in Eden Editor in the Modules page (F5), under the Sites category.

Insert the module in editor, set the desired parameters and you have animals in your mission.
Spawned animals will be for mission decoration purpose instead of heavily-scripted behaviour.


Disable animal's scripted behaviour

You can disable animal's scripted behaviour using the following:

_animal setVariable ["BIS_fnc_animalBehaviour_disable", true];


Animations

Animations can be played using playMove, playMoveNow or switchMove. Their names are quite self-explanatory.

Animals will remain in the same animation cycle until manually changed!

Dog

_dog playMove "Dog_Stop"; 
_dog playMove "Dog_Sit";
_dog playMove "Dog_Walk";
_dog playMove "Dog_Run";
_dog playMove "Dog_Sprint";
_dog playMove "Dog_Idle_Stop"; // wandering, default behaviour

Sheep

_sheep playMove "Sheep_Stop";
_sheep playMove "Sheep_Walk";
_sheep playMove "Sheep_Run";
_sheep playMove "Sheep_Idle_Stop"; // wandering, default behaviour

Goat

_goat playMove "Goat_Stop";
_goat playMove "Goat_Walk";
_goat playMove "Goat_Run";
_goat playMove "Goat_Idle_Stop"; // wandering, default behaviour

Rabbit

_rabbit playMove "Rabbit_Stop";
_rabbit playMove "Rabbit_Hop";
_rabbit playMove "Rabbit_Idle_Stop"; // wandering, default behaviour

Cockerel

_cock playMove "Cock_Stop";
_cock playMove "Cock_Walk";
_cock playMove "Cock_Run";
_cock playMove "Cock_Idle_Stop"; // wandering, default behaviour

Hen

_hen playMove "Hen_Stop";
_hen playMove "Hen_Walk";
_hen playMove "Hen_Idle_Stop"; // wandering, default behaviour

Snake

_snake playMove "Snakes_Stop";
_snake playMove "Snakes_Move";
_snake playMove "Snakes_Idle_Stop"; // wandering, default behaviour


Example script

Simple example for a dog to follow the player:

// Spawn dog
_dog = createAgent ["Fin_random_F", getPosATL player, [], 5, "NONE"];

// Disable animal behaviour
_dog setVariable ["BIS_fnc_animalBehaviour_disable", true];

// Following loop
[_dog] spawn {
	params ["_dog"];

	// Force dog to sprint
	_dog playMove "Dog_Sprint";

	while { sleep 1; alive _dog } do
	{
		_dog moveTo getPosATL player;
	};
};


Dog example mission