createUnit: Difference between revisions
Jump to navigation
Jump to search
Lou Montana (talk | contribs) m (Text replacement - "<tt>([^= ]+)<\/tt>" to "{{hl|$1}}") |
Lou Montana (talk | contribs) m (Some wiki formatting) |
||
Line 29: | Line 29: | ||
|descr= Create a unit of class that is defined in [[:Category:CfgVehicles|CfgVehicles]]. | |descr= Create a unit of class that is defined in [[:Category:CfgVehicles|CfgVehicles]]. | ||
{{Feature | important | | {{Feature | important | {{GVI|ofp|1.34}} {{GVI|arma1|1.00}} This command could be bugged in {{ofp}} or {{arma1}}; an additional [[join]] may solve the problem.<br> | ||
This command could be 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]].}} | ||
{{Feature | | {{Feature | warning | The unit will not be created if the passed group does not exist (a.k.a [[grpNull]]); this can happen if [[createGroup]] fails because the '''group limit has been reached''' (see [[createGroup]] for respective game limits).}} | ||
|mp= It is recommended to create the unit where the group is '''[[Multiplayer Scripting#Locality|local]]''' - use [[remoteExec]] if needed.<br> | |mp= It is recommended to create the unit where the group is '''[[Multiplayer Scripting#Locality|local]]''' - use [[remoteExec]] if needed.<br> | ||
Line 41: | Line 40: | ||
|p1= group: [[Group]] - Existing group new unit will join | |p1= group: [[Group]] - Existing group new unit will join | ||
|p2= [ | |p2= type: [[String]] - Class name of unit to be created as per [[CfgVehicles]] | ||
|p3= | |p3= position: [[Position]], [[Position2D]], [[Object]] or [[Group]] - Location unit is created at. In case of [[Group]] position of the [[group]] [[leader]] is used | ||
|p4= | |p4= markers: [[Array]] - Placement markers | ||
|p5= | |p5= placement: [[Number]] - Placement radius | ||
|p6 | |p6= special: [[String]] - Unit placement special, one of: | ||
* {{hl|"NONE"}} - The unit will be created at the first available free position nearest to given position | * {{hl|"NONE"}} - The unit will be created at the first available free position nearest to given position | ||
* {{hl|"FORM"}} - Not implemented, currently functions the same as "NONE". | * {{hl|"FORM"}} - Not implemented, currently functions the same as "NONE". | ||
Line 63: | Line 60: | ||
|p21= type: [[String]] - Class name of unit to be created as per [[CfgVehicles]] | |p21= type: [[String]] - Class name of unit to be created as per [[CfgVehicles]] | ||
|p22= | |p22= 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 | ||
| | |p23= group: [[Group]] - Existing group 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]]. Avoid using global commands in unit init statement as it already runs on every client. | ||
| | |p25= skill: [[Number]] - (Optional, default 0.5) Unit [[skill]] | ||
| | |p26= rank: [[String]] - (Optional, default "PRIVATE") Unit [[rank]] | ||
|r2= {{Feature|Warning|'''[[Nothing]]''' - This syntax does NOT return a unit reference! In order to reference this unit, | |r2= {{Feature|Warning|'''[[Nothing]]''' - This syntax does NOT return a unit reference! In order to reference this unit, it is possible to use {{ic|newUnit {{=}} this}} in the ''init'' statement.<br> | ||
If the target group is not local, the init script will only execute after a slight delay; see [[User:Killzone_Kid|Killzone_Kid]]'s note below for more information and issues about this syntax.}} | If the target group is not local, the init script will only execute after a slight delay; see [[User:Killzone_Kid|Killzone_Kid]]'s note below for more information and issues about this syntax.}} | ||
Line 99: | Line 94: | ||
}} | }} | ||
{{Note | |||
|user= ffur2007slx2_5 | |||
|timestamp= 20161127073900 | |||
|text= {{GVI|arma3|1.64}} The side of the created unit by this command uses its config's side and not the side of a passed empty group created by [[createGroup]]: | |||
{{GVI|arma3|1.64}} The side of the created unit by this command uses its config's side and not the side of a passed empty group created by [[createGroup]]: | |||
<code>_grp = [[createGroup]] [[east]]; | <code>_grp = [[createGroup]] [[east]]; | ||
[[hint]] [[str]] [[side]] _grp; {{cc|EAST}} | [[hint]] [[str]] [[side]] _grp; {{cc|EAST}} | ||
Line 112: | Line 103: | ||
[[hint]] [[str]] [[side]] _ap; {{cc|CIV, not EAST}}</code> | [[hint]] [[str]] [[side]] _ap; {{cc|CIV, not EAST}}</code> | ||
We can use [[join]] command or fill the _grp with entities in advance to set _ap to the desired side. | We can use [[join]] command or fill the _grp with entities in advance to set _ap to the desired side. | ||
}} | |||
{{Note | |||
|user= oOKexOo | |||
|timestamp= 20181208215700 | |||
|text= Since {{GVI|arma3|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'': | |||
Since {{GVI|arma3|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'': | |||
<code>[[private]] _grp = [[createGroup]] [[sideLogic]]; | <code>[[private]] _grp = [[createGroup]] [[sideLogic]]; | ||
"ModuleSmokeWhite_F" [[createUnit]] [ | "ModuleSmokeWhite_F" [[createUnit]] [ | ||
Line 125: | Line 115: | ||
"[[Magic Variables#this_2|this]] [[setVariable]] ['BIS_fnc_initModules_disableAutoActivation', [[false]], [[true]]];" | "[[Magic Variables#this_2|this]] [[setVariable]] ['BIS_fnc_initModules_disableAutoActivation', [[false]], [[true]]];" | ||
];</code> | ];</code> | ||
}} | }} | ||
< | {{Note | ||
|user= Killzone_Kid | |||
< | |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> | ||
{{ | 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: | ||
<code>{{cc|real example of the bad code}}<nowiki> | |||
</nowiki>"O_Soldier_AR_F" [[createUnit]] [<nowiki/>[[position]] [[player]], someRemoteGroup, "thisUnit {{=}} this"];<nowiki> | |||
</nowiki>[[publicVariable]] "thisUnit";<nowiki> | |||
</nowiki>[[hint]] [[str]] [[isNil]] "thisUnit"; {{cc|[[true]]!}}<nowiki> | |||
</nowiki>{{cc|The unit reference is [[nil]] because init statement has not been executed on this client yet}}</code> | |||
}} | |||
}} | |||
{{Note | |||
|user= fusselwurm | |||
|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. | |||
}} |
Revision as of 10:25, 26 November 2021
Description
- Description:
- Create a unit of class that is defined in CfgVehicles.
- Multiplayer:
- It is recommended to create the unit where the group is local - use remoteExec if needed.
The Alt Syntax requires the group to be local. - Groups:
- Object Manipulation
Syntax
- Syntax:
- group createUnit [type, position, markers, placement, special]
- Parameters:
- group: Group - Existing group new unit will join
- type: String - Class name of unit to be created as per CfgVehicles
- position: Position, Position2D, Object or Group - Location unit is created at. In case of Group position of the group leader 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 passed 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". To check available cargo space use:
_hasCargo = _veh emptyPositions "CARGO" > 0;
- Return Value:
- Object - The created unit
Alternative Syntax
- Syntax:
- type createUnit [position, group, init, skill, rank]
- Parameters:
- type: String - Class name 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 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. Avoid using global commands in unit init statement as it already runs on every client.
- skill: Number - (Optional, default 0.5) Unit skill
- rank: String - (Optional, default "PRIVATE") Unit rank
- Return Value:
Examples
- Example 1:
_unit = group player createUnit ["B_RangeMaster_F", position player, [], 0, "FORM"];
- Example 2:
"B_RangeMaster_F" createUnit [position player, group player];
- Example 3:
"B_RangeMaster_F" createUnit [getMarkerPos "barracks", _groupAlpha];
- 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"];
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 Nov 27, 2016 - 07:39 (UTC)
-
1.64 The side of the created unit by this command uses its config's side and not the side of a passed empty group created by createGroup:
_grp = createGroup east; hint str side _grp; // EAST _ap = _grp createUnit [ "C_man_p_beggar_F", position player, [], 0, "FORM"]; hint str side _ap; // CIV, not EAST
We can use join command or fill the _grp with entities in advance to set _ap to the desired side.
- 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:
private _grp = createGroup sideLogic; "ModuleSmokeWhite_F" createUnit [ getPos player, _grp, "this setVariable ['BIS_fnc_initModules_disableAutoActivation', false, true];" ];
- 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