Ok, got that problem solved. The best way to do it is through RecvFromUnsynced, I found. Here's a simple sample of how to select / re-select a Unit in the UI after completing a sync command that, for whatever reason, needs to break the UI focus. Public Domain.
Code: Select all
if (gadgetHandler:IsSyncedCode()) then
---------------------------------------START SYNCED
function DoSomeStuff(u, ud, team)
--Do Some Game Logic, ending with the value "y", which is a UnitID we want to select
SendToUnsynced("SelectMe", y) --This is sending a Table to RecvFromSynced on this frame, consisting of {Tablename,"SelectMe",y}
end
---------------------------------------END SYNCED
else
---------------------------------------UNSYNCED
function gadget:RecvFromSynced(name,unitID)
if (name == 'SelectMe') then
-- We're the value of the first argument, "name" against our value, "SelectMe", that we sent to Unsync earlier.
Spring.SelectUnitArray({unitID},true)
-- The second argument, the value of "y" in the previous section, is a unitID, so we're looking for that unitID now.
end
end
---------------------------------------END PROGRAM
end
Please feel free to correct any incorrect statements, I think I got the technical terms right, and the code certainly works. This is very simple, and runs only if something has been received from Sync on a given frame, I think, otherwise it's just an empty loop in unsync and doesn't check that IF/THEN within it, which is what I was trying to avoid here.
What would you use this for? Well, there are a lot of situations where you need to re-select things, because you've broken the UI focus for some reason. Or you could have code that needs to select, say, the 6 nearest friendly Units, then select them for some reason, such as a fake-transport script.
In my case, I'm using something like this (albeit with some fancy elements I'm not showing here) to do "transformer" code with. This was the last little wrinkle- I wanted the UI to behave "normally" insofar as the player is concerned, and retain the UI focus (in this case, what Units are selected) throughout the operation.
Since "transformer" code usually involves destroying a Unit (there is another way of dealing with this, through LuaMoveCtrl, but I'm not entirely happy with that yet), it's impossible to maintain UI focus on the original UnitID. Therefore a method of selecting the Unit that has been newly-created was necessary, so that users will not lose focus, and have to click on the Unit again (remember, in users' minds, this is one Unit, not a Unit we just killed that's been replaced with a completely new one- we don't want to break that illusion).