Unit UIDs?

Started by Jackeea, December 22, 2013, 07:05:33 PM

Previous topic - Next topic

Jackeea

Okay, reading the wiki and going through the CRPL reference, I realise that there's a thing called a UID, which lets you select units via CRPL. However, I really haven't a clue how they work, to be honest. I know that you can get them by using "GetUnitsInRange" etc, but I don't know how to get the UID, how to store it, and basically what to do with them. For example, in a map I'm making, I need to find a way to return all landed Command Nodes to orbit, how would I do something like that?

Clean0nion

Quote from: Jackeea on December 22, 2013, 07:05:33 PM
Okay, reading the wiki and going through the CRPL reference, I realise that there's a thing called a UID, which lets you select units via CRPL. However, I really haven't a clue how they work, to be honest. I know that you can get them by using "GetUnitsInRange" etc, but I don't know how to get the UID, how to store it, and basically what to do with them. For example, in a map I'm making, I need to find a way to return all landed Command Nodes to orbit, how would I do something like that?
once
   CreateList ->landedNodes
endonce

0 0 9999 0 GetAllUnitsInRange 0 do
  ->checkee
  <-checkee GetUnitType "COMMANDNODE" eq if
     <-landedNodes <-checkee AppendToList
  endif
loop

<-landedNodes GetListCount 0 do
  <-landedNodes I GetListElement 0 Destroy
loop

Grayzzur

The unit ID is basically just an integer, but it's used as a key to access a particular unit for any CRPL command that needs a unit ID. "Self" is just a keyword for the unit ID of the unit that your script is currently running in.

This grabs pretty much every unit on the map (x/y of the script unit, 9999 range, true for square box) - then sets up a do loop to run through them all
CurrentCoords 9999 true GetAllUnitsInRange 0 do
Store the unit ID in case we need it, because the GetUnitType command would consume it
   ->uid
Get the unit's type, and if it equals "COMMANDNODE" do something with it
   <-uid GetUnitType "COMMANDNODE" eq if
For command nodes, destroying them sends them back to orbit, so *boom*
      <-uid 0 Destroy
   endif
loop
"Fate. It protects fools, little children, and ships named 'Enterprise.'" -William T. Riker

Jackeea

Quote from: Grayzzur on December 22, 2013, 07:35:04 PM
The unit ID is basically just an integer, but it's used as a key to access a particular unit for any CRPL command that needs a unit ID. "Self" is just a keyword for the unit ID of the unit that your script is currently running in.

This grabs pretty much every unit on the map (x/y of the script unit, 9999 range, true for square box) - then sets up a do loop to run through them all
CurrentCoords 9999 true GetAllUnitsInRange 0 do
Store the unit ID in case we need it, because the GetUnitType command would consume it
   ->uid
Get the unit's type, and if it equals "COMMANDNODE" do something with it
   <-uid GetUnitType "COMMANDNODE" eq if
For command nodes, destroying them sends them back to orbit, so *boom*
      <-uid 0 Destroy
   endif
loop

Yeah, this script worked, thanks! One question that I do have, though, is when you do the ->uid bit, then the <-uid , does it cycle through the list, performing the next bit of the list with every item of the list? If so, how does the <-uid 0 Destroy part work?

eduran

GetAllUnitsInRange returns the UIDs of all units it finds and puts them on the stack, together with the number of units found (which is at the top of the stack). At this point the stack looks like this, from bottom to top:
uidN uid(N-1) .... uid2 uid1 numberOfUnitsFound
'numberOfUnitsFound' is popped off and used by 'do', so the do loop runs exactly as many times as there are UIDs on the stack. The '->uid' bit at the start of the do-loop pops the topmost item from the stack and stores it for later use in a variable called 'uid'. On the next loop iteration '->uid' grabs the next item. The loop repeats until it went through all UIDs.