Hopefully somebody is more Lua than me....

The place to discuss scripting and game modifications for X Rebirth.

Moderators: Scripting / Modding Moderators, Moderators for English X Forum

Post Reply
Dev disco
Posts: 33
Joined: Wed, 2. Dec 20, 16:35

Hopefully somebody is more Lua than me....

Post by Dev disco » Thu, 25. Mar 21, 10:42

I find it very annoying that the ships in the trade lists are sorted by area or distance from you.
You keep cycling for a particular ship while the order changes when you're at it.
So I wondered if it's possible to add a alphabetical sorting to this function:

Code: Select all

function menu.getShipList()
	if menu.mode == "wareexchange" then
		menu.ships = { GetTradeShipData(menu.modeparam[1]) }
	else
		menu.ships = GetTradeShipList()
		for i = #menu.ships, 1, -1 do
			local ship = menu.ships[i]
			local commander = GetCommander(ship.shipid)
			if IsSameComponent(ship.shipid, menu.playership) then
				table.remove(menu.ships, i)
			elseif commander and not IsSameComponent(commander, menu.playership) then
				table.remove(menu.ships, i)
			elseif GetBuildAnchor(ship.shipid) then
				table.remove(menu.ships, i)
			elseif #GetTransportUnitMacros(GetComponentData(ship.shipid, "macro")) == 0 then
				table.remove(menu.ships, i)
			elseif (menu.mode == "deals") and (GetComponentData(ship.shipid, "primarypurpose") == "build") then
				table.remove(menu.ships, i)
			end
		end
	end
end
I can't find anything about GetTradeShipList, unfortunately. That would be a better target, I think.

barra
Posts: 25
Joined: Tue, 31. Jan 17, 07:58
x4

Re: Hopefully somebody is more Lua than me....

Post by barra » Tue, 27. Apr 21, 14:03

Im a newbie in LUA too, as I understood it, you could "just" reorder the "menu.ships" Table after it was filled by "GetTradeShipList()" and before it goes to the add to list iterator?!
Probably a bit dirty and should by done direct when iterating, but hey we are new and your CPU will surely survive this lil extra step.

In regards on how to sort lua tables, i found this:

http://www.lua.org/pil/19.3.html

Code: Select all

 		a = {}
   	        for n in pairs(menu.ships) do table.insert(a, n) end
                table.sort(a)
              
this one should be interesting too:
https://stackoverflow.com/questions/157 ... ble-in-lua





Im not sure about the syntax beeing fine, but it may look similar to this:

Code: Select all

function menu.getShipList()
	if menu.mode == "wareexchange" then
		menu.ships = { GetTradeShipData(menu.modeparam[1]) }
		
	else
		menu.ships = GetTradeShipList()
 		a = {}
   	        for n in pairs(menu.ships) do table.insert(a, n) end
                table.sort(a)
                b = {}
                for i,n in ipairs(a) do table.insert(b, n)
                menu.ships = b()
		
		for i = #menu.ships, 1, -1 do
			local ship = menu.ships[i]
			local commander = GetCommander(ship.shipid)
			if IsSameComponent(ship.shipid, menu.playership) then
				table.remove(menu.ships, i)
			elseif commander and not IsSameComponent(commander, menu.playership) then
				table.remove(menu.ships, i)
			elseif GetBuildAnchor(ship.shipid) then
				table.remove(menu.ships, i)
			elseif #GetTransportUnitMacros(GetComponentData(ship.shipid, "macro")) == 0 then
				table.remove(menu.ships, i)
			elseif (menu.mode == "deals") and (GetComponentData(ship.shipid, "primarypurpose") == "build") then
				table.remove(menu.ships, i)
			end
		end
	end
end
I dont know if this will do the trick, but this should be a good starting point?!

Post Reply

Return to “X Rebirth - Scripts and Modding”