createUnit: Difference between revisions
Jump to navigation
Jump to search
Lou Montana (talk | contribs) m (re-add "FORM", for doc and potential fix purpose) |
m (Warning) |
||
(2 intermediate revisions by one other user not shown) | |||
Line 54: | Line 54: | ||
{{Feature|arma1| | {{Feature|arma1| | ||
{{GVI|ofp|1.34}} {{GVI|arma1|1.00}} This command | {{GVI|ofp|1.34}} {{GVI|arma1|1.00}} This command was sometimes bugged in {{ofp}} or {{arma1}}; an additional [[join]] may solve the problem.<br> | ||
However, some commands such as [[setUnitPos]] only work if run before the [[join]]. | However, some commands such as [[setUnitPos]] only work if run before the [[join]]. | ||
}} | }} | ||
Line 97: | Line 97: | ||
|p23= group: [[Group]] - existing group the new unit will join | |p23= group: [[Group]] - existing group the new unit will join | ||
|p24= init: [[String]] - (Optional, default "") unit init statement, similar to unit init field in the editor. The code placed in unit init will run upon unit creation for every client on network, present and future. The code itself receives the reference to the created unit via local variable [[Magic Variables#this_2|this]]. Do not use global commands in a unit's ''init'' as it runs on every client. | |p24= init: [[String]] - (Optional, default "") unit init statement, similar to unit init field in the editor. The code placed in unit init will run upon unit creation for every client on network, present and future. | ||
The code itself receives the reference to the created unit via local variable [[Magic Variables#this_2|this]]. | |||
{{Feature|warning| | |||
* Do not use global effect commands in a unit's ''init'' as it runs on every client (local effect ones are OK, e.g [[setIdentity]]). | |||
* Do not use local variables inside the init code as they are not available in multiplayer.}} | |||
|p25= skill: [[Number]] - (Optional, default 0.5) unit [[skill]] | |p25= skill: [[Number]] - (Optional, default 0.5) unit [[skill]] | ||
Line 105: | Line 110: | ||
|r2= {{Feature|warning|'''[[Nothing]]''' - this syntax does '''not''' return a unit reference! See {{Link|#Example 7}} for a workaround.}} | |r2= {{Feature|warning|'''[[Nothing]]''' - this syntax does '''not''' return a unit reference! See {{Link|#Example 7}} for a workaround.}} | ||
|x1= <sqf>_unit = group player createUnit ["B_RangeMaster_F", position player, [], 0, " | |x1= <sqf>_unit = group player createUnit ["B_RangeMaster_F", position player, [], 0, "NONE"];</sqf> | ||
|x2= <sqf>"B_RangeMaster_F" createUnit [position player, group player];</sqf> | |x2= <sqf>"B_RangeMaster_F" createUnit [position player, group player];</sqf> | ||
Line 132: | Line 137: | ||
_grp = createGroup east; | _grp = createGroup east; | ||
hint str side _grp; // EAST | hint str side _grp; // EAST | ||
_ap = _grp createUnit ["C_man_p_beggar_F", position player, [], 0, " | _ap = _grp createUnit ["C_man_p_beggar_F", position player, [], 0, "NONE"]; | ||
hint str side _ap; // CIV, not EAST | hint str side _ap; // CIV, not EAST | ||
Line 143: | Line 148: | ||
Reference the created unit through a global variable: | Reference the created unit through a global variable: | ||
<sqf> | <sqf> | ||
_myUnit = "B_RangeMaster_F" createUnit [position player, group player]; | _myUnit = "B_RangeMaster_F" createUnit [position player, group player]; // wrong - this syntax does not return a reference | ||
"B_RangeMaster_F" createUnit [position player, group player, "myUnit = this"]; // correct - the unit is myUnit | "B_RangeMaster_F" createUnit [position player, group player, "myUnit = this"]; // correct - the unit is myUnit (NOT _myUnit!) | ||
</sqf> | </sqf> | ||
Line 168: | Line 173: | ||
|user= Killzone_Kid | |user= Killzone_Kid | ||
|timestamp= 20190318193100 | |timestamp= 20190318193100 | ||
|text= Alt Syntax is the older syntax and differs in functionality from the main, newer syntax. The main difference is that the older syntax '''does not''' return unit reference. This is because the unit created with Alt Syntax is created strictly where passed [[group]] is [[Multiplayer Scripting#Locality|local]]. This means that if the group is remote the unit will be created on the different client than the one the command was executed on and therefore it is not possible to return created unit reference immediately. In contrast, the newer syntax allows creating units in remote groups while returning unit reference immediately, which could be unsafe and the appropriate warning is logged into ''.rpt'' file: {{hl|Warning: Adding units to a remote group is not safe. Please use [[setGroupOwner]] to change the group owner first.}}<br><br> | |text= Alt Syntax is the older syntax and differs in functionality from the main, newer syntax. The main difference is that the older syntax '''does not''' return unit reference. | ||
Another very important difference is that the older syntax (Alt Syntax) will create unit of the same [[side]] as the side of the [[group]] passed as argument. For example, if the group is WEST and the unit faction is OPFOR of type, say {{hl|"O_Soldier_GL_F"}}, the unit created will be on the WEST side as well. In contrast, newer syntax will create the same unit on the EAST side in the WEST group, which will be treated as hostile by other group members and eliminated. | This is because the unit created with Alt Syntax is created strictly where passed [[group]] is [[Multiplayer Scripting#Locality|local]]. | ||
{{Feature|warning|Beware that in MP if unit is created in remote group with older syntax, the unit init will execute on calling client sometime in the future, after the unit is created on remote client, therefore the following code will fail: | This means that if the group is remote the unit will be created on the different client than the one the command was executed on and therefore it is not possible to return created unit reference immediately. | ||
In contrast, the newer syntax allows creating units in remote groups while returning unit reference immediately, which could be unsafe and the appropriate warning is logged into ''.rpt'' file: {{hl|Warning: Adding units to a remote group is not safe. Please use [[setGroupOwner]] to change the group owner first.}}<br><br> | |||
Another very important difference is that the older syntax (Alt Syntax) will create unit of the same [[side]] as the side of the [[group]] passed as argument. | |||
For example, if the group is WEST and the unit faction is OPFOR of type, say {{hl|"O_Soldier_GL_F"}}, the unit created will be on the WEST side as well. | |||
In contrast, newer syntax will create the same unit on the EAST side in the WEST group, which will be treated as hostile by other group members and eliminated. | |||
{{Feature|warning| | |||
Beware that in MP if unit is created in remote group with older syntax, the unit init will execute on calling client sometime in the future, after the unit is created on remote client, therefore the following code will fail: | |||
<sqf> | <sqf> | ||
// real example of the bad code | // real example of the bad code | ||
Line 184: | Line 195: | ||
|user= fusselwurm | |user= fusselwurm | ||
|timestamp= 20200619101300 | |timestamp= 20200619101300 | ||
|text= {{GVI|arma3|1.98}} note that even when setting the ''placement special'' parameter to "NONE", 3DEN-placed objects are being ignored when looking for a free position. In other words: units will spawn within editor-placed rocks or under houses. | |text= {{GVI|arma3|1.98}} note that even when setting the ''placement special'' parameter to "NONE", 3DEN-placed objects are being ignored when looking for a free position. | ||
In other words: units will spawn within editor-placed rocks or under houses. | |||
}} | }} |
Latest revision as of 09:27, 22 October 2024
Description
- Description:
- Create a unit of the provided CfgVehicles class.
Syntax 1 Syntax 2 Group's locality the provided group can be non-local, but a warning will be logged the provided group must be local Unit's side using a classname from a different side than the provided group will result in the unit itself being of a (config-defined) side inside a group of another side - see Example 6 for more information using a classname from a different side than the provided group will result in the unit being of the same side as the provided group Other the unit's init code will execute after a slight delay if the provided group is not local this syntax does not return a reference to the created unit (see Example 7) - Multiplayer:
- It is recommended to create the unit where the group is local - use remoteExec if needed.
- Groups:
- Object Manipulation
Syntax
- Syntax:
- group createUnit [type, position, markers, placement, special]
- Parameters:
- group: Group - existing group new unit will join; if the group is not local, a warning will be logged
- type: String - classname of unit to be created as per CfgVehicles
- position: Object, Group or Array format Position or Position2D - location where to create the unit. In case of Group, the group leader's position is used
- markers: Array - placement markers
- placement: Number - placement radius
- special: String - unit placement special, one of:
- "NONE" - The unit will be created at the first available free position nearest to given position
- "FORM" - Not implemented, currently functions the same as "NONE"
- "CAN_COLLIDE" - The unit will be created exactly at the provided position
- "CARGO" - The unit will be created in cargo of the group's vehicle, regardless of the passed position (see Example 5). If group has no vehicle or there is no cargo space available, the unit will be placed according to "NONE". "CARGO" placement excludes cargo positions with personal FFV turrets. To check available cargo space use:
1.34 FFV before - Return Value:
- Object - the created unit
Alternative Syntax
- Syntax:
- type createUnit [position, group, init, skill, rank]
- Parameters:
- type: String - classname of unit to be created as per CfgVehicles
- position: Object, Group or Array format Position or Position2D - location at which the unit is created. In case of Group position of the group leader is used
- group: Group - existing group the new unit will join
- init: String - (Optional, default "") unit init statement, similar to unit init field in the editor. The code placed in unit init will run upon unit creation for every client on network, present and future. The code itself receives the reference to the created unit via local variable this.
- skill: Number - (Optional, default 0.5) unit skill
- rank: String - (Optional, default "PRIVATE") unit rank
- Return Value:
Examples
- Example 1:
- Example 2:
- Example 3:
- Example 4:
- "B_RangeMaster_F" createUnit [ getMarkerPos "marker_1", _groupAlpha, "loon1 = this; this addWeapon 'BAF_L85A2_RIS_SUSAT'", 0.6, "corporal" ];
- Example 5:
- _veh = "O_Quadbike_01_F" createVehicle (player getRelPos [10, 0]); _grp = createVehicleCrew _veh; _unit = _grp createUnit [typeOf driver _veh, _grp, [], 0, "CARGO"];
- Example 6:
- Creating a unit from a different side may lead to issues:
- Example 7:
- Reference the created unit through a global variable:
Additional Information
Notes
-
Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
- Posted on Dec 08, 2018 - 21:57 (UTC)
- Since 1.86: If you want to place a module with createUnit, you have to ensure that the module gets activated automatically by setting BIS_fnc_initModules_disableAutoActivation to false, e.g:
- Posted on Mar 18, 2019 - 19:31 (UTC)
-
Alt Syntax is the older syntax and differs in functionality from the main, newer syntax. The main difference is that the older syntax does not return unit reference.
This is because the unit created with Alt Syntax is created strictly where passed group is local.
This means that if the group is remote the unit will be created on the different client than the one the command was executed on and therefore it is not possible to return created unit reference immediately.
In contrast, the newer syntax allows creating units in remote groups while returning unit reference immediately, which could be unsafe and the appropriate warning is logged into .rpt file: Warning: Adding units to a remote group is not safe. Please use setGroupOwner to change the group owner first.
Another very important difference is that the older syntax (Alt Syntax) will create unit of the same side as the side of the group passed as argument. For example, if the group is WEST and the unit faction is OPFOR of type, say "O_Soldier_GL_F", the unit created will be on the WEST side as well. In contrast, newer syntax will create the same unit on the EAST side in the WEST group, which will be treated as hostile by other group members and eliminated.
- Posted on Jun 19, 2020 - 10:13 (UTC)
- 1.98 note that even when setting the placement special parameter to "NONE", 3DEN-placed objects are being ignored when looking for a free position. In other words: units will spawn within editor-placed rocks or under houses.
Categories:
- Scripting Commands
- Introduced with Operation Flashpoint version 1.34
- Operation Flashpoint: New Scripting Commands
- Operation Flashpoint: Scripting Commands
- Operation Flashpoint: Elite: Scripting Commands
- ArmA: Armed Assault: Scripting Commands
- Arma 2: Scripting Commands
- Arma 2: Operation Arrowhead: Scripting Commands
- Take On Helicopters: Scripting Commands
- Arma 3: Scripting Commands
- Command Group: Object Manipulation
- Scripting Commands: Global Effect