• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Using Set X and Y

Status
Not open for further replies.
Level 13
Joined
Mar 24, 2013
Messages
1,105
Hello, I was wondering if someone could tell me how to use the setX and setY and incorporate it in a GUI Trigger. I'm trying to make a unit slide but not lose its current orders. Any help would be very much appreciated!

Thanks!
 
Level 20
Joined
Jul 14, 2011
Messages
3,213
You basically do it in JASS, since GUI doesn't give you easy direct access to X/Y movement.

Do your movement thing in GUI with "Unit - Set unit position". Then post it here and i'll give you some tips to turn it into JASS, replacing "Set unit position" with "SetUnitX" and "SetUnitY"

Note. If you aditionaly pick the trigger and go to Edit -> Convert to Custom Text" and paste that text here within jass tags [code=jass]TheText[/code] would be a lot better.

Note 2: You can do it in GUI, but with some (several) custom scripts :D
 
Level 20
Joined
Jul 14, 2011
Messages
3,213
Assuming he doesn't know the math behind moving unit towards angle based on X/Y, I asked him to post the trigger and script in order to take him, step by step, to the X/Y movement from the GUI positioning movement.
 
Level 20
Joined
Apr 14, 2012
Messages
2,901
It's pretty easy once you're really familiar with it's parameters:
  • Custom script : local real x = GetLocationX( GetUnitLoc(GetTriggerUnit()))
  • Custom script : local real y = GetLocationY( GetUnitLoc(GetTriggerUnit()))
  • Custom script : call SetUnitX( GetTriggerUnit(), x)
  • Custom script : call SetUnitY( GetTriggerUnit(), y)
See? It just takes a short amount of time to type, and (as far as I know) has no leaks. Just 4 lines of custom script.
 
Level 20
Joined
Jul 14, 2011
Messages
3,213
That's the easy part. Using 4 times GetTriggerUnit() is not good, and using twice "GetUnitLoc" means 2 leaks. The "Hard" part is the math involved in moving the unit towards angle via x/y. I say "Hard" because not many people is familiar with maths, polar projections, vectors, reals, coords, etc. In fact, I'm just able to do it because JNPG has the Bj's formulas.

BTW, using SetUnitX and SetUnitY is better than SetUnitPosition(newX, newY)?
 
Level 13
Joined
Mar 24, 2013
Messages
1,105
Wow! Thank you all for your replies.

@Chaosy
That's what I use right now, but I'm looking to expand my knowledge and also retain orders the moving unit has.

@Spartipilo
Is there some place to learn these "maths", I'm mainly looking to move the unit in a straight line so I figured it would be pretty easy xD

@MasterTrainer
If I save my unit to a variable would I just change GetTriggerUnit() to udg_myUnit?
 
BTW, using SetUnitX and SetUnitY is better than SetUnitPosition(newX, newY)?

SetUnitPosition() has its own quirks. SetUnitPosition() has pathing checks and will interrupt orders/animations. SetUnitX/Y() will retain orders/animations, but will not check pathing.

Because of the extra pathing checks, SetUnitPosition() becomes pretty slow.

Wow! Thank you all for your replies.

@Chaosy
That's what I use right now, but I'm looking to expand my knowledge and also retain orders the moving unit has.

@Spartipilo
Is there some place to learn these "maths", I'm mainly looking to move the unit in a straight line so I figured it would be pretty easy xD

@MasterTrainer
If I save my unit to a variable would I just change GetTriggerUnit() to udg_myUnit?

For the final question, yes you can just save it to a variable and replace GetTriggerUnit() with udg_<Variable Name>.

As for the question on how to learn the maths, you usually figure it out by looking at examples. This tutorial is pretty old, but it has solid information:
http://www.thehelper.net/threads/graphics-dynamic-effects-gui.35362/

You can also look up the GUI - JASS equivalents of certain calculations, such as DistanceBetweenUnits, AngleBetweenPoints, and polar position.

When you set a unit's position with an offset it is:
originalX + distance * Cos(some angle) = newX
originalY + distance * Sin(some angle) = newY

<some angle> should be in radians. If it is in degrees, you can multiply it by bj_DEGTORAD, or pi/180. This basically will get the new coordinates of a point, offset by some distance towards some angle.

You don't learn all of this stuff in normal math classes. You'll learn most of these concepts, but might not be able to put two and two together that easily. You learn the distance formula really early (I think Algebra 2), which is basically DistanceBetweenUnits(). AngleBetweenPoints() you'll also learn in either Geometry or Algebra2, but the form it is in [Atan2()], you might not be familiar with. Polar position you tend to learn in Calculus BC/college level calculus. You may be introduced to it earlier, though.

Basically, the point of that^ paragraph above is to say that it is kind of scattered information. Play around with it, and you'll begin to learn why certain things happen. Just look at examples. Don't be afraid to look up a slide or knockback in our spells section--there should be tons of them. :)
 
Level 20
Joined
Jul 14, 2011
Messages
3,213
as PurgeandFire said, you can check the maths involved in the GUI actions by turning them to custom script and looking for the natives of the BJ's.
 
Level 20
Joined
Jul 14, 2011
Messages
3,213

  • Actions
    • Set tempUnit = (Triggering unit)
    • Set tempLoc1 = (Position of tempUnit)
    • Set tempLoc2 = (tempLoc1 offset by 5.00 towards (Facing of tempUnit) degrees)
    • Unit - Move (Triggering unit) instantly to tempLoc2
    • Custom script: call RemoveLocation(udg_tempLoc1)
    • Custom script: call RemoveLocation(udg_tempLoc2)



JASS:
function Trig_JASS_movement_Actions takes nothing returns nothing
    set udg_tempUnit = GetTriggerUnit()
    set udg_tempLoc1 = GetUnitLoc(udg_tempUnit)
    set udg_tempLoc2 = PolarProjectionBJ(udg_tempLoc1, 5.00, GetUnitFacing(udg_tempUnit))
    call SetUnitPositionLoc( GetTriggerUnit(), udg_tempLoc2 )
    call RemoveLocation(udg_tempLoc1)
    call RemoveLocation(udg_tempLoc2)
endfunction



JASS:
function Trig_JASS_movement_Actions takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local real facing = GetUnitFacing(u)
    local location loc1 = GetUnitLoc(u)
    local location loc2 = PolarProjectionBJ(loc1, 5.00, facing)
    call SetUnitPositionLoc( u, loc2)
    call RemoveLocation(loc1)
    call RemoveLocation(loc2)
    set u = null
    set loc1 = null
    set loc2 = null
endfunction



JASS:
function PolarProjectionBJ takes location source, real dist, real angle returns location
    local real x = GetLocationX(source) + dist * Cos(angle * bj_DEGTORAD)
    local real y = GetLocationY(source) + dist * Sin(angle * bj_DEGTORAD)
    return Location(x, y)
endfunction



JASS:
function Trig_JASS_movement_Actions takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local location loc1 = GetUnitLoc(u)
    local real facing = GetUnitfacing(u)
    local real x = GetLocationX(loc1) + 5 * Cos(facing * bj_DEGTORAD)
    local real y = GetLocationY(loc1) + 5 * Sin(facing * bj_DEGTORAD)
    local location loc2 = Location(x, y)
    
    call SetUnitPositionLoc( u, loc2)
    
    call RemoveLocation(loc1)
    call RemoveLocation(loc2)
    
    set u = null
    set loc1 = null
    set loc2 = null
endfunction



JASS:
function Trig_JASS_movement_Actions takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local real facing = GetUnitFacing(u)
    local real x = GetUnitX(u) + 5 * Cos(facing * bj_DEGTORAD)
    local real y = GetUnitY(u) + 5 * Sin(facing * bj_DEGTORAD)
    local location loc2 = Location(x, y)
    
    call SetUnitPositionLoc( u, loc2)
    
    call RemoveLocation(loc2)
    
    set u = null
    set loc2 = null
endfunction



JASS:
function Trig_JASS_movement_Actions takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local real facing = (GetUnitFacing(u) * bj_DEGTORAD)
    local real x = GetUnitX(u) + 5 * Cos(facing)
    local real y = GetUnitY(u) + 5 * Sin(facing)
    
    call SetUnitX(u, x)
    call SetUnitY(u, y)
    
    set u = null
endfunction


I hope it makes it clear.
 
Status
Not open for further replies.
Top