Knuckle Cracker

Creeper World 3 => The Coder's Corner => Topic started by: piggood on June 09, 2019, 05:21:34 AM

Title: Making a emitter at Powerzones
Post by: piggood on June 09, 2019, 05:21:34 AM
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
Title: Re: Making a emitter at Powerzones
Post by: Builder17 on June 09, 2019, 06:21:13 AM
Edit: Read message from Grabz for answer.
Title: Re: Making a emitter at Powerzones
Post by: Grabz on June 09, 2019, 06:31:00 AM
The issue with your script lies here:
<-unit GetUnitAt ->x ->y
As per the wiki page for GetUnitAt (https://knucklecracker.com/wiki/doku.php?id=crpl:docs: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 (https://knucklecracker.com/wiki/doku.php?id=crpl:docs:getunitattribute)
Title: Re: Making a emitter at Powerzones
Post by: piggood on June 09, 2019, 07:54:39 AM
Thanks!  :D
Title: Re: Making a emitter at Powerzones
Post by: Grabz on June 10, 2019, 04:20:55 AM
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.