Jump to content

Minimal Spawn Unit Script?


Slazi

Recommended Posts

Can anyone provide a minimal script to spawn a unit?

 

I've been looking at CCTS for examples, but there are a lot of connections in there to decode. I'm sure I could do it given the time, but if anyone has a simpler version it would help me out a lot.

 

Ideally I want to just spawn a unit next to / near another unit.

Link to comment
Share on other sites

Taken from the "Spawn HMMWV (Lua Interaction)" example of DCS Witchcraft:

 


local unitsTable = {
	["visible"] = false,
	["taskSelected"] = true,
	["route"] = 
	{
		["spans"] = 
		{
		}, -- end of ["spans"]
		["points"] = 
		{
			[1] = 
			{
				["alt"] = event.vec3.y,
				["type"] = "Turning Point",
				["ETA"] = 0,
				["alt_type"] = "BARO",
				["formation_template"] = "",
				["y"] = event.vec2.y,
				["x"] = event.vec2.x,
				["ETA_locked"] = true,
				["speed"] = 5.5555555555556,
				["action"] = "Off Road",
				["task"] = 
				{
					["id"] = "ComboTask",
					["params"] = 
					{
						["tasks"] = 
						{
							[1] = 
							{
								["enabled"] = true,
								["auto"] = true,
								["id"] = "WrappedAction",
								["number"] = 1,
								["params"] = 
								{
									["action"] = 
									{
										["id"] = "EPLRS",
										["params"] = 
										{
											["value"] = true,
											["groupId"] = 2,
										}, -- end of ["params"]
									}, -- end of ["action"]
								}, -- end of ["params"]
							}, -- end of [1]
						}, -- end of ["tasks"]
					}, -- end of ["params"]
				}, -- end of ["task"]
				["speed_locked"] = true,
			}, -- end of [1]
		}, -- end of ["points"]
	}, -- end of ["route"]
	["groupId"] = 30,
	["tasks"] = 
	{
	}, -- end of ["tasks"]
	["hidden"] = false,
	["units"] = 
	{
		[1] = 
		{
			["y"] = event.vec2.y,
			["type"] = "M1043 HMMWV Armament",
			["name"] = "cursor_unit",
			["unitId"] = 30,
			["heading"] = witchcraft.heading,
			["playerCanDrive"] = true,
			["skill"] = "Average",
			["x"] = event.vec2.x,
		}, -- end of [1]
	}, -- end of ["units"]
	["y"] = event.vec2.y,
	["x"] = event.vec2.x,
	["name"] = "CursorGroup",
	["start_time"] = 0,
	["task"] = "Ground Nothing",
}

unitsTable.country = "USA"
unitsTable.category = "vehicle"

if copy then
	unitsTable.groupId = nil
	unitsTable.name = nil
	unitsTable.units[1].unitId = nil
	unitsTable.units[1].name = nil
end

mist.dynAdd(unitsTable)

 

See also: mist.dynAdd documentation

 

In this case:

event.vec2 is the position where the HMMWV will be spawned

witchcraft.heading is the initial heading

 

It's not a minimal example, but it comes close. The general idea is that you provide a table like the mission editor would generate and then add country and category attributes before calling mist.dynAdd.

 

If you provide a group ID or name, the spawned group will replace an already existing group with the same identifier.

 

If you want to spawn the unit relative to another one, use someting like

local spawn_vec3 = Group.getByName("SomeGroup"):getUnit(1):getPosition()
local spawn_vec2 = { x = spawn_vec3.x, y = spawn_vec3.z }

 

Shameless plug: You may want to install DCS Witchcraft, which provides a Lua debug console. Select the "Spawn HMMWV" template, then modify it to suit your needs. If you write custom Lua scripts, the 15 minutes setting up DCS Witchcraft will save you a lot of debugging time down the road because if a code snippet does not work, you can fix the error and try it seconds later without having to reload the mission.


Edited by [FSF]Ian
  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

[FSF]Ian,

 

Can you explain the purpose of this batch of code?

 

if copy then

unitsTable.groupId = nil

unitsTable.name = nil

unitsTable.units[1].unitId = nil

unitsTable.units[1].name = nil

end

314-я смешанная авиационная дивизия

314th Mixed Aviation Division: The "Fighting Lemmings"- Forums: http://314thsquadron.enjin.com/ - ED Forum Group: http://forums.eagle.ru/group.php?groupid=119

Link to comment
Share on other sites

In this case (the "spawn HMMWV" example snippet), the "copy" variable holds a boolean that determines if the (existing) group should be moved or a copy should be created. If you look at the documentation of mist.dynAdd, you will see that it will create a new group if you pass an empty group name and group ID.

  • Like 1
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...