Making a emitter at Powerzones

Started by piggood, June 09, 2019, 05:21:34 AM

Previous topic - Next topic

piggood

I don't Know What's Wrong But It only Destroys half of powerzones and doesn't create Any CRPL cores.
once
Self "Main" 2 2 SetImageScale
endonce

CurrentCoords 300 SetCreeper
30 Delay

:Destroyed
0 0 9999 TRUE GetAllUnitsInRange 0 do
->unit

<-unit GetUnitType "POWERZONE" eq if
<-unit GetUnitAt ->x ->y
Destroy(<-unit 0)
"CRPLCore" <-x <-y CreateUnit ->child
<-child "Just.crpl" AddScriptToUnit
endif
loop

Builder17

#1
Edit: Read message from Grabz for answer.

Grabz

The issue with your script lies here:
<-unit GetUnitAt ->x ->y
As per the wiki page for GetUnitAt, this is a function that takes two arguments: x and y, and returns the unit ID if a unit was found at said coordinates. In your script this is backwards, presumably because of a false assumption that this function would give the coordinates of the unit. This is the reason only half of power zones are destroyed - the function returns one argument but you are saving two, so you are saving what the function returns into `x`, and then saving a power zone UID from GetAllUnitsInRange into `y`, thus half of all the power zone UID's are lost from the stack.

Replace this line:
<-unit GetUnitAt ->x ->y
With this:

<-unit CONST_COORDX GetUnitAttribute ->x
<-unit CONST_COORDY GetUnitAttribute ->y

And your script should work fine. Reference: GetUnitAttribute

piggood


Grabz

As a side note, functions that return coordinates, for example:
CurrentCoords ->y ->x
always put x first on the stack, and then y, therefore to save them into variables, you first have to save y (as it was inserted last, it's the last item on the stack) then x.

This is largely arbitrary, you could have your own CRPL function that puts coordinates on the stack in the order y x, and then you would retrieve them by doing ->x ->y. None of built in CRPL functions use this order however so it is best to follow the standard.