Um... I'm having trouble with something that's so simple, it's annoying. I just want to exactly copy the orders of one unit to another, sans the "tag".
So, I've written the code like this, where u == the unitID of the unit I want to copy from, and k == the unitID of the unit I want to copy to:
local cmds = Spring.GetUnitCommands(u)
Spring.GiveOrderArrayToUnitMap(k,cmds)
... isn't it supposed to be that simple? Or am I missing something here? Sorry in advance, I can't seem to find an example that actually works when I use it.
Cloning a Unit's Order Queue
Moderator: Moderators
Re: Cloning a Unit's Order Queue
For a start unitmap takes a table as the first arg, tried
local cmds = Spring.GetUnitCommands(u)
Spring.GiveOrderArrayToUnitMap({k},cmds)
?
That might be complete rubbish, its 4.45am
local cmds = Spring.GetUnitCommands(u)
Spring.GiveOrderArrayToUnitMap({k},cmds)
?
That might be complete rubbish, its 4.45am

Re: Cloning a Unit's Order Queue
Meh, that didn't work, either... hmm...
Re: Cloning a Unit's Order Queue
Code: Select all
--copy command queue
local cmds = Spring.GetUnitCommands(unitID)
for i = 2, cmds.n do -- skip the first command (CMD_MORPH)
local cmd = cmds[i]
Spring.GiveOrderToUnit(newUnit, cmd.id, cmd.params, cmd.options.coded)
end
Re: Cloning a Unit's Order Queue
Yeah, I tried that earlier, it didn't work, either. Something must be screwy about how I'm going about this, lemme take another look.
- TheFatController
- Balanced Annihilation Developer
- Posts: 1177
- Joined: 10 Dec 2006, 18:46
Re: Cloning a Unit's Order Queue
Here's mine from a Gadget i haven't released, should work out of context (target should be a unit with no orders)
Code: Select all
local function copyCommandQueue(targetID, unitID)
local states = Spring.GetUnitStates(targetID)
if (states ~= nil) then
Spring.GiveOrderToUnit(unitID, CMD.FIRE_STATE, { states.firestate }, 0)
Spring.GiveOrderToUnit(unitID, CMD.MOVE_STATE, { states.movestate }, 0)
Spring.GiveOrderToUnit(unitID, CMD.REPEAT, { states['repeat'] and 1 or 0 }, 0)
Spring.GiveOrderToUnit(unitID, CMD.ONOFF, { states.active and 1 or 0 }, 0)
Spring.GiveOrderToUnit(unitID, CMD.CLOAK, { states.cloak and 1 or 0 }, 0)
Spring.GiveOrderToUnit(unitID, CMD.TRAJECTORY, { states.trajectory and 1 or 0 }, 0)
end
local queue = Spring.GetCommandQueue(targetID);
if (queue ~= nil) then
for k,v in ipairs(queue) do -- in order
local opts = v.options
if (not opts.internal) then
local newopts = {}
if (opts.alt) then table.insert(newopts, "alt") end
if (opts.ctrl) then table.insert(newopts, "ctrl") end
if (opts.right) then table.insert(newopts, "right") end
table.insert(newopts, "shift")
Spring.GiveOrderToUnit(unitID, v.id, v.params, opts.coded)
end
end
end
local buildqueue = Spring.GetRealBuildQueue(targetID)
if (buildqueue ~= nil) then
for udid,buildPair in ipairs(buildqueue) do
local udid, count = next(buildPair, nil)
Spring.AddBuildOrders(unitID, udid, count)
end
end
end
Re: Cloning a Unit's Order Queue
I got it working, finally. I've found that Units don't get their CommandQueue before StartMoving() runs, though. Might be a useful factoid for people out there.
Re: Cloning a Unit's Order Queue
I have a problem with this too to get the widget I last mentioned.
Off course there is much more to the code but I took out the only thing that isn't working. In one way it works. The unit gets the order but it just ignores "alt", "ctrl", "shift", "right" options.
Code: Select all
function widget:CommandNotify(commandID, params, options)
Spring.GiveOrderToUnit(unitID, CMD.FIGHT, params, options.coded)
return true