Difference between revisions of "Talk:GetPositionX - ObjectReference"

From the CreationKit Wiki
Jump to navigation Jump to search
imported>Rasikko
m (global and local positions)
imported>Rasikko
 
(4 intermediate revisions by 2 users not shown)
Line 4: Line 4:
[[User:Lisselli|Lisselli]] ([[User talk:Lisselli|talk]])[[User:Lisselli|Lisselli]] ([[User talk:Lisselli|talk]]) 2017-10-06T11:57:52 (EDT)
[[User:Lisselli|Lisselli]] ([[User talk:Lisselli|talk]])[[User:Lisselli|Lisselli]] ([[User talk:Lisselli|talk]]) 2017-10-06T11:57:52 (EDT)


== GetCoordinateX ==
== GetCellCoordinates ==
<source lang="papyrus">
<source lang="papyrus">
Int Function GetCoordinateX(ObjectReference akRef)
Int[] Function GetCellCoordinates(ObjectReference akRef)
     ; Converts the objects' position to the X coordinate of the cell.
     ; Returns an array of this references' cell coordinates.
     Float GetPosX = akRef.GetPositionX()
     If GetPosX >= 0.0
     int posX = akRef.GetPositionX() as int
        Return GetPosX as int
     int posY = akRef.GetPositionY() as int
     Else
         Return Math.Floor(GetPosX)
     if (posX < 0)
     EndIf
         posX = (posX / 4096) - 1
EndFunction
     else
</source>
posX = posX / 4096
Use this to get the exact X coordinate for the cell. To be used only for exterior cells. Takes negative values into account. [[User:Lisselli|Lisselli]] ([[User talk:Lisselli|talk]]) 2017-10-27T12:22:34 (EDT)
    endif
: Math.Floor would give an inaccurate coordinate. If Y is -14.2, Floor will put it to -15. To properly handle positive and negatives in the case of coordinates, simply casting as int will do and no need to check if the position is negative or positive. --[[User:Rasikko|Rasikko]] ([[User talk:Rasikko|talk]]) 2018-01-10T12:39:49 (EST)
 
    if (posY < 0)
== Global and Local positions ==
        posY = (posY / 4096) - 1
When in the exterior worldspace, [[GetPositionX - ObjectReference]] and [[GetPositionY - ObjectReference]] return what I will call the '''global positions'''. These are accurate of course and do in fact help you determine where you are Skyrim. But what if you don't care about knowing the global positions? What if you can find out the '''local position'''. That is, the current position inside of a cell, ignoring any other cells around you. I shall explain..<br>
    else
 
        posY = posY / 4096
Every exterior cell is 4096x4096 units, and your global positions is based on that number and grows(or shrinks in the case of negative numbers) from there. The origin cell is 0 to 4096 on the X, and 4096 to 0 on the Y. Using a random spot in that cell, say your X is 215 and your Y is 943. That would be your global position in the world ''but'' it would also be your local position in that cell. The origin cell is the only cell where the global and local are the same. For all other cells, the local positions needs to be calculated from the global positions.<br><br> So how do you get the local position? First you need to know what is the '''starting X and Y values for the cell'''. For example, cell 0, 1 has a starting X value of 4096, and and starting Y value of 4096. For it is these numbers you must subtract your global positions from to get the local positions.
    endif
 
To get the X local position:
    Int[] CellCoordinates = new Int[2]
<source lang="papyrus">
    CellCoordinates[0] = posX
Float Function GetLocalPositionX(ObjectReference akObj) Global
    CellCoordinates[1] = posY
; Returns the local X position in a cell.
Float posX = akObj.GetPositionX()
; calculation is done by getting the starting x position for a cell.
    return CellCoordinates
return posX - (posX - (posX - ((posX / 4096) as int) * 4096)) as Float
EndFunction
</source>
<br>
To get the Y location position:
<source lang="papyrus">
Float Function GetLocalPositionY(ObjectReference akObj) Global
        ; Returns the local Y position in a cell.


Float posY = akObj.GetPositionY()
return posY - (posY - (posY - ((posY / 4096) as int) * 4096)) as Float
EndFunction
EndFunction
</source>
</source>
Proof:
To cut down on function calls, it does not check for interiors, so do not use this for references that are in an interior. Also the Z position was intentionally
<source lang="papyrus">
omit, as the purpose of this function is to obtain only the X,Y coordinates as seen in the Cell View Window. --[[User:Rasikko|Rasikko]] ([[User talk:Rasikko|talk]]) 2019-01-15T04:04:35 (EST)
[01/10/2018 - 06:17:26PM] The player's X: 23444.015625 The player's Y: -43183.757813
:I havent tested this in game but this looks like a worthy example to add on the primary page. Nice function. [[User:Scrivener07|Scrivener07]] ([[User talk:Scrivener07|talk]]) 2019-01-17T19:31:31 (EST)
 
::I prefer adding things to the talk page, to keep the primary page clean as possible, and only changing if, it I find a bug, or a "special" way the vanilla function has to be used. --[[User:Rasikko|Rasikko]] ([[User talk:Rasikko|talk]]) 2019-01-18T02:55:46 (EST)
[01/10/2018 - 06:17:26PM] The starting positions for this cell: X: 20480.000000 Y: -40960.000000
 
[01/10/2018 - 06:17:26PM] The player's local X: 2964.015625 The player's local Y: -2223.757813
</source>
It is possible to get the ending X/Y values for a cell, but they aren't necessary and are really just the starting values on a given side of a cell. --[[User:Rasikko|Rasikko]] ([[User talk:Rasikko|talk]]) 2018-01-10T13:28:47 (EST)

Latest revision as of 02:55, 18 January 2019

Getting Cell Coordinates[edit source]

Ever wondered how you can use the Cell View window's coordinates in game? Simple. You take the two values and you multiply them by 4096. If you want to convert an object's in game coordinates to use in the Cell View window, you divide the X and Y values by 4096 and cast as int to remove the decimals. Lisselli (talk)Lisselli (talk) 2017-10-06T11:57:52 (EDT)

GetCellCoordinates[edit source]

Int[] Function GetCellCoordinates(ObjectReference akRef)
    ; Returns an array of this references' cell coordinates.
	
    int posX = akRef.GetPositionX() as int
    int posY = akRef.GetPositionY() as int
	
    if (posX < 0)
        posX = (posX / 4096) - 1
    else
	posX = posX / 4096
    endif
	
    if (posY < 0)
        posY = (posY / 4096) - 1
    else
        posY = posY / 4096
    endif
	
    Int[] CellCoordinates = new Int[2]
    CellCoordinates[0] = posX
    CellCoordinates[1] = posY
	
    return CellCoordinates

EndFunction

To cut down on function calls, it does not check for interiors, so do not use this for references that are in an interior. Also the Z position was intentionally omit, as the purpose of this function is to obtain only the X,Y coordinates as seen in the Cell View Window. --Rasikko (talk) 2019-01-15T04:04:35 (EST)

I havent tested this in game but this looks like a worthy example to add on the primary page. Nice function. Scrivener07 (talk) 2019-01-17T19:31:31 (EST)
I prefer adding things to the talk page, to keep the primary page clean as possible, and only changing if, it I find a bug, or a "special" way the vanilla function has to be used. --Rasikko (talk) 2019-01-18T02:55:46 (EST)