How to add a ship to a sobgroup using the function Create(CustomGroup, playerIndex, shipID)

Trying to generate randomized subsystems on ship creation.

The script below properly selects the subsystem to apply, but customGroup is a list and I get a CTD when I use it in the create_subsystem function

Is there any way to specifically get the ship that called the function. I don’t really understand why ShipID is included if we can’t add the ship to a sobgroup specifically using the shipID

what I would need is basically SobGroup_SobGroupAdd(“destination group”, This)

maybe I am missing something?

freightList = {
	"frt_cont_d_blue",
	"frt_cont_d_clean",
	"frt_cont_d_dirty",
	"frt_cont_d_gray",
	"frt_cont_d_green",
	"frt_cont_d_red",
	"frt_cont_d_yellow",	
	"frt_cont_e_gray",
	"frt_cont_e_orange",
	"frt_cont_e_warning",
	"frt_cont_e_white",
	}

function Create_Freight(CustomGroup, playerIndex, shipID)
	slots = 4
	for i=1, slots, 1 do
		local r = random(1,getn(freightList))
		subsystem = freightList[r]
		print(subsystem) --properly picks a subsystem name
		SobGroup_CreateSubSystem(CustomGroup, subsystem) --errors on arg #1
	end
end

strangely enough this script seems to work and always keep 12 miners with the mining ship (except for the docking portion where is spawns at the center of the ship rather than docking , probably due to the same issue above)

function Update_MiningShip(CustomGroup, playerIndex, shipID)
  if SobGroup_Count("miners"..shipID) < 12 then

     SobGroup_Create("miners"..shipID.."temp")

     position = SobGroup_GetPosition(CustomGroup)

     Volume_AddSphere("minespawn"..shipID.."vol", position, 5)

     SobGroup_SpawnNewShipInSobGroup(playerIndex, "vgr_mole_miner", "miners", "miners"..shipID, "minespawn"..shipID.."vol")

     SobGroup_DockSobGroupInstant("miners"..shipID.."temp", CustomGroup) -- this fails

     Volume_Delete("minespawn"..shipID.."vol")

   end

 end

What is passed as “CustomGroup” ? a SobGroup or an array? If it is an array you just need to add a loop to browse it.

A sob group, though I haven’t tested to determine if it is prepopulated with the ship that called it or all instances of the shiptype that calls the function.

I checked my old scripts and I find something like:

assert(SobGroup_CreateSubSystem(sgShips, tSubsystem[i]))

So it seems that SobGroup_CreateSubSystem creates an error when the ship cannot build the subsystem, and assert() is your safety net.

Still getting the same error, assert didn’t seem to help, still got a CTD.
Here is the ship file line for the subs, if I put in a starting subsystem after “Destroyable” the hardpoint works fine, so I don’t think it is in the ship file (the ship can’t build subsystems, I hope that is not a prerequisite for creating them)

StartShipHardPointConfig(NewShipType,"Cargo","Hardpoint_Cargo1","System","Generic","Destroyable","","FRT_CONT_D_BLUE","FRT_CONT_D_CLEAN","FRT_CONT_D_DIRTY","FRT_CONT_D_GRAY","FRT_CONT_D_GREEN","FRT_CONT_D_RED","FRT_CONT_D_YELLOW","FRT_CONT_E_GRAY","FRT_CONT_E_ORANGE","FRT_CONT_E_WARNING","FRT_CONT_E_WHITE")

I did get my mining ship to spawn miners automatically and keep a set number. The instant dock doesn’t work. I can’t figure out a way to keep them from ranging too far with the resourcing, it looks like it will try to resource anywhere on the map. I cannot find a function that will keep them close by if their destination is too far away, only a limit to their distance from the parent ship. The other really odd thing is that the last print statement in the create function shows zero members of the thisship… sobgroup, but when execute that in the update it now shows a population of one.

The strange thing is when I tostring(CustomGroup) it will come back with the ship type, but it doesn’t interact outside of the scope of the current ship as far as I can tell. Each ship performs its function properly and doesn’t overlap.

function Create_MiningShip(CustomGroup, playerIndex, shipID)
--debug
print("This ship is "..shipID.." get ship ID is "..GetShipId("vgr_mining_ship"))
print("Starting ships in mining group :"..SobGroup_Count(CustomGroup))
--create our group of miners for this ship and clear it
SobGroup_CreateIfNotExist("miners"..shipID)
SobGroup_Clear("miners"..shipID)

--create a special group to find all player mining ships
SobGroup_CreateIfNotExist("AllThisShips"..shipID)
SobGroup_Clear("AllThisShips"..shipID)
SobGroup_FillShipsByType("AllThisShips"..shipID, "Player_Ships"..playerIndex, "vgr_mining_ship" )

--create a sobgroup to isolate just this ship
SobGroup_CreateIfNotExist("ThisShip"..shipID)
SobGroup_Clear("ThisShip"..shipID)

--locate the custom group and add it to the ThisShip group
position = SobGroup_GetPosition(CustomGroup)
print("This Ship Created at "..tostring(position[1])..":"..tostring(position[2])..":"..tostring(position[3]))
Volume_AddSphere("findship"..shipID.."vol", position, 5)
SobGroup_FillSobGroupInVolume("ThisShip"..shipID, "AllThisShips"..shipID, "findship"..shipID.."vol")
Volume_Delete("findship"..shipID.."vol")

--add us back to the custom group
SobGroup_SobGroupAdd(CustomGroup, "ThisShip"..shipID)

print("Ships in mining group after mods :"..SobGroup_Count(CustomGroup))

--if custom group has extra crap in it then we can create CustomGroup..shipID
end

function Update_MiningShip(CustomGroup, playerIndex, shipID)
	if SobGroup_Count("miners"..shipID) < 12 then
		SobGroup_Create("miners"..shipID.."temp")
		SobGroup_Clear("miners"..shipID.."temp")
		print("ships in mining group :"..SobGroup_Count(CustomGroup).." parent; "..SobGroup_Count("miners"..shipID).." Children")
		position = SobGroup_GetPosition(CustomGroup)
		Volume_AddSphere("minespawn"..shipID.."vol", position, 5)
		SobGroup_SpawnNewShipInSobGroup(playerIndex, "vgr_mole_miner", "miners", "miners"..shipID, "minespawn"..shipID.."vol")
		SobGroup_DockSobGroupInstant("miners"..shipID.."temp", "ThisShip"..shipID)
		Volume_Delete("minespawn"..shipID.."vol")
		SobGroup_SobGroupAdd("miners"..shipID, "miners"..shipID.."temp")
		SobGroup_ParadeSobGroup("miners"..shipID.."temp", CustomGroup, 0)
		SobGroup_Resource(playerIndex,"miners"..shipID)
	end
end

Is there a function to get the number and type of subsystems on a ship? I remember that not being possible in HW2.